Merging Two List Lists - Python

This is a great tutorial, but it doesn't answer all I need: Combining two sorted lists in Python

I have two Python lists, each of which is a list of datetime, value pairs of data:

list_a = [['1241000884000', 3], ['1241004212000', 4], ['1241006473000', 11]]

and

list_x = [['1241000884000', 16], ['1241000992000', 16], ['1241001121000', 17], ['1241001545000', 19], ['1241004212000', 20], ['1241006473000', 22]]
  • In fact, there are many list_a lists with different keys / values.
  • All list_a datetimes are in list_x.
  • I want to create a list_c list corresponding to each list_a that has each datetime from list_x and value_a / value_x.

Bonus:

In my real program, list_a is actually a list in a dictionary. The answer to the dictionary level will be as follows:

dict = {object_a: [['1241000884000', 3], ['1241004212000', 4], ['1241006473000', 11]], object_b: [['1241004212000', 2]]}

I can understand that this is part.

+1
source share
4

, , . . , , . , .

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)
+4

" list_c, _, datetime 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])

- .

merged= list( merge_lists( someDict['object_a'], someDict['object_b'] )

, .

+3

:

reduce(lambda l1,l2: l1 + l2, list)
+2

:

list_a.extend(list_b)
0

Source: https://habr.com/ru/post/1721871/


All Articles