This is a great primer but doesn't answer what I need: Combining two sorted lists in Python
这是一个很好的入门,但没有回答我的需要:在Python中组合两个排序列表
I have two Python lists, each is a list of datetime,value pairs:
我有两个Python列表,每个列表都包含日期时间,值对:
list_a = [['1241000884000', 3], ['1241004212000', 4], ['1241006473000', 11]]
And:
list_x = [['1241000884000', 16], ['1241000992000', 16], ['1241001121000', 17], ['1241001545000', 19], ['1241004212000', 20], ['1241006473000', 22]]
- There are actually numerous list_a lists with different key/values.
- All list_a datetimes are in list_x.
- I want to make a list, list_c, corresponding to each list_a which has each datetime from list_x and value_a/value_x.
实际上有许多list_a列表具有不同的键/值。
所有list_a日期时间都在list_x中。
我想创建一个列表list_c,对应于每个list_a,其中包含list_x和value_a / value_x中的每个日期时间。
Bonus:
In my real program, list_a is actually a list within a dictionary like so. Taking the answer to the dictionary level would be:
在我的真实程序中,list_a实际上是字典中的列表。对词典级别的回答是:
dict = {object_a: [['1241000884000', 3], ['1241004212000', 4], ['1241006473000', 11]], object_b: [['1241004212000', 2]]}
I can figure that part out though.
我可以想出那个部分。
4 个解决方案
#1
Here's some code that does what you asked for. You can turn your list of pairs into a dictionary straightforwardly. Then keys that are shared can be found by intersecting the sets of keys. Finally, constructing the result dictionary is easy given the set of shared keys.
这里有一些代码可以满足您的要求。您可以直接将对子列表转换为字典。然后,通过交叉键组可以找到共享的键。最后,给定共享密钥集,构造结果字典很容易。
dict_a = dict(list_a)
dict_x = dict(list_x)
shared_keys = set(dict_a).intersection(set(dict_x))
result = dict((k, (dict_a[k], dict_x[k])) for k in shared_keys)
#2
"I want to make a list, list_c, corresponding to each list_a which has each datetime from list_x and value_a/value_x."
“我想创建一个列表list_c,对应于每个list_a,其中包含list_x和value_a / value_x中的每个日期时间。”
def merge_lists( list_a, list_x ):
dict_x= dict(list_x)
for k,v in list_a:
if k in dict_x:
yield k, (v, dict_x[k])
Something like that may work also.
这样的东西也可以起作用。
merged= list( merge_lists( someDict['object_a'], someDict['object_b'] )
This may be slightly quicker because it only makes one dictionary for lookups, and leaves the other list alone.
这可能会稍快一点,因为它只为查找创建一个字典,而另一个列表单独留下。
#3
Nothing beats a nice functional one-liner:
没有什么比一个很好的功能性单线:
reduce(lambda l1,l2: l1 + l2, list)
#4
Could try extend:
可以尝试扩展:
list_a.extend(list_b)
#1
Here's some code that does what you asked for. You can turn your list of pairs into a dictionary straightforwardly. Then keys that are shared can be found by intersecting the sets of keys. Finally, constructing the result dictionary is easy given the set of shared keys.
这里有一些代码可以满足您的要求。您可以直接将对子列表转换为字典。然后,通过交叉键组可以找到共享的键。最后,给定共享密钥集,构造结果字典很容易。
dict_a = dict(list_a)
dict_x = dict(list_x)
shared_keys = set(dict_a).intersection(set(dict_x))
result = dict((k, (dict_a[k], dict_x[k])) for k in shared_keys)
#2
"I want to make a list, list_c, corresponding to each list_a which has each datetime from list_x and value_a/value_x."
“我想创建一个列表list_c,对应于每个list_a,其中包含list_x和value_a / value_x中的每个日期时间。”
def merge_lists( list_a, list_x ):
dict_x= dict(list_x)
for k,v in list_a:
if k in dict_x:
yield k, (v, dict_x[k])
Something like that may work also.
这样的东西也可以起作用。
merged= list( merge_lists( someDict['object_a'], someDict['object_b'] )
This may be slightly quicker because it only makes one dictionary for lookups, and leaves the other list alone.
这可能会稍快一点,因为它只为查找创建一个字典,而另一个列表单独留下。
#3
Nothing beats a nice functional one-liner:
没有什么比一个很好的功能性单线:
reduce(lambda l1,l2: l1 + l2, list)
#4
Could try extend:
可以尝试扩展:
list_a.extend(list_b)