用整数序列代替每个字符
这是我的代码一个函数
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是这样的吗?我为
rann(x)
生成了字符x
的ASCII代码值,但是您会得到这个想法。You mean like this? I contrived a
rann(x)
to generate the ascii code value of the characterx
, but you get the idea.查看您要在此处生成哪种整数或序列 - 请发布一个示例将很有用。
据我了解,您想根据输入字符串生成数字列表?
最简单的方法可能是为每个字符使用ASCII代码:
从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:
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.