[英]Enumerate unique strings in list
免責聲明:我不是一位經驗豐富的Python用戶。
我遇到了一個任務,現在我正試圖找出用Python做的最優雅的方法。
這是任務本身:給定一個字符串list
返回一個int
列表(每個int
從0到N - 1,其中N是列表中唯一字符串的數量),其中每個int對應於初始列表中的某個字符串。 相同的字符串應映射到相同的數字,不同的字符串 - 不同的數字。
我提出的第一件事似乎“有點”過於復雜:
a = ["a","b","a","c","b","a"]
map(lambda x: dict(map(lambda x: reversed(x), enumerate(set(a))))[x], a)
上面代碼的結果:
[0, 2, 0, 1, 2, 0]
您可以使用dict和list comprehensions:
>>> a = ["a","b","a","c","b","a"]
>>> d = {x:i for i, x in enumerate(set(a))}
>>> [d[item] for item in a]
[0, 2, 0, 1, 2, 0]
為了保持秩序:
>>> seen = set()
>>> d = { x:i for i, x in enumerate(y for y in a
if y not in seen and not seen.add(y))}
>>> [d[item] for item in a]
[0, 1, 0, 2, 1, 0]
上述字典理解相當於:
>>> seen = set()
>>> lis = []
for item in a:
if item not in seen:
seen.add(item)
lis.append(item)
...
>>> lis
['a', 'b', 'c']
>>> d = {x:i for i,x in enumerate(lis)}
我認為如果你想保留接近字符的順序,你使用set的方法可能會導致錯誤。 實際上你可以在你的例子中看到它 - 'b'
得到索引2
而不是1
。 如果您想保留訂單,可以使用OrderedDict :
>>> a = ["a","b","a","c","b","a"]
>>> d = {x:i for i, x in enumerate(OrderedDict(izip(a, a)).values())}
>>> [d[x] for x in a]
[0, 1, 0, 2, 1, 0]
強調可讀性,而不是速度:我會使用列表index
方法和列表理解:
>>> a = ["a","b","a","c","b","a"]
>>> b = list(set(a))
>>> c = [b.index(x) for x in a]
>>> c
[0, 2, 0, 1, 2, 0]
首先從列表中獲取唯一的字符串並枚舉它,因此每個字符串都有一個數字(從0到N-1)。 然后為每個字符串獲取此值,並將其放入列表中。 這是如何完成的,在一行中:
a = ["a","b","a","c","b","a"]
[{s:i for i, s in enumerate(set(a))}[s] for s in a]
您也可以使用defaultdict和count迭代器來完成它。
>>> from collections import defaultdict
>>> from itertools import count
>>> a = ["a","b","a","c","b","a"]
>>> x = defaultdict(count().next)
>>> [x[i] for i in a]
[0, 1, 0, 2, 1, 0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.