【发布时间】:2023-03-31 01:17:02
【问题描述】:
我有两个列表x 和y:
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