gpt4 book ai didi

python - 计算列表中的元素会产生意外结果

转载 作者:行者123 更新时间:2023-11-30 22:51:45 25 4
gpt4 key购买 nike

我正在尝试计算句子中每个字符的出现次数。我使用了下面的代码:

printed = False
sentence = "the quick brown fox jumps over the lazy dog"
chars = list(sentence)
count = 0
for char in chars:
if char == ' ':
chars.remove(char)
if printed == False:
count = chars.count(char)
print "char count: ", char, count
else:
printed = False

问题是,除了第一个单词之外,每个单词的第一个字母都没有打印,并且计数不正确(每次有新单词开始,计数就减 1,从 7 开始):

['t', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', 'e', 'r', ' ', 't', 'h', 'e', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']
char count: t 2
char count: h 2
char count: e 3
char count: 7
char count: u 2
char count: i 1
char count: c 1
char count: k 1
char count: 6
char count: r 2
char count: o 4
char count: w 1
char count: n 1
char count: 5
char count: o 4
char count: x 1
char count: 4
char count: u 2
char count: m 1
char count: p 1
char count: s 1
char count: 3
char count: v 1
char count: e 3
char count: r 2
char count: 2
char count: h 2
char count: e 3
char count: 1
char count: a 1
char count: z 1
char count: y 1
char count: 0
char count: o 4
char count: g 1

当我创建 2 个 for 循环(而不是 1 个)时,效果会更好:

sentence = "the quick brown fox jumps over the lazy dog"
chars = list(sentence)
count = 0
for char in chars:
if char == ' ':
chars.remove(char)
print chars
printed = False

for char in chars:
if printed == False:
count = chars.count(char)
print "char count: ", char, count
printed = True
else:
printed = False

这是输出:

['t', 'h', 'e', 'q', 'u', 'i', 'c', 'k', 'b', 'r', 'o', 'w', 'n', 'f', 'o', 'x', 'j', 'u', 'm', 'p', 's', 'o', 'v', 'e', 'r', 't', 'h', 'e', 'l', 'a', 'z', 'y', 'd', 'o', 'g']
char count: t 2
char count: e 3
char count: u 2
char count: c 1
char count: b 1
char count: o 4
char count: n 1
char count: o 4
char count: j 1
char count: m 1
char count: s 1
char count: v 1
char count: r 2
char count: h 2
char count: l 1
char count: z 1
char count: d 1
char count: g 1

唯一的问题是,“o”字符在输出中出现了两次...这是为什么?另外,为什么 1 循环不起作用?

最佳答案

迭代列表的副本并使用elif,否则在删除后继续,您不想计数空白处。

printed = False
sentence = "the quick brown fox jumps over the lazy dog"
chars = list(sentence)

for char in chars[:]:
if char == ' ':
chars.remove(char)
continue
if not printed:
count = chars.count(char)
print "char count: ", char, count
printed = True
else:
printed = False

您也可以在按空格分割后仅str.join:

for char in "".join(sentence.split()):
if not printed:
count = chars.count(char)
print "char count: ", char, count
printed = True
else:
printed = False

但是,即使复制列表,您自己的解决方案实际上也无法正常工作,您在输出中丢失了字母:

char count:  t 2
char count: e 3
char count: u 2
char count: c 1
char count: b 1
char count: o 4
char count: n 1
char count: o 4
char count: j 1
char count: m 1
char count: s 1
char count: v 1
char count: r 2
char count: h 2
char count: l 1
char count: z 1
char count: d 1
char count: g 1

字符串中有 26 个唯一字母,但您输出了 ~17 个。

您需要的是跟踪看到的字母并只打印一次计数,您的代码不会记录已打印的字符,它只是随机设置一个标志:

sentence = "the quick brown fox jumps over the lazy dog"
chars = list(sentence)

printed = set()
for char in "".join(sentence.split()):
if char not in printed:
count = chars.count(char)
print "char count: ", char, count
printed.add(char)

或者,如果第一次看到的顺序并不重要,那么只需在字符串上调用 set 即可:

for char in set("".join(sentence.split())):
count = chars.count(char)
print "char count: ", char, count

或者,如果您有大量数据,那么使用 Counter dict 会更好。 :

from collections import Counter
for char, count in Counter("".join(sentence.split())).items():
print(char, count)

关于python - 计算列表中的元素会产生意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38916042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com