简体   繁体   中英

How to make each element in a list another list in python

I have a list inside a loop for eg

A=[25,45,34,....87]

in the next iteration A should be

A=[[25,32],[45,13],[34,65],....[87,54]]

in the next iteration A should be

A=[[25,32,44],[45,13,67],[34,65,89],....[87,54,42]]

and so on.How can i do that?is it possible?The code i am working on is

    s=0
    e=25
    for i in range(0,4800):
        if not m_list_l:
            m_list_l.append(max(gray_sum[s:e]))
        m_list_l[i].append(max(gray_sum[s:e]))
        s+=25
        e+=25

But this give me Error as

m_list_l[i].append(max(gray_sum[s:e]))
AttributeError: 'int' object has no attribute 'append'

The first element you insert should be a list, not an int. Change m_list_l.append(max(gray_sum[s:e])) to m_list_l.append([max(gray_sum[s:e])]) to fix this.

Say there are two lists as

A = [i for i in range(10,100,10)]
A
[10, 20, 30, 40, 50, 60, 70, 80, 90]

B = [i for i in range(20,100,10)]
B
[20, 30, 40, 50, 60, 70, 80, 90, 100]

The combined list would be

L = [[i,j] for i,j in zip(A,B)]
L
 [[10, 20],
 [20, 30],
 [30, 40],
 [40, 50],
 [50, 60],
 [60, 70],
 [70, 80],
 [80, 90],
 [90, 100]]

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