用整数序列代替每个字符

发布于 2025-01-25 11:32:00 字数 692 浏览 0 评论 0原文

这是我的代码一个函数

def seq():
   q=1
   n=rann()
   List=[n]
   while q<n:
       if (n % 2):
           n = 3*n + 1
           List.append(n)
       else:
           n=n//2
           List.append(n)
   if (len(List)>=len(y)):
       print(List)
   else:
       return(rann())

x=input("enter name")    #the next four lines of the code are my first trial.
y= [char for char in x]
for i in y:
    y[i]=rann()
seq()

,我有两个函数,即第一个函数rann(),生成一个整数,另一个函数(上面的代码)基于该数字生成序列。我想取一个输入X并用生成的序列替换每个字符。我尝试这样做

text = 'bat ball'

y=[char for char in text]
y[0:] = rann()
print(y)

,例如,让输入为“蝙蝠球”,所以我想要的是按序列的每个项替换输入的每个字符。 我知道有很多错误,但我无法弄清楚另一种方法。

Here is one function from my code

def seq():
   q=1
   n=rann()
   List=[n]
   while q<n:
       if (n % 2):
           n = 3*n + 1
           List.append(n)
       else:
           n=n//2
           List.append(n)
   if (len(List)>=len(y)):
       print(List)
   else:
       return(rann())

x=input("enter name")    #the next four lines of the code are my first trial.
y= [char for char in x]
for i in y:
    y[i]=rann()
seq()

I have two functions, the 1st function, rann(), generates an integer and the other(the code above) generates a sequence based on that number. I wanted to take an input x and replace every character with the sequence generated. I tried doing this

text = 'bat ball'

y=[char for char in text]
y[0:] = rann()
print(y)

For example let the input be "bat ball" so what I wanted was to replace every character of the input by every term of the sequence.
I know there is a lot of things wrong But I can't figure out another method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最单纯的乌龟 2025-02-01 11:32:00

你是这样的吗?我为rann(x)生成了字符x的ASCII代码值,但是您会得到这个想法。

# example rann()
def rann(x):
    return [ord(ch) for ch in x]

def seq(n):
   q = 1
   List = [n]
   while q < n:
       if n % 2:
           n = 3*n + 1
           List.append(n)
       else:
           n = n // 2
           List.append(n)
   if len(List) >= len(y):
       print(List)
   else:
       return n

text = 'bat ball'
x = [char for char in text]
y = rann(x)
print(y)

[seq(n) for n in y]

You mean like this? I contrived a rann(x) to generate the ascii code value of the character x, but you get the idea.

# example rann()
def rann(x):
    return [ord(ch) for ch in x]

def seq(n):
   q = 1
   List = [n]
   while q < n:
       if n % 2:
           n = 3*n + 1
           List.append(n)
       else:
           n = n // 2
           List.append(n)
   if len(List) >= len(y):
       print(List)
   else:
       return n

text = 'bat ball'
x = [char for char in text]
y = rann(x)
print(y)

[seq(n) for n in y]
白馒头 2025-02-01 11:32:00

查看您要在此处生成哪种整数或序列 - 请发布一个示例将很有用。

据我了解,您想根据输入字符串生成数字列表?

最简单的方法可能是为每个字符使用ASCII代码:

def seq():
    '''
    (None) -> List of int
    Get user input and convert to int list
    '''
    nums = []
    text = input("Enter text: ")
    for ch in text:
        nums.append(ord(ch))
    return nums

从INT值中构建新列表要比突变您正在迭代的列表更容易。

如果RANN生成了Char的INT列表,请用RANN替换上面的ORD,然后返回INT列表。

您的预期输出样本确实会在这里有所帮助。

It would be useful to see what kind of integer or sequence you are trying to generate here- please post an example.

From what I can understand, you want to generate a list of numbers based on the input string?

the easiest way may be to use the ascii code for each char like this:

def seq():
    '''
    (None) -> List of int
    Get user input and convert to int list
    '''
    nums = []
    text = input("Enter text: ")
    for ch in text:
        nums.append(ord(ch))
    return nums

It is easier to just build a new list from the int values than to mutate a list that you are iterating through.

If rann generates a list of int from the char, replace ord above with rann and have it return a list of int.

A sample of you expected output would really help here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文