简体   繁体   中英

Python - merging two 2D lists, overwriting values of one

I have two, two dimensional lists

list 1 = [[None, None], [None, None], [None, None]]
list 2 = [[1, 2], [3, 4]]

The desired output is

list 3 = [[1, 2], [3, 4], [None, None]]

In case there is a much simpler solution, I'm doing this because I don't always have data for the third element in the list. I need the list to always have three first level items. I'm trying to stop my script throwing out of range error.

Some additional context to hopefully address the below questions.

This comes about in my app when database query returns result that are put into a list, eg 'biggest 10 problems'. Sometimes things aren't so bad, and there are only 9 problems.

When another part of the application looks for that 10th problem (a Flask website), it throws the error. Try/Except would indeed work, but would be pretty heavy.

So I need a solution where I can create a list of placeholder values, and overwrite them with the data if available.

The accepted solution works great with just 1 line of code.

I think this is what you meant

list_3 = [ list_2[i] if i < len(list_2) else e for i,e in enumerate(list_1) ]

here is the equivalent without list comprehension

list_3 = list()
for i,e in enumerate(list_1):
    if i < len(list_2):
        list_3.append(list_2[i])
    else:
        list_3.append(e)

I am not sure I fully understand your question, but I think you may want this:

list1 = [[None, None], [None, None], [None, None]]
list2 = [[1, 2], [3, 4]]
list3 = list2 + list1[len(list2):]

In the third line, all elements in list2 will replace len(list2) elements in list1 but leave the remaining elements untouched. If this is what you want, you should make sure the length of list1 not less than list2, otherwise, it will raise IndexError .

You don't need to create a new list. Just use the following for accessing the elements of list2

list2[10] if 10 < len(list2) else [None, None]

I have used the index as 10 below but possibly in your case it will be a variable most likely:

list2[i] if i < len(list2) else [None, None]

If you want to do something with two lists, consider using itertools.izip_longest to combine them.

In [1]: list1 = [[None, None], [None, None], [None, None]]

In [2]: list2 = [[1, 2], [3, 4]]

In [3]: import itertools

In [8]: [a for a, b in itertools.izip_longest(list2, list1, fillvalue=[None, None])]
Out[8]: [[1, 2], [3, 4], [None, None]]

Note that in Python 3 the name is itertools.zip_longest .

You can do that this way:

list_3 = list_2 + list_1[len(list_2):]

But I am not sure what is needed to be done.

You could use a generator which first produce the elements from list_2 and then produces None values. Then, simply get the first 3 elements from that generator.

from itertools import chain, islice, repeat
list_2 = [[1, 2], [3, 4]]
list_3 = list(
    islice(
        chain(
            list_2,
            repeat(None)
        ),
        3))

But you may be facing a typical missing data problem. If you are going to work with a lot of data, i suggest you to use Pandas. Here is a related text from Pandas documentation: http://pandas.pydata.org/pandas-docs/stable/missing_data.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM