提问人:thealienman 提问时间:10/21/2023 最后编辑:mkrieger1thealienman 更新时间:10/21/2023 访问量:53
从大括号中删除频率列表以向下列出项目 [复制]
Removing frequency list from braces to list items downwards [duplicate]
问:
我一直在尝试解决一项任务,该任务要求我生成一个程序来确定字符串中字母的频率。
我生产了一些几乎满足任务要求的东西,但并不完全是。最后一个元素是列出字母表的字母以及每个字母以这种格式出现在字符串中的频率:
A: 10
B: 2
C : 1
... etc.
我的解决方案让我走到了这一步:
{'a': 10, 'b': 1, 'c': 2, 'd': 2, 'e': 2, 'f': 2, 'g': 0, 'h': 4, 'i': 6, 'j': 0, 'k': 0, 'l': 0, 'm': 2, 'n': 7, 'o': 9, 'p': 0, 'q': 0, 'r': 2, 's': 0, 't': 10, 'u': 5, 'v': 2, 'w': 2, 'x': 0, 'y': 2, 'z': 0}`
但我无法弄清楚如何从大括号中删除此列表并如上所示显示它们。
这是我的代码:
message = "You can have data without information, but you cannot have information without data."
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
frequency = { "a": 0, "b": 0, "c": 0, "d": 0, "e": 0, "f": 0, "g": 0, "h": 0, "i": 0, "j": 0, "k": 0, "l": 0, "m": 0, "n": 0, "o": 0, "p": 0, "q": 0, "r": 0, "s": 0, "t": 0, "u": 0, "v": 0, "w": 0, "x": 0, "y": 0, "z": 0 }
for i in alphabet:
for j in message.lower():
if i == j:
frequency[i] += 1
print(frequency)
这会产生以下结果:
{'a': 10, 'b': 1, 'c': 2, 'd': 2, 'e': 2, 'f': 2, 'g': 0, 'h': 4, 'i': 6, 'j': 0, 'k': 0, 'l': 0, 'm': 2, 'n': 7, 'o': 9, 'p': 0, 'q': 0, 'r': 2, 's': 0, 't': 10, 'u': 5, 'v': 2, 'w': 2, 'x': 0, 'y': 2, 'z': 0}
谁能帮我了解如何将这些结果转换为此:
a: 10
b: 1
c: 2
...
答:
0赞
XMehdi01
10/21/2023
#1
此代码计算每个字符串中出现的次数,并以字母
: 次数
letter
alphabet
message = "You can have data without information, but you cannot have information without data."
alphabet = "abcdefghijklmnopqrstuvwxyz"
frequency = {char: 0 for char in alphabet}
for char in message.lower():
if char in frequency:
frequency[char] += 1
for char in alphabet:
print(f"{char}: {frequency[char]}")
0赞
Isaias
10/21/2023
#2
它实际上很容易,只需使用返回键值对元组的字典的 items 方法即可。
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
frequency = {letter: 0 for letter in alphabet} # you can use for clause comprehension to initialize the letters.
message = message.lower()
for char in message:
if char in frequency:
frequency[char] += 1
for letter, count in frequency.items(): # use items method then unpack the tuple.
print(f"{letter.upper()}: {count}")
评论
0赞
XMehdi01
10/21/2023
您添加了此行无济于事,您可以使用此行message = message.lower()
for char in message.lower():
0赞
Isaias
10/24/2023
如果您想要一条线解决方案,那就去吧!from collections import Counter import re message = "You can have data without information, but you cannot have information without data." print({ **{ chr(l): 0 for l in range(ord('A'), ord('Z') + 1)}, **Counter(re.sub(r'[^A-Z]', '', message.upper()))})
0赞
Codist
10/21/2023
#3
与许多类似的挑战一样,有多种方法可以实现您的目标。
您可能应该使用集合模块中的 Counter 类,以便:
from collections import Counter
message = "You can have data without information, but you cannot have information without data."
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
counter = Counter(message.upper())
for c in alphabet:
print(f"{c}: {counter.get(c, 0)}")
输出:
A: 10
B: 1
C: 2
D: 2
E: 2
F: 2
G: 0
H: 4
I: 6
J: 0
K: 0
L: 0
M: 2
N: 7
O: 9
P: 0
Q: 0
R: 2
S: 0
T: 10
U: 5
V: 2
W: 2
X: 0
Y: 2
Z: 0
评论
0赞
XMehdi01
10/21/2023
您提供的代码不完整;您必须首先定义 .Counter
0赞
Codist
10/21/2023
@XMehdi01 感谢您的宝贵建议。但是,我不倾向于定义 Counter。我更喜欢导入它
0赞
XMehdi01
10/21/2023
是的,但您提供的代码不起作用,因为未定义Counter
0赞
Codist
10/21/2023
@XMehdi01 您可能需要查看您最近的评论
0赞
XMehdi01
10/21/2023
是的,但您修改了代码 😉
评论