【问题标题】:Obtaining the first and second "column's" from a pair of lists从一对列表中获取第一个和第二个“列”
【发布时间】:2015-01-18 04:39:34
【问题描述】:

我有很多可变长度列表对(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] AND SO ON) 的所有列表对。

所以最后我将有两个变量(比如 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 随每个列表而变化。但是因为不同的对,我不知道该怎么做。

我该怎么做呢?

【问题讨论】:

  • 一个小样本输入和预期输出将使您的问题更加透明。
  • @PadraicCunningham 我已经更新了问题

标签: python list python-2.7 list-manipulation


【解决方案1】:

您应该转置子列表并使用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

【讨论】:

  • 嘿,我也在做同样的事情,使用链 :)
  • @Hackaholic,干杯,很高兴得到一个真正正确的答案的支持
  • @PadraicCunningham list(x) 和 list(y) 如何拥有我想要的东西? list 是 chain 的内置函数吗?
  • chain 返回一个 itertools.chain 对象,在它上面调用 list 只是迭代它并创建一个列表。您可以迭代链对象而不将所有内容保存在内存中
【解决方案2】:

您的 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]])

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

【讨论】:

    【解决方案3】:

    我会使用两个简单的 for 循环(对于大于 2 的 LIST 也是通用的):

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

    【讨论】:

    • 一个非常简短且更简洁的答案,无需使用任何导入! +1
    • 但它会更慢并且吃掉内存,O(N*M) 而不是 O(N+M)
    • @Hackaholic 好吧,对于内部包含超过一百万个列表的列表来说可能......但对于像我这样的业余程序员来说,这看起来可以理解!
    • 感谢您接受我的回答 =) @Hackaholic 我同意,它在时间和空间复杂度方面更复杂 =)
    【解决方案4】:

    将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
    

    【讨论】:

    • 我收到TypeError: zip() argument after * must be a sequence, not int
    • 您将l 提供为int。
    • 这不适用于列表列表,在提供的输入中它返回 [(38.621833, -10.825707), (38.28152, -10.744559)] 这甚至没有关闭
    【解决方案5】:

    给你。 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)
    

    【讨论】:

    • 但是我的列表是列表的列表。那我该怎么做呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2022-10-03
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多