【发布时间】:2021-04-20 04:00:03
【问题描述】:
您好,我想创建一个使用 pi 值随机排列的字母列表。 我现在很困。只是想提出一些建议来解决这个代码。
import re
import string
#I searched values of pi on the web and set it as x.
#I used this for loop to get a list of pi values with range from 1 to 100k
pi_list = []
for num in range(0, 10000):
pi_list.append(x[num])
#I used this method to join the values into one entry of string
#I think setting it to string would help for slicing later.
pi_values = "".join(pi_list)
#reference list to be used later
a = list(string.ascii_lowercase)
#string of pi values
b = pi_values
#empty list I want to put return values
c = []
#This is by far my code right now.
while len(c) < 27: #27 because alphabet has 27 letters
x = 2 #This x is for concatenation
for i in range(len(a)):
#I want to use pi values from b to return numbers to c then I will assign c list numbers later on to
#objects inside the list a.
while int(b[i]) % 27 in c:
b[i] = int(b[i:i+x])
x+=1
i+=1
#The objective here is to concatenate the next pi value if the current one exists in c and set that as
#the new value for i when the object already exists in list c. Then I use modulus 27 for values with 3
#digits and above. I want the concatenation to continue until it gets a new value that's not in c yet.
else:
c.append(int(b[i]))
print(c)
我希望程序返回一组 27 个字母字符,每个字母 1 次。就像图片中的示例一样。
现在,它返回这些值。
['3', '1', '4', '1', '5', '9', '2', '6', '5', '3', '5', '8', '9', '7', '9', '3', '2', '3', '8', '4', '6', '2', '6', '4', '3', '3', '3', '1', '4', '1', '5', '9', '2', '6', '5', '3', '5', '8', '9', '7', '9', '3', '2', '3', '8', '4', '6', '2', '6', '4', '3', '3']
它似乎会在 27 次后重复。不知道在这里做什么。很高兴得到一些帮助。
【问题讨论】:
标签: python-3.x