简体   繁体   中英

Combining Multiple Sets of Letters in Python

I am trying to figure out how to print all the combinations there are for multiple sets of letters without repetition.

An example: A,B,C and X,Y,Z

The combinations would be:

AX AY AZ BX BY BZ CX CY CZ

You can use itertools.product to get what you want.

from itertools import product
a = ['A', 'B', 'C']
b = ['X', 'Y', 'Z']

for i in product(a, b):
    print ''.join(i)

You could just loop over both sets:

for a in abcstring:
    for x in xyzstring:
        print a + x

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