简体   繁体   中英

How to append a list to a list in python?

Right now I'm trying to create a small function that takes two parameters, a list and the limit before changing those numbers in said list. The 2D list should only return 1's and 0's. If an element is greater than or equal to the limit it changes the element to a 1 and if its less than the limit it becomes a 0. So far this is what I've come up with:

def tempLocations(heatMat,tempMat):
newMat=[]           
for i in heatMat:
    for j in i: #j goes into the list within the list
        if j >= tempMat: #if the element of j is greater than or equal to the limit of the matrix
            j = 1 #it will turn the element into a 1
            newMat.append(j)
        else:
            if j < tempMat:
                j = 0
                newMat.append(j)          
print newMat


tempLocations([[12,45,33,22,34],[10,25,45,33,60]],30)

This does what I want for the most part, except it instead creates a single list where it places all the 1's and 0's into. I'm trying to get it to keep the 2D list style while still changing the values inside the lists so that what I end up with isn't [0, 1, 1, 0, 1, 0, 0, 1, 1, 1] but rather [[0, 1, 1, 0, 1],[0, 0, 1, 1, 1]]. How would I go about doing this? Any help is appreciated :)

There's a simpler way all together:

data = [[12,45,33,22,34],[10,25,45,33,60]]
mask = [[int(x > 30) for x in sub_list] for sub_list in data]

and if you want it as a function with the threshold as an argument:

def make_mask(thresh, data):
    return [[int(x > thresh) for x in sub_list] for sub_list in data]

make_mask(30, data)

and for purists who don't want to cast the bool result as an int (or who might want different values than 0 and 1), this too is easy to read:

[[1 if x > 30 else 0 for x in sub_list] for sub_list in data]

or

def make_mask(thresh, data, hi=1, lo=0):
    return [[hi if x > thresh else lo for x in sub_list] for sub_list in data]

for example

In [97]: make_mask(30, data, "hot", "cold")
Out[97]: [['cold', 'hot', 'hot', 'cold', 'hot'], ['cold', 'cold', 'hot', 'hot', 'hot']]
list1 = [0,0,0,1,1]
list2 = [1,1,1,0,0]

wrap = []
wrap.append(list1)
wrap.append(list2)

Then wrap == [[0,0,0,1,1],[1,1,1,0,0]]

Something like this? You can then change list1 and list2 as you like, and it will be reflected in wrap .

You're initializing newMat with an empty list then add numbers to it, so in the end newMat will still be a list. A possible solution is to use an intermediate list initialized with an empty list before the inner loop, append elements to it (instead of to newMat ) in the inner loop and after the inner loop append this intermediate list to newMat .

def tempLocations(heatMat,tempMat):
    newMat=[]           
    for i in heatMat:
        newRow=[]
        for j in i:
            if j >= tempMat:
                j = 1
            else:
                j = 0
            newRow.append(j)          
        newMat.append(newRow)
    print newMat

Just a couple of adjustments to your original code - just before the inner loop, create a new list and put all the ones and zeroes in it, then when that list is complete append it to the main list. I also added a return statement to your function to return the new list to the statement that called it:

def tempLocations(heatMat,tempMat):
    newMat = []
    for i in heatMat:
        inner_list = list()
        for j in i: #j goes into the list within the list
            if j >= tempMat: #if the element of j is greater than or equal to the limit of the matrix
                j = 1 #it will turn the element into a 1
            elif j < tempMat:
                j = 0
            #append to the inner list
            inner_list.append(j)
        # append inner_list to the outer list when done
        newMat.append(inner_list)
    #return the new list to the calling statement
    return newMat

new = tempLocations([[12,45,33,22,34],[10,25,45,33,60]],30)
print new

>>> 
[[0, 1, 1, 0, 1], [0, 0, 1, 1, 1]]
>>> 

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