2

我在一个大列表中有很多对可变长度的列表(5、4、6 对等),我们称之为LIST以下是 big 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] AND SO ON)列表(即 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随着每个列表的变化而变化。但是由于不同的对,我不知道该怎么做。

我该怎么做呢?

4

5 回答 5

3

我会使用两个简单for的循环(它也LIST比 2 长):

x=[]
y=[]
for i in range(len(LIST)):
    for j in LIST[i]:
        x.append(j[0])
        y.append(j[1])
于 2014-11-20T11:09:30.067 回答
2

您应该转置子列表并使用itertool.chain创建一个列表:

from itertools import chain
zipped = [zip(*x) for x in l]
x, y = chain.from_iterable(ele[0] for ele in  zipped),chain.from_iterable(ele[1] for ele in  zipped)
print(list(x),list(y))


[38.621833, 38.572191, 38.580202, 38.610917, 38.631526, 38.28152, 38.246368, 38.246358, 38.281515] [-10.825707, -10.84311, -10.860877, -10.85217, -10.839338, -10.744559, -10.744552, -10.779088, -10.779096]


for ele1,ele2 in zip(x,y):
    print(ele1,ele2)

38.621833 -10.825707
38.572191 -10.84311
38.580202 -10.860877
38.610917 -10.85217
38.631526 -10.839338
38.28152 -10.744559
38.246368 -10.744552
38.246358 -10.779088
38.281515 -10.779096
于 2014-11-20T11:04:13.107 回答
0

map函数与zip*stuple 运算符一起使用。

l = [(38.621833, -10.825707),
 (38.572191, -10.84311),      
 (38.580202, -10.860877),
 (38.610917, -10.85217),
 (38.631526, -10.839338)]
x= map(list, zip(*l))[0]
y = map(list, zip(*l))[1]
print 'x = {},\n y = {}' .format(x,y)

 x = [38.621833, 38.572191, 38.580202, 38.610917, 38.631526],
 y = [-10.825707, -10.84311, -10.860877, -10.85217, -10.839338]

或者,如果您不想将其存储在变量中,则不要在上述解决方案中使用索引,

map(list, zip(*l)) # will give you a nested list
于 2014-11-20T11:01:14.460 回答
0

干得好。tuple按照要求。

my = [(38.621833, -10.825707),(38.572191, -10.84311),(38.580202, -10.860877),(38.610917, -10.85217),(38.631526, -10.839338)]

my1 = [(38.28152, -10.744559),(38.246368, -10.744552),(38.246358, -10.779088),(38.281515, -10.779096)]



l1 = map(tuple,zip(*my))[0]
l2 = map(tuple,zip(*my))[1]
print l1,l2

输出:

(38.621833, 38.572191, 38.580202, 38.610917, 38.631526)(-10.825707, -10.84311, -10.860877, -10.85217, -10.839338)
于 2014-11-20T11:04:00.783 回答
0

您的LIST扩展了 2 个列表。和

for i in range(len(LIST)):

您在循环中运行了 2 次。

如果你想用 for 循环解决你的问题,你需要嵌套它们:

#declare x, y as lists
x = []
y = []
for i_list in LIST:
    #outer for-loop runs 2 times - one for each list appended to LIST.
    #1st run: i_list becomes LIST[0]
    #2nd run: i_list becomes LIST[1]
    for touple in i_list:
        #inner for-loop runs as often as the number of tuple appended to i_list
        #touple becomes the content of i_list[#run]
        x.append(touple[0]) #adds x-value to x
        y.append(touple[1]) #adds y-value to y

如果您更喜欢使用索引,请使用:

for i in range(len(LIST)):
    for j in range(len(LIST[i])):
        x.append(LIST[i][j][0])
        y.append(LIST[i][j][1]])

使用索引来附加 x 或 y 值更容易编写(节省关于列表结构的复杂想法和正确使用索引),并且对于阅读您的代码的外部人员来说更容易理解。

于 2014-11-20T11:23:47.403 回答