我正在尝试学习 python。以下是练习的相关部分:
For each word, check to see if the word is already in a list. If the word is not in the list, add it to the list.
这就是我所拥有的。
fhand = open('romeo.txt')
output = []
for line in fhand:
words = line.split()
for word in words:
if word is not output:
output.append(word)
print sorted(output)
这是我得到的。
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'and', 'and',
'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'is', 'is',
'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'sun',
'the', 'the', 'the', 'through', 'what', 'window', 'with', 'yonder']
注意重复(and、is、sun 等)。
如何只获取唯一值?
最佳答案
要消除列表中的重复项,您可以维护一个辅助列表并进行检查。
myList = ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'and', 'and',
'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'is', 'is', 'kill', 'light',
'moon', 'pale', 'sick', 'soft', 'sun', 'sun', 'the', 'the', 'the',
'through', 'what', 'window', 'with', 'yonder']
auxiliaryList = []
for word in myList:
if word not in auxiliaryList:
auxiliaryList.append(word)
输出:
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east',
'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick',
'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']
这很容易理解,代码是不言自明的。然而,代码的简单性是以牺牲代码效率为代价的,因为对不断增长的列表进行线性扫描会使线性算法降级为二次算法。
如果顺序不重要,可以使用set()
A set object is an unordered collection of distinct hashable objects.
Hashability使对象可用作字典键和集合成员,因为这些数据结构在内部使用哈希值。
由于在哈希表中进行成员资格检查的平均情况是 O(1),因此使用集合更有效。
auxiliaryList = list(set(myList))
输出:
['and', 'envious', 'already', 'fair', 'is', 'through', 'pale', 'yonder',
'what', 'sun', 'Who', 'But', 'moon', 'window', 'sick', 'east', 'breaks',
'grief', 'with', 'light', 'It', 'Arise', 'kill', 'the', 'soft', 'Juliet']
关于python - 仅将唯一值添加到python中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42334197/