假設我有2所列出如何刪除此列表中的一個項目?
list1 = [['abc', '123'], ['def', '456']]
list2 = ['abc','123','def','456','ghi','789']
如何從列表1中移除1元通過鍵入隻字母或數字
例如,如果我輸入「ABC」或「123」我希望它顯示
[['def', '456'], ['ghi', '789']]
在list2中我可以使它通過下面的代碼
contact = input("Type the name/number of the contact you want to remove")
if contact in list2:
pos = list2.index(contato)
if pos % 2 == 0:
list2.pop(pos)
list2.pop(pos)
if pos % 2 != 0:
list2.pop(pos-1)
list2.pop(pos-1)
刪除對
但是我不能夠做,在列表1既是號碼和字母在一起,使我想唯一的辦法是轉移的新列表2至LIST1,但我不知道我該怎麼做
我知道我寫的是真的很混亂,但基本上我想改造
this: ['def','456','ghi','789'] into this: [['def', '456'], ['ghi', '789']]
用戶似乎是python的新手。也許你應該教他們關於元組。 – naomik
啊!打我吧! – anshanno
看起來他們想要'(姓名,電話)' – naomik