[英]Getting a list of unique words (i.e., not duplicates) in Python
I have a list with several words and I want to print the unique words from that list.我有一个包含多个单词的列表,我想打印该列表中的唯一单词。 With "unique" words I mean words that only appear once in my original list.
对于“独特”词,我的意思是在我的原始列表中只出现一次的词。 That is, if a word appears twice (or more than twice), then it shouldn't be printed.
也就是说,如果一个单词出现两次(或两次以上),则不应打印它。
Here's my list of words:这是我的单词列表:
my_list = ["door", "table", "door", "chair", "couch", "door", "table", "closet"]
And here's the code I've tried so far:这是我到目前为止尝试过的代码:
print(set(my_list))
However, set
prints a list with ALL the words, though there aren't duplicates.但是,
set
打印一个包含所有单词的列表,尽管没有重复。 That is: door, table, chair, couch, closet
.即:
door, table, chair, couch, closet
。 However, what I want is a list like chair, couch, closet
(because they are the ones that appear only once in the list).但是,我想要的是像
chair, couch, closet
这样的列表(因为它们是列表中只出现一次的列表)。
Not a neat way to achieve what you want, but still a good workaround, by using Counter
.通过使用
Counter
,这不是实现您想要的目标的巧妙方法,但仍然是一个很好的解决方法。 Counter will count how many times each item appear in a list.计数器将计算每个项目在列表中出现的次数。
from collections import Counter
my_list = ["door", "table", "door", "chair", "couch", "door", "table", "closet"]
my_list_count = Counter(my_list) # Counter({'door': 3, 'table': 2, 'chair': 1, 'closet': 1, 'couch': 1})
# Unique item have count = 1
print([xx for xx in my_list_count if my_list_count[xx] == 1])
# Results: ['chair', 'closet', 'couch']
You can use something like -你可以使用类似的东西 -
res = [x for x in my_list if my_list.count(x) == 1]
It will return list of elements that occur once.它将返回出现一次的元素列表。
You can use the Counter
for this, which will create a dictionary with each unique word as the key and the respective occurrence count as the corresponding value.您可以为此使用
Counter
,它将创建一个字典,其中每个唯一的单词作为键,相应的出现次数作为相应的值。 Then iterate through the dictionary to find the key with the value of 1
.然后遍历字典以找到值为
1
的键。 The sample code is as follows:示例代码如下:
from collections import Counter
my_list = ["door", "table", "door", "chair", "couch", "door", "table", "closet"]
count_all = Counter(my_list)
for key, value in count_all.items():
if 1 == value:
print key
Here's a short snippet on how you can go about it.这里有一个关于如何去做的简短片段。
from collections import Counter
count_dict = Counter(my_list)
for k, v in count_dict.items():
if v == 1:
print(k)
Here is something you can try这是你可以尝试的东西
def keep_unique(array):
counts = {}
for e in array:
if e not in counts:
counts[e] = 0
counts[e] += 1
return [k for (k, v) in counts.items() if v == 1]
print(keep_unique(["door", "table", "door", "chair", "couch", "door", "table", "closet"]))
The keep_unique
method counts the number of occurrences of each element and then returns only the ones which appear only once. keep_unique
方法计算每个元素出现的次数,然后只返回只出现一次的元素。
A solution without Counter
could be to sort your list and check if surrounding words are different:没有
Counter
的解决方案可能是对您的列表进行排序并检查周围的单词是否不同:
my_list = ["door", "table", "door", "chair", "couch", "door", "table", "closet"]
my_list.sort()
unique = [word for i, word in enumerate(my_list) if my_list[i - 1] != word and my_list[i + 1] != word]
print(unique)
# ['chair', 'closet', 'couch']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.