我正在写我的第一个程序。
我需要知道如何根据字符串创建一组唯一的单词。
我想知道如何做到这一点,以便能够知道集合中的元素数量(或字符串中唯一单词的数量)
我需要发生这种情况:
´´´´´´´´´´´
输入:
string =(“一一二三三四你好你好”)
´´´´´´´´´´´
输出:
(“一个”,“两个”,“三个”,“四个”,“你好”)
´´´´´´´´´´´
答案 0 :(得分:1)
string具有方法'split',该方法返回按给定参数分割的单词列表。
string=("one one two three three four hello hello")
set_of_words = set(string.split(' ')
输出为:
{'three', 'one', 'hello', 'two', 'four'}
答案 1 :(得分:0)
如果您需要保留单词OrderedDict的顺序,那就去吧:
import collections # OrderedDict is one Python's high-performance containers
string=("one one two three three four hello hello")
unique_word_dict = collections.OrderedDict() # creates and empty ordered dictionary
# The split method of strings breaks the string into parts using the specified separator.
# In this case the separator is a space character so each element in the list is a word.
word_list = string.split(' ')
# This loops though each element of the list and makes the word a key in the OrderedDict.
# The .get(word, 0) method creates a new key in the dictionary if it does not already
# exist and initializes it to 0.
# If the key already exists, .get(word, 0) returns the current value.
for word in word_list:
unique_word_dict[word] = unique_word_dict.get(word, 0) + 1
print('key: %s, value: %i' % (word, unique_word_dict.get(word)))
unique_words = tuple(unique_word_dict.keys())
print(unique_word_dict)
print(unique_words)
print(len(unique_words))
输出:
key: one, value: 1
key: one, value: 2
key: two, value: 1
key: three, value: 1
key: three, value: 2
key: four, value: 1
key: hello, value: 1
key: hello, value: 2
OrderedDict([('one', 2), ('two', 1), ('three', 2), ('four', 1), ('hello', 2)])
('one', 'two', 'three', 'four', 'hello')
5