从一对列表中获取第一个和第二个“列”

时间:2014-11-20 10:43:27

标签: python list python-2.7 list-manipulation

我在一个大的列表中有很多可变长度列表(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] AND SO ON)

所以最后我将有两个变量(比如x,y),它们将包含第一个和第二个"列的所有值"列表中的所有列表。

我面临的问题是所有这些列表的长度不一样!!

我试过

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会更改每个列表。但由于不同的对,我不知道如何去做。

我该怎么做呢?

5 个答案:

答案 0 :(得分: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])

答案 1 :(得分: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

答案 2 :(得分:0)

mapzip 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

答案 3 :(得分: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)

答案 4 :(得分:0)

您的 LIST 延伸到2个列表中。 随着

for i in range(len(LIST)):

你在循环中运行了两次。

如果你想用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]])

NOT 使用索引来附加x值或y值更容易编写(保存关于List-Structure的复杂想法并正确使用索引),并且对于外部人员来说更容易理解阅读你的代码。