[英]How to iterate through a string and find the duplicated values
My question is that I can't seem to find the right method for iterating through subjects2 and pick out the duplicated strings. 我的问题是,我似乎找不到正确的方法来遍历subject2并挑选出重复的字符串。 Below is my method:
下面是我的方法:
nosubjects = []
subjects2 = ["hi","hi","bi","ki","si","bi","li"]
for i in subjects2:
if subjects2.count(i)==2:
nosubjects.extend(i)
print(nosubjects)
But when I print it out it appears like this: 但是当我打印出来时,它看起来像这样:
['hi','hi']
['h', 'i', 'h', 'i','b', 'i']
['hi', 'i', 'h', 'i', 'b', 'i', 'b', 'i']
Please help thanks! 请帮忙谢谢!
Use collections.Counter
to get count of each element and take only those whose count exceeds 1: 使用
collections.Counter
可以获取每个元素的计数,并仅获取计数超过1的元素:
from collections import Counter
subjects2 = ['hi', 'hi', 'bi', 'ki', 'si', 'bi', 'li']
nosubjects = [x for x, i in Counter(subjects2).items() if i > 1]
print(nosubjects)
# ['hi', 'bi']
Problems in your code: 您的代码中的问题:
nosubjects
inside the if condition which will cause it to be printed multiple times nosubjects
,这将导致它多次打印 Use sets
. 使用
sets
。 First to get unique set of elements in the list, then you can check if the count of each element in the set exceeds 1 in the original list. 首先获取列表中唯一的元素集,然后可以检查原始列表中集合中每个元素的计数是否超过1。
nosubjects = []
subjects2 = ['hi','hi','bi','ki','si','bi','li']
for i in set(subjects2):
if subjects2.count(i)>=2:
nosubjects.append(i)
print(nosubjects)
Using list comprehension: 使用列表理解:
subjects2 = ['hi','hi','bi','ki','si','bi','li']
nosubjects = [i for i in set(subjects2) if subjects2.count(i) >=2]
print(nosubjects)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.