📜  Python – 索引频率字母表

📅  最后修改于: 2022-05-13 01:55:05.991000             🧑  作者: Mango

Python – 索引频率字母表

创建一个字符串列表,其中每个字符重复与其位置编号一样多。

方法 #1:使用 ascii_lowercase + ord() + 循环

在这里,获取索引的任务是使用 ord() 完成的,ascii lowercase() 用于提取字母。循环用于对每个字母执行任务。

Python3
# Python3 code to demonstrate working of
# Index Frequency Alphabet List
# Using ascii_lowercase + ord() + loop
from string import ascii_lowercase
 
# extracting start index
strt_idx = ord('a') - 1
 
res = []
for ele in ascii_lowercase:
     
    # multiplying Frequency
    res.append(ele * (ord(ele) - strt_idx))
 
# printing result
print("The constructed list : " + str(res))


Python3
# Python3 code to demonstrate working of
# Index Frequency Alphabet List
# Using list comprehension + ascii_lowercase + ord()
from string import ascii_lowercase
 
# extracting start index
strt_idx = ord('a') - 1
 
# list comprehension to solve as one liner
res = [ele * (ord(ele) - strt_idx) for ele in ascii_lowercase]
 
# printing result
print("The constructed list : " + str(res))


输出
The constructed list : ['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz']

方法 #2:使用列表理解 + ascii_lowercase + ord()

在此,列表推导用于解决此问题。这是上述方法的简写。

Python3

# Python3 code to demonstrate working of
# Index Frequency Alphabet List
# Using list comprehension + ascii_lowercase + ord()
from string import ascii_lowercase
 
# extracting start index
strt_idx = ord('a') - 1
 
# list comprehension to solve as one liner
res = [ele * (ord(ele) - strt_idx) for ele in ascii_lowercase]
 
# printing result
print("The constructed list : " + str(res))
输出
The constructed list : ['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz']