Python 计算列表中唯一单词的数量
使用中的以下代码,我能够找到一个单词是否唯一(通过比较它是否使用过一次或多次): 但是,如果我有一个包含数百个单词的字符串,我如何计算该字符串中唯一单词的数量 例如,我的代码有:Python 计算列表中唯一单词的数量,python,python-3.x,Python,Python 3.x,使用中的以下代码,我能够找到一个单词是否唯一(通过比较它是否使用过一次或多次): 但是,如果我有一个包含数百个单词的字符串,我如何计算该字符串中唯一单词的数量 例如,我的代码有: uniqueWordCount = 0 helloString = ['hello', 'world', 'world', 'how', 'are', 'you', 'doing', 'today'] count = {} for word in words : if word in count :
uniqueWordCount = 0
helloString = ['hello', 'world', 'world', 'how', 'are', 'you', 'doing', 'today']
count = {}
for word in words :
if word in count :
count[word] += 1
else:
count[word] = 1
如何将
uniqueWordCount
设置为6
?通常,我真的很擅长解决这些类型的算法难题,但我一直没有成功地解决这个问题。我觉得它就在我的鼻子底下。在当前代码中,您可以在else
的情况下增加uniqueWordCount
,如果您已经设置了count[word]
,或者只需查找字典中的键数:
如果您只想知道唯一元素的数量,然后在
集合中获取元素
:您有很多选择,我推荐一个集合,但您也可以使用计数器,它计算数字显示的数量,或者您可以查看您制作的词典的键数
设置 您还可以将列表转换为集合,其中所有元素都必须是唯一的。不会丢弃唯一的元素:
helloString = ['hello', 'world', 'world', 'how', 'are', 'you', 'doing', 'today']
helloSet = set(helloString) #=> ['doing', 'how', 'are', 'world', 'you', 'hello', 'today']
uniqueWordCount = len(set(helloString)) #=> 7
这里有一个链接,可以进一步阅读
柜台
您还可以使用计数器,如果您仍然需要该信息,它还可以告诉您单词的使用频率
from collections import Counter
helloString = ['hello', 'world', 'world', 'how', 'are', 'you', 'doing', 'today']
counter = Counter(helloString)
len(counter) #=> 7
counter["world"] #=> 2
环
在循环结束时,您可以检查计数的len
,并且您将helloString
误输入为单词
:
uniqueWordCount = 0
helloString = ['hello', 'world', 'world', 'how', 'are', 'you', 'doing', 'today']
count = {}
for word in helloString:
if word in count :
count[word] += 1
else:
count[word] = 1
len(count) #=> 7
解决此问题的最佳方法是使用集合
集合类型。集合
是所有元素都是唯一的集合。因此:
unique = set([ 'one', 'two', 'two'])
len(unique) # is 2
您可以从一开始就使用集合,边使用边添加单词:
unique.add('three')
这将在添加任何副本时丢弃它们。或者,您可以收集列表中的所有元素,并将列表传递给set()
函数,该函数将在此时删除重复项。我上面提供的示例显示了这种模式:
unique = set([ 'one', 'two', 'two'])
unique.add('three')
# unique now contains {'one', 'two', 'three'}
我会用一个集合来做这件事
def stuff(helloString):
hello_set = set(helloString)
return len(hello_set)
您可以使用集合。计数器
helloString = ['hello', 'world', 'world']
from collections import Counter
c = Counter(helloString)
print("There are {} unique words".format(len(c)))
print('They are')
for k, v in c.items():
print(k)
我知道这个问题不是特别要求这个,而是为了维持秩序
helloString = ['hello', 'world', 'world', 'how', 'are', 'you', 'doing', 'today']
from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict):
pass
c = OrderedCounter(helloString)
print("There are {} unique words".format(len(c)))
print('They are')
for k, v in c.items():
print(k)
我可能误解了这个问题,但我相信目标是找到列表中只出现一次的所有元素
from collections import Counter
helloString = ['hello', 'world', 'world', 'how', 'are', 'you', 'doing', 'today']
counter = Counter(helloString)
uniques = [value for value, count in counter.items() if count == 1]
这将给我们6个项目,因为“世界”在我们的列表中出现两次:
>>> uniques
['you', 'are', 'doing', 'how', 'today', 'hello']
计数器是做这件事的有效方法。
此代码类似于计数器
text = ['hello', 'world']
# create empty dictionary
freq_dict = {}
# loop through text and count words
for word in text:
# set the default value to 0
freq_dict.setdefault(word, 0)
# increment the value by 1
freq_dict[word] += 1
for key,value in freq_dict.items():
if value == 1:
print(f'Word "{key}" has single appearance in the list')
有6个独特的词,不是57个独特的词,不是6个和什么不一样的词?你的意思是和我写的答案一样,所以看不到这个页面?谢谢你指出这一点。为了记录在案,我对你的答案投了赞成票……我以为你说的是“而不是使用”。我的错。删除了否决票。我不知道,也许有人认为我抄袭了。我没有。但正如您首先回答的,这些答案基本相同,我不介意撤回我的答案。您的循环版本只是复制了集合的功能,因为集合基本上是一个具有忽略值的字典。@jamylax,是的,但我将其包括在内,以防OP需要更详细的答案(这正是我也包括集合的原因)。我将循环示例移到按钮上。如果只将值始终设置为True
,则没有理由将count
作为字典。把它改成一个set
@jamylak我用了一个dict,因为OP用了一个dict。如果他想试试set版本,那没关系。我改变了我的答案,对OP的原始代码做了最小的更改。现在这是有意义的,因为您使用的是字典的值
text = ['hello', 'world']
# create empty dictionary
freq_dict = {}
# loop through text and count words
for word in text:
# set the default value to 0
freq_dict.setdefault(word, 0)
# increment the value by 1
freq_dict[word] += 1
for key,value in freq_dict.items():
if value == 1:
print(f'Word "{key}" has single appearance in the list')
Word "hello" has single appearance in the list
Word "world" has single appearance in the list
[Program finished]