我對python(2.7)有點新,而且我很難做到這一點。如何合併兩個字符串列表中的重複項?
我有以下列表:
animal = ['cat', 'cat', 'dog', 'dog', 'dog', 'horse']
names = ['cat_01', 'cat_02', 'dog_01', 'dog_02', 'dog_03', 'horse_01']
我想有以下(元組的它可能是一個列表或者一個字典)
new = {"cat":('cat_01','cat_02'), "dog":('dog_01','dog_02', 'dog_03'), "horse":('horse_01')}
如何最好地做到這一點?
這會改變'in'運算符的'str.startwith'。因爲我正在處理不以我需要的字符串開頭的文件路徑。不管怎樣,謝謝你! –
如果是這樣,我已經改變了這個片段。它會工作 – RomanPerekhrest