John
John

Reputation: 143

Some ways to combine sets in python

I am trying to write a game that uses grids, and I need a way to make sets by combining two other sets.

For example, if I had [a, b, c] and [1, 2, 3], are there are any functions in Python 3 that will give me [a1, a2, a3, b1, b2, b3, c1, c2, c3]?

Upvotes: 2

Views: 1630

Answers (3)

Eric
Eric

Reputation: 466

letters = ["a", "b", "c"]
numbers = [ 1 ,  2 ,  3 ]

[x + str(y) for x in letters for y in numbers]

Upvotes: 0

unutbu
unutbu

Reputation: 879561

Use itertools.product:

In [41]: import itertools

In [42]: x='abc'

In [43]: y='123'

In [45]: [letter+num for letter,num in itertools.product(x,y)]
Out[45]: ['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']

Upvotes: 4

Rui Jiang
Rui Jiang

Reputation: 1672

http://docs.python.org/tutorial/datastructures.html

Take a look at the map function.

Upvotes: 0

Related Questions