小编Car*_*pus的帖子

在列表列表中获取对

我需要你的帮助,我希望你能指出我正确的方向,所以我得到了一个列表列表(这只是一个例子,列表列表可以有更多的元素),我试图得到成对的元素列表

mylist = [[2, 3, 4], [2, 3]]

#desired output
# newlist = [[2,3], [3,4], [2,3]]
Run Code Online (Sandbox Code Playgroud)

所以在这个问题中它有助于创建一个元组列表,其中每个元组都是一对,所以我使用这个问题的答案来创建这个代码

mylist = [[2, 3, 4], [2, 3]]

coordinates = []

for i in mylist:
    coordinates.append(list(map(list, zip(i, i[1:])))) #Instead of list of tuples, I use map to get a list of lists

print(coordinates)
#output [[[2, 3], [3, 4]], [[2, 3]]] #3D list  but not exactly what I want

a = [e for sl in coordinates for e in sl] #Use list comprehension …
Run Code Online (Sandbox Code Playgroud)

python list-comprehension list

0
推荐指数
1
解决办法
166
查看次数

标签 统计

list ×1

list-comprehension ×1

python ×1