2 回答

TA贡献1842条经验 获得超13个赞
将测试与and
if elem not in uniqueList and capital not in uniqueList and title not in uniqueList and lower not in uniqueList:
您还可以使用集合操作:
if not set((elem, capital, title, lower)).isdisjoint(uniqueList):
但是,与其测试所有不同形式的elem
,不如一开始只输入小写单词会更简单self.words
。
并制作self.words
aset
而不是 a list
,然后将自动删除重复项。

TA贡献1936条经验 获得超7个赞
如果要保留输入中的原始大写/小写,请检查以下内容:
content = "Hello john hello hELLo my naMe Is JoHN"
words = content.split()
dictionary = {}
for word in words:
if word.lower() not in dictionary:
dictionary[word.lower()] = [word]
else:
dictionary[word.lower()].append(word)
print(dictionary)
# here we have dictionary: {'hello': ['Hello', 'hello', 'hELLo'], 'john': ['john', 'JoHN'], 'my': ['my'], 'name': ['naMe'], 'is': ['Is']}
# we want the value of the keys that their list contains a single element
uniqs = []
for key, value in dictionary.items():
if len(value) == 1:
uniqs.extend(value)
print(uniqs)
# will print ['my', 'naMe', 'Is']
添加回答
举报