[英]Mixing lists in python
from itertools import product
x_coord = ['a','b','c','d','e']
y_coord = ['1', '2', '3', '4', '5']
board = []
index = 0
for item in itertools.product(x_coord, y_coord):
board += item
for elements in board:
board[index] = board[index] + board[index +1]
board.remove(board[index +1])
index += 1
print board
Hello. 你好。 Let me explain what I want to do with that:
让我解释一下我要怎么做:
I have two lists( x_coord
and y_coord
) and I want to mix them like that: 我有两个列表(
x_coord
和y_coord
),我想像这样混合它们:
board = ['a1', 'a2', ..., 'e1', 'e2', ...]
But I get the IndexError: list index out of range
error instead of that. 但我得到
IndexError: list index out of range
错误而不是那个。
How should I proceed? 我应该如何进行?
OBS.:If there's any type of error in my english, please tell me. OBS .:如果我的英语有任何错误,请告诉我。 I'm learning english as well as code.
我正在学习英语和代码。
You can try like that, 你可以这样尝试
>>> x_coord = ['a','b','c','d','e']
>>> y_coord = ['1', '2', '3', '4', '5']
>>> [item + item2 for item2 in y_coord for item in x_coord]
['a1', 'b1', 'c1', 'd1', 'e1', 'a2', 'b2', 'c2', 'd2', 'e2', 'a3', 'b3', 'c3', 'd3', 'e3', 'a4', 'b4', 'c4', 'd4', 'e4', 'a5', 'b5', 'c5', 'd5', 'e5']
Sorted results: 排序结果:
>>> sorted([item + item2 for item2 in y_coord for item in x_coord])
['a1', 'a2', 'a3', 'a4', 'a5', 'b1', 'b2', 'b3', 'b4', 'b5', 'c1', 'c2', 'c3', 'c4', 'c5', 'd1', 'd2', 'd3', 'd4', 'd5', 'e1', 'e2', 'e3', 'e4', 'e5']
t = [a + b for a,b in itertools.product(x_coord,y_coord)]
print t % prints what you want
Normally itertools.product(x_coord,y_coord)
will print the following: [('a', '1'), ('a', '2'), ('a', '3'), ('a', '4'), ('a', '5'), ('b', '1'), ('b', '2'), ('b', '3'), ('b', '4'), ('b', '5'), ('c', '1'), ('c', '2'), ('c', '3'), ('c', '4'), ('c', '5'), ('d', '1'), ('d', '2'), ('d', '3'), ('d', '4'), ('d', '5'), ('e', '1'), ('e', '2'), ('e', '3'), ('e', '4'), ('e', '5')] 通常,
itertools.product(x_coord,y_coord)
将打印以下内容:[['a','1'),('a','2'),('a','3'),('a', '4'),('a','5'),('b','1'),('b','2'),('b','3'),('b', '4'),('b','5'),('c','1'),('c','2'),('c','3'),('c', '4'),('c','5'),('d','1'),('d','2'),('d','3'),('d', '4'),('d','5'),('e','1'),('e','2'),('e','3'),('e', '4'),('e','5')]
As you can see it's already in order because itertools.product
will multiply a
by each index in y_coord
before moving x_coord
to b
, etc. etc. 正如你可以看到它已经为了因为
itertools.product
要加倍a
在每个索引y_coord
移动前x_coord
到b
,等等等等。
By using list comprehension
we can combine the two indices using a+b
for each pair in the output, resulting in this: 通过使用
list comprehension
我们可以对输出中的每一对使用a+b
组合两个索引,结果是:
['a1', 'a2', 'a3', 'a4', 'a5', 'b1', 'b2', 'b3', 'b4', 'b5', 'c1', 'c2', 'c3', 'c4', 'c5', 'd1', 'd2', 'd3', 'd4', 'd5', 'e1', 'e2', 'e3', 'e4', 'e5'] ['a1','a2','a3','a4','a5','b1','b2','b3','b4','b5','c1','c2',' c3','c4','c5','d1','d2','d3','d4','d5','e1','e2','e3','e4','e5' ]
combined = map(lambda x: ''.join(x), product(x_coord, y_coord))
coord = map(lambda x,y: x+y, x_coord, y_coord)
print(coord)
x_coord = ['a','b','c','d','e']
y_coord = ['1', '2', '3', '4', '5']
a=[]
for i in range(len(x_coord)):
for j in range(len(y_coord)):
a.append(x_coord[i].join(" "+ y_coord[j]))
b=[]
for item in a:
b.append(item.replace(" ",""))
print b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.