计算字符串列表中的特定字符
嗨,我要做的是计算下列表中每个字符串共有的字符的特定出现,并打印出每个字符串以及出现的常见字符的次数 例子 巴拉克A出现2 Barack R出现1 当我运行代码时,它会打印每个字符出现1
list1 = ['barack', 'obar?ma', '?america?', 'war', 'russia?', 'mak?er']
common = set.intersection(*map(set,list1))
new_list = list(common)
for i in list1:
for a in new_list:
if a in i:
x = new_list.count(a)
print([i] + [a])
print(x)
Hi what I am trying to do is count the specific occurrences of the characters common to each string in the list below and print out each string and how many times the common characters appear
example
barack a appears 2
barack r appears 1
when I run my code it prints that each character appears 1
list1 = ['barack', 'obar?ma', '?america?', 'war', 'russia?', 'mak?er']
common = set.intersection(*map(set,list1))
new_list = list(common)
for i in list1:
for a in new_list:
if a in i:
x = new_list.count(a)
print([i] + [a])
print(x)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 x = new_list.count(a) 更改为 x = i.count(a)。目前您正在计算
'a'
中有多少个a
;您想要计算barack
中有多少个a
。经过此修改的新代码:
打印:
Change
x = new_list.count(a)
tox = i.count(a)
. At the moment you are counting how manya
s in'a'
; you want to count how manya
s inbarack
.The new code with this modification:
prints this: