Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表的数据与其最后一个值配对_Python_List - Fatal编程技术网

Python 将列表的数据与其最后一个值配对

Python 将列表的数据与其最后一个值配对,python,list,Python,List,如何将列表的值与同一列表的最后一个值配对,而不包括其最后一个值 [1,2,3,4,5,10] [6,7,8,9,10,11] [12,13,14,15,16,17] 预期产出: [1,10], [2,10], [3,10], [4,10], [5,10] [6,11], [7,11], [8,11], [9,11], [10,11] [12,17], [13,17], [14,17], [15,17], [16,17] 这是一种使用列表切片的方法 Ex: lst = [[1,2,3,4,

如何将列表的值与同一列表的最后一个值配对,而不包括其最后一个值

[1,2,3,4,5,10]

[6,7,8,9,10,11]

[12,13,14,15,16,17]
预期产出:

[1,10], [2,10], [3,10], [4,10], [5,10]
[6,11], [7,11], [8,11], [9,11], [10,11]
[12,17], [13,17], [14,17], [15,17], [16,17]

这是一种使用列表切片的方法

Ex:

lst = [[1,2,3,4,5,10], [6,7,8,9,10,11], [12,13,14,15,16,17]]
for i in lst:
    print([[j, i[-1]] for j in i[:-1]])
[[1, 10], [2, 10], [3, 10], [4, 10], [5, 10]]
[[6, 11], [7, 11], [8, 11], [9, 11], [10, 11]]
[[12, 17], [13, 17], [14, 17], [15, 17], [16, 17]]
输出:

lst = [[1,2,3,4,5,10], [6,7,8,9,10,11], [12,13,14,15,16,17]]
for i in lst:
    print([[j, i[-1]] for j in i[:-1]])
[[1, 10], [2, 10], [3, 10], [4, 10], [5, 10]]
[[6, 11], [7, 11], [8, 11], [9, 11], [10, 11]]
[[12, 17], [13, 17], [14, 17], [15, 17], [16, 17]]

或者使用
itertools.product

from itertools import product

lst = [[1,2,3,4,5,10], [6,7,8,9,10,11], [12,13,14,15,16,17]]
for i in lst:
    print(list(product(i[:-1], [i[-1]])))

这是一种使用列表切片的方法

Ex:

lst = [[1,2,3,4,5,10], [6,7,8,9,10,11], [12,13,14,15,16,17]]
for i in lst:
    print([[j, i[-1]] for j in i[:-1]])
[[1, 10], [2, 10], [3, 10], [4, 10], [5, 10]]
[[6, 11], [7, 11], [8, 11], [9, 11], [10, 11]]
[[12, 17], [13, 17], [14, 17], [15, 17], [16, 17]]
输出:

lst = [[1,2,3,4,5,10], [6,7,8,9,10,11], [12,13,14,15,16,17]]
for i in lst:
    print([[j, i[-1]] for j in i[:-1]])
[[1, 10], [2, 10], [3, 10], [4, 10], [5, 10]]
[[6, 11], [7, 11], [8, 11], [9, 11], [10, 11]]
[[12, 17], [13, 17], [14, 17], [15, 17], [16, 17]]

或者使用
itertools.product

from itertools import product

lst = [[1,2,3,4,5,10], [6,7,8,9,10,11], [12,13,14,15,16,17]]
for i in lst:
    print(list(product(i[:-1], [i[-1]])))
你可以做:

x = [1,2,3,4,5,10]
x_new = [[i,x[-1]] for i in x[:-1]]
print(x_new)
输出:

[[1, 10], [2, 10], [3, 10], [4, 10], [5, 10]]
你可以做:

x = [1,2,3,4,5,10]
x_new = [[i,x[-1]] for i in x[:-1]]
print(x_new)
输出:

[[1, 10], [2, 10], [3, 10], [4, 10], [5, 10]]

您可以使用列表理解:

l = [1,2,3,4,5,10]
[[i, l[-1]] for i in l[:-1]]
或者您可以使用
itertools.cycle

from itertools import cycle

[[i, j] for i, j in zip(l, cycle(l[-1:]))]
输出:

[[1, 10], [2, 10], [3, 10], [4, 10], [5, 10], [10, 10]]

您可以使用列表理解:

l = [1,2,3,4,5,10]
[[i, l[-1]] for i in l[:-1]]
或者您可以使用
itertools.cycle

from itertools import cycle

[[i, j] for i, j in zip(l, cycle(l[-1:]))]
输出:

[[1, 10], [2, 10], [3, 10], [4, 10], [5, 10], [10, 10]]
使用
zip

>>>
>>> a = [1,2,3,4,5,10]
>>> x = [a[-1]]*len(a) #get last item, add to list, multiply by len of 'a' list.
>>> z = zip(a,x)
>>>output = []
>>> for x,y in zip(a,x):
...    output.append([x,y])
...
>>> output
[[1, 10], [2, 10], [3, 10], [4, 10], [5, 10], [10, 10]]
使用
zip

>>>
>>> a = [1,2,3,4,5,10]
>>> x = [a[-1]]*len(a) #get last item, add to list, multiply by len of 'a' list.
>>> z = zip(a,x)
>>>output = []
>>> for x,y in zip(a,x):
...    output.append([x,y])
...
>>> output
[[1, 10], [2, 10], [3, 10], [4, 10], [5, 10], [10, 10]]