Get Indexes For First Occurrence Of Each Element In A List?
Solution 1:
You can build a dictionary that stores the index of the first occurrence of each word. That way, you only look through your big list once, and the dictionary lookups are much faster, since the dictionary contains each value only once, and is accessed in O(log(n)).
l = ["foo", "bar", "baz", "bar", "foo"]
v = {}
for i, x in enumerate(l):
if x not in v:
v[x] = i
# v is now {'bar': 1, 'baz': 2, 'foo': 0}
Also, if you want to output a 90k-long list containing the index of the first occurrence for each element in the original list, you can get it that way:
output = [v[x] for x in l]
# output is now [0, 1, 2, 1, 0]
Solution 2:
I think you just want to use enumerate
(unless you want the first occurrence of each item in the list):
strings = ["foo", "bar", "baz", "bar", "foo"]
forindex, value in enumerate(strings):
printindex, value
outputs
0 foo
1 bar
2 baz
3 bar
4 foo
If you wanted, for example, 1 bar
instead of 3 bar
, you can maintain a dictionary of found strings:
forindex, value in enumerate(strings):
if value not in d:
d[value] = indexfor value in strings:
print value, d[value]
Solution 3:
Your question is very ambiguous but as i understood it you have many duplicate values and you just want to get the index of the first appearance for each. I would leverage sets like this:
my_list = ["foo", "bar", "baz", "bar", "foo"]
my_list_unique = set(my_list)
indexes = [(x, my_list.index(x)) forxin my_list_unique]
print(indexes) # prints -> [('foo', 0), ('bar', 1), ('baz', 2)]
Note that the creation of a set in line 3 removes the duplicates so every entry in my_list_unique
only exists once. This saves time when looking for the indexes. As far as the results go, it is a list of tuples where each tuple contains the string and the index in which it is first found in my_list
Post a Comment for "Get Indexes For First Occurrence Of Each Element In A List?"