简体   繁体   中英

How to create a list of nested lists using for-loop

I have created a data that contains nested lists

[[[0.0, 0.0, 1.0, 1.0, 1.0],
 [0.19470745, -0.3966015, 0.65908825, 0.5956116, 0.39256063],
 [0.039188415, 0.17440148, 0.7077338, 0.9051374, 0.6405964],
 [-0.30367687, -0.32211977, 0.75126046, 1.0, 0.75126046],

 [0.0, 0.0, 1.0, 1.0, 1.0],
 [-0.15551904, 0.57100296, 1.0738074, 1.5196773, 1.6318406],
 [-0.49838433, 0.07448171, 1.139848, 1.6789465, 1.9137439]]]

I created above data by reading a tsv file. Performed some operations on it and then trying to create these lists mentioned above

if __name__ == '__main__':

    in_data = []
    final_list=[]
    final_list_2=[]
    with open(infile, "r+b") as tsv_in_file:
        reader = csv.DictReader(codecs.iterdecode(tsv_in_file,'utf-8'), delimiter='\t', fieldnames = FIELDNAMES)
        for item in reader:
            for field in ['boxes']:
                item[field] = np.frombuffer(base64.b64decode(item[field]), dtype=np.float32).reshape((int(item['num_boxes']),-1))
                for i in range(len(item[field])):
                    for j in range(len(item[field])):
                      in_data = [0]*5
                      in_data[0]=(((item['boxes'][j][0])-(item['boxes'][i][0]))/(item['boxes'][0][2]))
                      in_data[1] = (((item['boxes'][j][1])-(item['boxes'][i][1]))/(item['boxes'][0][3]))
                      in_data[2] = ((item['boxes'][j][2])/(item['boxes'][i][2]))
                      in_data[3] = ((item['boxes'][j][3])/(item['boxes'][i][3]))
                      in_data[4]=(((item['boxes'][j][2])*(item['boxes'][j][3]))/((item['boxes'][i][2])*(item['boxes'][i][3])))
                      final_list.append(in_data.copy())
                final_list_2.append(final_list)
            break
    print (final_list_2)

I am trying make group of 3 lists each and one whole lists containing all groups of lists

The line of code final_list.append(in_data.copy()) contains the a single group of lists(contains 3 lists)

I am trying to make list of first three lists that is given by final_list.append(in_data.copy()) after completing for j in range(len(item[field])): and I want to save it under final_list_2.append(final_list)

For this I used a loop

for i in range(len(item[field])):

But I failed to do so. You can see in the data above that [[[ are present at the start and at the end. The group is not created. Can anyone help me regarding this

When you process your file line-by-line, you can add each item to a current_group list. When you have 3 elements in current_group , append that to the main list and create a new empty current_group list.

main_list = []
current_group = []
for item in reader:
    current_item = []

    # Do whatever processing you need to fill up current_item
    # This will be a list containing N elements
    # ...

    current_group.append(current_item) # Add current item to current group

    if len(current_group) == 3:
        main_list.append(current_group) # Add current group to main list
        current_group = [] # Create a new group

# Check if current_group is empty
# If it contains stuff, your last group didn't have three items so it didn't get appended to main_list
if current_group:
    main_list.append(current_group)

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