【问题标题】:Append items from Y list to one specific list from X list Python将 Y 列表中的项目附加到 X 列表 Python 中的一个特定列表
【发布时间】:2023-03-31 01:17:02
【问题描述】:

我有两个列表xy

x 列表里面有几个列表。例如:

x = [['1', 'hello', 'a'], ['3', 'hello', 'b'], ['11', 'hello', 'c'], ['2', 'hello', 'd'], ['4', 'hello', 'e'], ['22', 'hello', 'f']]

y 列表是单个列表。里面的项目是 url,其中包含 x 列表中每个列表的信息。

y = ['odd1', 'even2', 'odd3', 'even4', 'odd11', 'even22']

我从网络抓取中获得了信息。所以我试图将y 列表中的一个项目附加到x 列表中的一个列表中。例如,y[0] 项目应附加到 x[0] 列表,但 y[1] 项目应附加到 x[3]

这是我正在寻找的输出:

output = [['1', 'hello', 'a', 'odd1'], ['3', 'hello', 'b', 'odd3'], ['11', 'hello', 'c', 'odd11'], ['2', 'hello', 'd', 'even2'], ['4', 'hello', 'e', 'even4'], ['22', 'hello', 'f', 'even22']]

我不知道如何编码这些信息。首先,y 列表中的项目已排序,但 x 列表中的列表未排序。然而,他们有一个宠物。它们从我抓取的 odd 行的信息开始,然后是 even 行的信息。

我已经尝试过这些,以便首先对 x 列表进行排序:

1. x.sort(key=int)
2. sorted(x) | Result [['1...'], ['11...], ['2...'], ['22...'], ['3...]...]
3. x = [int(x) for x in x]

我怎么没有一个好的结果。

另一方面,我尝试以这种简单的方式将 y 中的项目附加到 x 列表中:

for i in x:
    i.append(y[:])

显然,y 列表中的所有项目都附加到 x 的每个列表中

我该如何解决这个代码。谢谢!

【问题讨论】:

    标签: python list python-2.7 append list-comprehension


    【解决方案1】:

    试试这个

    x = [['1', 'hello', 'a'], ['3', 'hello', 'b'], ['11', 'hello', 'c'], ['2', 'hello', 'd'], ['4', 'hello', 'e'], ['22', 'hello', 'f']]
    y = ['odd1', 'even2', 'odd3', 'even4', 'odd11', 'even22']
    x.sort(key=lambda z: int(z[0]))
    for i in range(0, len(x)):
        x[i].append(y[i])
    print(x)
    

    输出:

    [['1', 'hello', 'a', 'odd1'], ['2', 'hello', 'd', 'even2'], ['3', 'hello', 'b', 'odd3'], ['4', 'hello', 'e', 'even4'], ['11', 'hello', 'c', 'odd11'], ['22', 'hello', 'f', 'even22']]
    

    【讨论】:

    • 非常感谢这个解决方案。太好了,@SamuelLEMAITRE。
    【解决方案2】:
    x = [['1', 'hello', 'a'],
         ['3', 'hello', 'b'],
         ['11', 'hello', 'c'],
         ['2', 'hello', 'd'],
         ['4', 'hello', 'e'],
         ['22', 'hello', 'f']]
    
    y = ['odd1', 'even2', 'odd3', 'even4', 'odd11', 'even22']
    
    x = sorted(x, key=lambda i: int(i[0]))
    y = sorted(y, key=lambda i: int(i.replace('even', '').replace('odd', '')))
    
    print y
    
    result = [a + [b] for a, b in zip(x, y)]
    print result #prints out [['1', 'hello', 'a', 'odd1'], ['3', 'hello', 'b', 'odd3'], ...]
    

    【讨论】:

      【解决方案3】:

      y 重新排序为

      In [6]: y[::2]+y[1::2]
      Out[6]: ['odd1', 'odd3', 'odd11', 'even2', 'even4', 'even22']
      

      然后您可以使用zip 将重新排序的ys 与x 配对:

      x = [['1', 'hello', 'a'], ['3', 'hello', 'b'], ['11', 'hello', 'c'], 
           ['2', 'hello', 'd'], ['4', 'hello', 'e'], ['22', 'hello', 'f']]
      y = ['odd1', 'even2', 'odd3', 'even4', 'odd11', 'even22']
      print([xi+[yi] for xi, yi in zip(x, y[::2]+y[1::2])])
      

      产量

      [['1', 'hello', 'a', 'odd1'],
       ['3', 'hello', 'b', 'odd3'],
       ['11', 'hello', 'c', 'odd11'],
       ['2', 'hello', 'd', 'even2'],
       ['4', 'hello', 'e', 'even4'],
       ['22', 'hello', 'f', 'even22']]
      

      【讨论】:

      • 很好的解决方案。简单,一条线,效果很好。感谢您的支持。
      猜你喜欢
      • 2013-03-08
      • 2021-03-17
      • 2021-12-15
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      相关资源
      最近更新 更多