简体   繁体   中英

Multiplying two lists in python?

I'm trying to print a chess-board like list and I'm hoping it'll look something like this :)

row=1,2,3,4,5,6,7,8 column = a,b,c,d,e,f,g

board=[[1A], [1B], [1C], ......etc.]

Is there any way to do this? I'm still a newbie with python.

You can do:

row = [1,2,3,4,5,6,7,8]
column = ['a','b','c','d','e','f','g','h']
board = [[str(i)+j.upper() for j in column] for i in row]
>>> print board
['1A', '1B', '1C', '1D', '1E', '1F', '1G', '1H']
['2A', '2B', '2C', '2D', '2E', '2F', '2G', '2H']
...
['8A', '8B', '8C', '8D', '8E', '8F', '8G', '8H']

What is happening

The code above uses double list-comprehension to create eight lists of eight elements each thus creating a product of the rows and columns and giving a 2-D board.

Simpler visualization

>>> a = ['a','b','c']
>>> b = ['a','b','c']

>>> print [[i+j for j in a] for i in b]

[['aa','ab','ac'],
 ['ba','bb','bc'],
 ['ca','cb','cc']]

The most pythonic way in my opinion is to use the itertools standard library like so:

from itertools import product
board = list(product(rows, columns))

this will give you a list of tuples that you then can join in the way the above posters suggest (you might actually want to keep them as tuples, make a dict out of it for further usage for example instead of having a strings you could use

result = [{'row': r, 'column': c} for r, c in product(rows, columns)]

or, if you need to do something on the fly you just do

for row, column in product(rows, columns):
    # do something for each row colum combination

it is superior to a nested list comprehension in clarity, and it is a generator, so it's more memory efficient. The latter argument doesn't really matter in your case of a chessboard though.

You most probably wanted a list of lists:

board = [['{}{}'.format(row, col) for col in columns] for row in rows]

This produces a nested list of lists :

>>> rows = [1, 2, 3, 4, 5, 6, 7, 8]
>>> columns = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> board = [['{}{}'.format(row, col) for col in columns] for row in rows]
>>> from pprint import pprint
>>> pprint(board)
[['1a', '1b', '1c', '1d', '1e', '1f', '1g', '1h'],
 ['2a', '2b', '2c', '2d', '2e', '2f', '2g', '2h'],
 ['3a', '3b', '3c', '3d', '3e', '3f', '3g', '3h'],
 ['4a', '4b', '4c', '4d', '4e', '4f', '4g', '4h'],
 ['5a', '5b', '5c', '5d', '5e', '5f', '5g', '5h'],
 ['6a', '6b', '6c', '6d', '6e', '6f', '6g', '6h'],
 ['7a', '7b', '7c', '7d', '7e', '7f', '7g', '7h'],
 ['8a', '8b', '8c', '8d', '8e', '8f', '8g', '8h']]

and you can address an individual row with:

board[rownumber]

or a specific chess position with:

board[rownumber][columnnumber]

note that the column is a number too ! You'd have to translate from column name ( 'a' , 'b' , etc.) to column number here:

board[rownumber][ord(columnname) - 97]

would do that, as ord('a') (the ASCII position of the character 'a' ) is 97. Indexing is 0-based; chess position 1a translates to board[0][0] :

>>> board[0][0]
'1a'

You can use map:

 map(lambda x:(map(lambda y:str(x)+y,column)),row)

Output:

[
['1a', '1b', ..],
['2a', '2b', ..],
etc.. 
]
from itertools import product
row = range(1.9)
column = ['A', 'B', 'C', 'D', 'E', 'F','G', 'H']
board = [['{}{}'.format(*pos)] for pos in product(row,column)]
print board

[[1A], [1B], [1C], ......etc.]

EDIT :

So product(row, colum) creates a generator that will produce (1,'A'), (1, 'B'), ...,(8, 'G'), (8, 'H') one at a time. In other words it will create every combination of a entry in row with every entry in column . '{}{}'.format(*pos) takes the contents of the 2-tuple and splats, using the * , it into the format function. Another way to write it would be

board = [['{}{}'.format(r,c)] for r,c in product(row,column)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM