简体   繁体   English

在 Python 中获取唯一单词列表(即,不重复)

[英]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.

相关问题 python 相当于 filter() 获取两个输出列表(即列表的分区) - python equivalent of filter() getting two output lists (i.e. partition of a list) 我将如何使Python识别字符串的类别(即不同的语法类型的单词) - How would I make Python recognize categories of strings (i.e. different grammatical types of words) 使用 SQLAlchemy,我可以在查询中加入 Python object(即列表)吗? - Using SQLAlchemy, can I join a Python object (i.e. a list) within a Query? 使用tweepy和python从tweet获取其他图像URL(即不仅是第一个) - Getting additional image urls (i.e. not just first) from tweet with tweepy and python Python 正则表达式匹配数字但忽略其中包含数字的单词(即 N95 和 3D) - Python regex to match numbers but ignore words that have numbers in them (i.e. N95 and 3D) Python-熊猫替换数据框列值-列数据存储为列表(即'[this,that,']) - Python - Pandas replacing dataframe column values - column data stored as list (i.e., '[this, that,']) Python如何使用枚举和列表跳过第一(即零)迭代? - Python how to skip the first (i.e. zero) iteration using enumerate and a list? Python从列表列表中删除项目,即从子列表中删除项目 - Python remove items from a list of lists i.e. remove items from sublists 一种在python中存储素数的方法(即,有一个列表检查元素是否存在,并且可以按顺序输出元素) - A way to store primes in python (i.e. have a list that checks if a element is there, and that can output elements in order) Python 中是否有一种简短的方法来检查 object 是否类似于列表(即“序列”但不是“字符串”)? - Is there a short way in Python to check if an object is list-like (i.e. a `Sequence` but not a `str`)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM