【问题标题】:How to List alphabet characters based on Pi?如何根据 Pi 列出字母字符?
【发布时间】: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)        

Here is a small scale sample of what I want it to do. Only the first 11 letters of the alphabet was used here.

我希望程序返回一组 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


    【解决方案1】:

    从小做起

    我建议你给你的程序更多的结构。也许从定义一些概念开始,然后分别构建它们:

    def pi_digits(n):
      """get pi with n n-1 decimal places: pi_digits(3) returns 3.14"""
      # build this function first
      my_num = ...
      return my_num
    
    
    def digit_to_char(d):
      """convert digit to character: digit_to_char(0) returns 'a'"""
      # build this function next
      my_char = ...
      return my_char
    
    
    # test both functions
    print(pi_digits(10))
    print(digit_to_char(2))
    

    确保这有效。然后展开迭代。

    迭代

    一旦你有了这个,尝试迭代 Pi 的数字:

    pi_digits = str(pi_digits)
    
    my_output_str = ''
    
    for char_digit in pi_digits:
      if char_digit == '.':
        continue # skip if the character is a decimal point
      print('digit:', digit)
      char = char_digit
      # convert character to numerical digit (int)
      char = int(char)
      # convert numerical digit to alphabetical char
      char = digit_to_char(char)
      my_output_str += char 
    

    注意:因为数字只有 0-9,但有 27 个字母字符。

    无论哪种方式,您都可以将代码包装在另一个函数中,例如get_ndigit_picode(n)

    【讨论】:

    • 只是做了功能,不知道如何在cmets中分享为代码。
    • 编辑您的问题! :) 只需在下面添加您的新代码 使用``` 制作一个新代码部分
    猜你喜欢
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多