追加到嵌套列表 [英] Appending to a nested list

查看:101
本文介绍了追加到嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用嵌套列表在我创建的字典中查找值.然后,我想将找到的值附加到列表中.我不知道如何编码的问题是如何使值附加在同一嵌套列表结构中?这是我在最后一行将值附加到空列表的代码.

I'm using a nested list to look up values in a dictionary I created. I then want to append the values found to a list. The problem I don't know how to code is how to I keep the values appended within the same nested list structure? Here's the code where the last line I'm appending the values to an empty list.

#Creating a dictionary of FID: LU_Codes from external txt file
import sys, arcpy, string, csv

text_file = open("H:\SWAT\NC\FID_Whole.txt", "r")
Lines = text_file.readlines()
text_file.close()

FID_LU = map(string.split, Lines)
#print FID_LU
FID_GC_dict = dict(FID_LU)

Neighbors_file = open("H:\SWAT\NC\Sh_Neighbors2.txt","r")
Entries = Neighbors_file.readlines()
Neighbors_file.close()

Neighbors_List = map(string.split, Entries)

print Neighbors_List

#FID = [x[0] for x in Neighbors_List]
#print FID

gridList = []
for list in Neighbors_List:
    for item in list:
       #print FID_GC_dict[item]
       gridList.append(int(FID_GC_dict[item]))


 print gridList

以下是邻居列表"的输出(正确):

Here's the output for Neighbors List (correct):

[['0', '1', '11', '12', '13'], ['1', '0', '2', '12', '13', '14'], ['2', '1', '3', '13', '14', '15'], ['3', '2', '4', '14', '15', '16'], ['4', '3', '5', '15', '16', '17'], ['5', '4', '6', '16', '17', '18'], ['6', '5', '7', '17', '18', '19'], ['7', '6', '8', '18', '19', '20'], ['8', '7', '9', '19', '20', '21'], ['9', '8', '20', '21', '22'], ['10', '11']]

这是gridList的输出(不正确):

Here's the output for gridList (incorrect):

[3, 3, 4, 4, 4, 3, 3, 3, 4, 4, 4, 3, 3, 3, 4, 4, 2, 3, 3, 3, 4, 2, 2, 3, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4]

我希望gridList看起来像是:

What I would like gridList to look like is:

[[3, 3, 4, 4, 4], [3, 3, 3, 4, 4, 4], [3, 3, 3, 4, 4, 2], [3, 3, 3, 4, 2, 2], [3, 3, 3, 2, 2, 2], [3, 3, 3, 2, 2, 3], [3, 3, 3, 2, 3, 3], [3, 3, 3, 3, 3, 3], [3, 3, 3, 3, 3, 3], [3, 3, 3, 3, 3], [3, 4]]

任何帮助将不胜感激.我是python的新手,阅读这​​篇文章会有所帮助,但我正为此而苦苦挣扎.

Any help would be appreciated. I'm new to python...reading the posts helps, but I'm struggling with this one.

谢谢!

推荐答案

创建一个临时列表row.将项目从内部循环附加到row,然后在外部循环中,将行附加到gridList:

Make a temporary list, row. Append the items from the inner loop to row, and then in the outer loop, append the row to gridList:

gridList = []
for nlist in Neighbors_List:
    row = []
    for item in nlist:
       row.append(int(FID_GC_dict[item]))
    gridList.append(row)

请注意,您还可以在此处使用列表理解:

Note that you could also use a list comprehension here:

gridList = [[int(FID_GC_dict[item]) for item in nlist] 
            for nlist in Neighbors_List]

PS.最好不要命名变量list,因为它会掩盖同名的内置类型.

PS. It is best not to name a variable list, since it shadows the builtin type of the same name.

这篇关于追加到嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆