2 回答

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
將測(cè)試與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):
但是,與其測(cè)試所有不同形式的elem
,不如一開始只輸入小寫單詞會(huì)更簡(jiǎn)單self.words
。
并制作self.words
aset
而不是 a list
,然后將自動(dòng)刪除重復(fù)項(xiàng)。

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
如果要保留輸入中的原始大寫/小寫,請(qǐng)檢查以下內(nèi)容:
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']
添加回答
舉報(bào)