biology = ["Sarah", "Ahmed", "Fred", "Gillian", "Shradah", "Max", "Max", "Sara", "Max", "Esther"]computerScience = ["Sarah", "John", "Fred", "Gillian", "Jermaine", "Max", "Sara", "Juan", "Esther"]english = ["Nico", "Sharjeel", "Isabella", "Taylor", "Ali", "Ali", "Jean-Baptiste", "Jean-Baptiste", "Jean-Baptiste", "William"]setThing=set()for word in biology: setThing.add(word)listThing=[]for word in setThing: listThing.append(word)print(listThing)在此,我試圖將名為生物學(xué)的列表放入一個(gè)集合中,以便消除重復(fù)。我試圖將其轉(zhuǎn)回一個(gè)與列表具有相同名稱順序的列表:生物學(xué)。但當(dāng)我把它變成一套時(shí),名字的順序就完全改變了。有什么方法可以使用某種函數(shù)來訂購新集或新列表,以便名稱與原始“生物學(xué)”列表的順序相同?
1 回答

楊魅力
TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊
使用
biology = list(dict.fromkeys(biology))
這將按順序刪除重復(fù)項(xiàng)并返回列表中的唯一元素
添加回答
舉報(bào)
0/150
提交
取消