I wonder whether there's a quicker and less time consuming way to iterate over a list of tuples, finding the right match. What I do is:
我想知道是否有一种更快,更省时的方法来迭代元组列表,找到正确的匹配。我所做的是:
# this is a very long list.
my_list = [ (old1, new1), (old2, new2), (old3, new3), ... (oldN, newN)]
# go through entire list and look for match
for j in my_list:
if j[0] == VALUE:
PAIR_FOUND = True
MATCHING_VALUE = j[1]
break
this code can take quite some time to execute, depending on the number of items in the list. I'm sure there's a better way of doing this.
此代码可能需要相当长的时间才能执行,具体取决于列表中的项目数。我确信有更好的方法可以做到这一点。
5 个解决方案
#1
17
Assuming a bit more memory usage is not a problem and if the first item of your tuple is hashable, you can create a dict out of your list of tuples and then looking up the value is as simple as looking up a key from the dict
. Something like:
假设更多的内存使用不是问题,如果你的元组的第一项是可清除的,你可以从元组列表中创建一个字典,然后查找值就像从字典中查找键一样简单。就像是:
dct = dict(tuples)
val = dct.get(key) # None if item not found else the corresponding value
EDIT: To create a reverse mapping, use something like:
编辑:要创建反向映射,请使用以下内容:
revDct = dict((val, key) for (key, val) in tuples)
#2
33
I think that you can use
我认为你可以使用
for j,k in my_list:
[ ... stuff ... ]
#3
2
The code can be cleaned up, but if you are using a list to store your tuples, any such lookup will be O(N).
可以清除代码,但是如果使用列表来存储元组,则任何此类查找都将为O(N)。
If lookup speed is important, you should use a dict
to store your tuples. The key should be the 0th element of your tuples, since that's what you're searching on. You can easily create a dict from your list:
如果查找速度很重要,则应使用dict存储元组。键应该是元组的第0个元素,因为那是你正在搜索的内容。您可以从列表中轻松创建一个dict:
my_dict = dict(my_list)
Then, (VALUE, my_dict[VALUE])
will give you your matching tuple (assuming VALUE
exists).
然后,(VALUE,my_dict [VALUE])将为您提供匹配的元组(假设存在VALUE)。
#4
2
The question is dead but still knowing one more way doesn't hurt:
这个问题已经死了但仍然知道另一种方式并没有伤害:
my_list = [ (old1, new1), (old2, new2), (old3, new3), ... (oldN, newN)]
for first,*args in my_list:
if first == Value:
PAIR_FOUND = True
MATCHING_VALUE = args
break
#5
1
I wonder whether the below method is what you want.
我想知道下面的方法是否是你想要的。
You can use defaultdict
.
您可以使用defaultdict。
>>> from collections import defaultdict
>>> s = [('red',1), ('blue',2), ('red',3), ('blue',4), ('red',1), ('blue',4)]
>>> d = defaultdict(list)
>>> for k, v in s:
d[k].append(v)
>>> sorted(d.items())
[('blue', [2, 4, 4]), ('red', [1, 3, 1])]
#1
17
Assuming a bit more memory usage is not a problem and if the first item of your tuple is hashable, you can create a dict out of your list of tuples and then looking up the value is as simple as looking up a key from the dict
. Something like:
假设更多的内存使用不是问题,如果你的元组的第一项是可清除的,你可以从元组列表中创建一个字典,然后查找值就像从字典中查找键一样简单。就像是:
dct = dict(tuples)
val = dct.get(key) # None if item not found else the corresponding value
EDIT: To create a reverse mapping, use something like:
编辑:要创建反向映射,请使用以下内容:
revDct = dict((val, key) for (key, val) in tuples)
#2
33
I think that you can use
我认为你可以使用
for j,k in my_list:
[ ... stuff ... ]
#3
2
The code can be cleaned up, but if you are using a list to store your tuples, any such lookup will be O(N).
可以清除代码,但是如果使用列表来存储元组,则任何此类查找都将为O(N)。
If lookup speed is important, you should use a dict
to store your tuples. The key should be the 0th element of your tuples, since that's what you're searching on. You can easily create a dict from your list:
如果查找速度很重要,则应使用dict存储元组。键应该是元组的第0个元素,因为那是你正在搜索的内容。您可以从列表中轻松创建一个dict:
my_dict = dict(my_list)
Then, (VALUE, my_dict[VALUE])
will give you your matching tuple (assuming VALUE
exists).
然后,(VALUE,my_dict [VALUE])将为您提供匹配的元组(假设存在VALUE)。
#4
2
The question is dead but still knowing one more way doesn't hurt:
这个问题已经死了但仍然知道另一种方式并没有伤害:
my_list = [ (old1, new1), (old2, new2), (old3, new3), ... (oldN, newN)]
for first,*args in my_list:
if first == Value:
PAIR_FOUND = True
MATCHING_VALUE = args
break
#5
1
I wonder whether the below method is what you want.
我想知道下面的方法是否是你想要的。
You can use defaultdict
.
您可以使用defaultdict。
>>> from collections import defaultdict
>>> s = [('red',1), ('blue',2), ('red',3), ('blue',4), ('red',1), ('blue',4)]
>>> d = defaultdict(list)
>>> for k, v in s:
d[k].append(v)
>>> sorted(d.items())
[('blue', [2, 4, 4]), ('red', [1, 3, 1])]