简体   繁体   English

如何使用 for 循环创建嵌套列表列表

[英]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.我通过读取 tsv 文件创建了上述数据。 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我正在尝试制作一组 3 个列表和一个包含所有列表组的完整列表

The line of code final_list.append(in_data.copy()) contains the a single group of lists(contains 3 lists)代码行final_list.append(in_data.copy())包含一组列表(包含 3 个列表)

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 j in range(len(item[field])):之后列出final_list.append(in_data.copy())给出的前三个列表,我想将它保存在final_list_2.append(final_list)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.逐行处理文件时,可以将每个项目添加到current_group列表中。 When you have 3 elements in current_group , append that to the main list and create a new empty current_group list.当您在current_group中有3元素时,append 到主列表并创建一个新的空current_group列表。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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