[英]How to remove from a list in python, all but one of the elements that have equal length?
我正在輸入一個列表,它必須只包含一個特定長度的元素。
我試過:
lst = list(input("list: "))
for i, j in lst:
if len(j) == len(i):
lst.remove(j)
print(lst)
它顯示了這個:
for i, j in lst:
ValueError: not enough values to unpack (expected 2, got 1)
而且我不知道這個錯誤是什么意思,我哪里出錯了? 你能建議點什么嗎?
編輯:您不能使用len()
。 你怎么知道是 genip 還是梨? 兩者都包含 5 個字符。
更簡單的方法:
fruits = ["apples", "bananas", 'genip', "pears", "mango", 'coconut']
lst = input()
res = [i for i in fruits if i not in lst]
print(f'List after removing duplicate elements: {res}')
結果類型:genip # 刪除它。
刪除重復元素后的列表:['apples', 'bananas', 'pears', 'mango', 'coconut']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.