我在一个大列表中有许多对可变长度的列表(5、4、6对等),我们称之为LIST
。以下是大LIST内部众多示例中的两个列表:
[(38.621833, -10.825707),
(38.572191, -10.84311), -----> LIST[0]
(38.580202, -10.860877),
(38.610917, -10.85217),
(38.631526, -10.839338)]
[(38.28152, -10.744559),
(38.246368, -10.744552), -----> LIST[1]
(38.246358, -10.779088),
(38.281515, -10.779096)]
我需要创建两个单独的变量,比如说,其中一个变量将全部具有first "column"
(即LIST [0] [0] [0],LIST [0] [1] [0]等)对列表(即38.621833、38.572191等)和第二个变量将全部具有second "column"
(即LIST [0] [0] [1],LIST [0] [1] [1]等)列表对。
所以最后我将拥有两个变量(例如x,y),它们将包含LIST中所有列表的第一个和第二个“列”的所有值。
我面临的问题是所有这些列表的长度都不相同!
我试过了
x = []
y = []
for i in range(len(LIST)):
x.append(LIST[i][0][0]) #append all the values of the first numbers
y.append(LIST[i][1][1]) #append all the values of the second numbers
我的期望:
x = (38.621833,38.572191,38.580202,38.610917,38.631526,38.28152,38.246368,38.246358,38.281515)
y = (-10.825707,-10.84311,-10.860877,-10.85217,-10.839338,-10.744559,-10.744552,-10.779088,-10.779096)
但是由于变量对的存在,我的循环在两者之间突然停止了。
我在这里知道I need to also change the LIST[i][j][0]
,并且j
随每个列表而变化。但是由于配对不同,我不知道该怎么做。
我该怎么做呢?
python大神给出的解决方案
我将使用两个简单的for
循环(对于LIST
大于2也是通用的):
x=[]
y=[]
for i in range(len(LIST)):
for j in LIST[i]:
x.append(j[0])
y.append(j[1])