简体   繁体   中英

Generating possible Combinations of a given list of games in Python

I would like to generate the total number of possibilities from given list of games like ['France - Germany' . Such that total outcome will be 3 possible outcomes for this case {either the home team wins, they tie, or the away team wins}.

  • Possible outcomes will be 3
1   
FRANCE - GERMANY 1
2   
FRANCE - GERMANY x
3   
FRANCE - GERMANY 2

And for another case where the list is ['France - Germany', 'Spain - Italia']

  • Possible number of outcomes are 9
1
FRANCE - GERMANY 1
SPAIN - ITALIA 1 
2   
FRANCE - GERMANY 1
SPAIN - ITALIA x 
3   
FRANCE - GERMANY 1
SPAIN - ITALIA 2
4   
FRANCE - GERMANY x
SPAIN - ITALIA 1
5   
FRANCE - GERMANY x
SPAIN - ITALIA x
6   
FRANCE - GERMANY x
SPAIN - ITALIA 2
7   
FRANCE - GERMANY 2
SPAIN - ITALIA 1
8   
FRANCE - GERMANY 2
SPAIN - ITALIA x
9   
FRANCE - GERMANY 2
SPAIN - ITALIA 2

The Following is my code

import itertools

games = ['France - Germany']
case = ["1","X","2"]
results = []
for eachcase in case:
    for game in games:
        results.append("%s %s" % (game, eachcase))


print("\n".join(results)+ "\n %s" %len(results))

and its output for item in the list is

France - Germany 1
France - Germany X
France - Germany 2

Which is fine but as the number of items of the list increases it doesn't work. Any help in working this out would be appreciated thanks in advance :)

you can do this easily with list comprehensions:

fg = [ ("FRANCE-GERMANY",outcome) for outcome in (1,"x",2) ]
for g1 in fg: print(g1)
print("")

si = [ ("SPAIN-ITALY",outcome) for outcome in (1,"x",2) ]
final = [ (g1,g2) for g1 in fg for g2 in si ]
for g1,g2 in final:
    print(g1)
    print(g2)
    print("")

If you want a more generalized approach, the product function in the itertools module can also help:

from itertools import product
fg    = list(product(["FRANCE-GERMANY"],(1,"x",2)))
si    = list(product(["SPAIN-ITALY"],(1,"x",2)))
final = list(product(fg,si))

You can use itertools.product with the repeat argument to create as many game result products as the total number of games.

from itertools import product
games = ['France - Germany', 'Spain - Italia']
case = ["1","X","2"]

results = []
for i in product(case, repeat = len(games)):
    results.append({k:v for k, v in zip(games, i)})
    #if you prefer list of tuples instead, alternatively,
    #results.append([(k, v) for k, v in zip(games, i)])

print(results)

Output:

[{'France - Germany': '1', 'Spain - Italia': '1'},
 {'France - Germany': '1', 'Spain - Italia': 'X'},
 {'France - Germany': '1', 'Spain - Italia': '2'},
 {'France - Germany': 'X', 'Spain - Italia': '1'},
 {'France - Germany': 'X', 'Spain - Italia': 'X'},
 {'France - Germany': 'X', 'Spain - Italia': '2'},
 {'France - Germany': '2', 'Spain - Italia': '1'},
 {'France - Germany': '2', 'Spain - Italia': 'X'},
 {'France - Germany': '2', 'Spain - Italia': '2'}]

For a given list of games, we can use itertools.product for this:

from itertools import product

def game_product(games):
    for i, res in enumerate(product('1x2', repeat=len(games)), 1):
        print(i)
        for gr in zip(games, res):
            print('{} {}'.format(*gr))

For example:

>>> game_product(['France - Germany', 'Spain - Italia', 'Brazil - Spain'])
1
France - Germany 1
Spain - Italia 1
Brazil - Spain 1
2
France - Germany 1
Spain - Italia 1
Brazil - Spain x
3
France - Germany 1
Spain - Italia 1
Brazil - Spain 2
4
France - Germany 1
Spain - Italia x
Brazil - Spain 1
5
France - Germany 1
Spain - Italia x
Brazil - Spain x
6
France - Germany 1
Spain - Italia x
Brazil - Spain 2
7
France - Germany 1
Spain - Italia 2
Brazil - Spain 1
8
France - Germany 1
Spain - Italia 2
Brazil - Spain x
9
France - Germany 1
Spain - Italia 2
Brazil - Spain 2
10
France - Germany x
Spain - Italia 1
Brazil - Spain 1
11
France - Germany x
Spain - Italia 1
Brazil - Spain x
12
France - Germany x
Spain - Italia 1
Brazil - Spain 2
13
France - Germany x
Spain - Italia x
Brazil - Spain 1
14
France - Germany x
Spain - Italia x
Brazil - Spain x
15
France - Germany x
Spain - Italia x
Brazil - Spain 2
16
France - Germany x
Spain - Italia 2
Brazil - Spain 1
17
France - Germany x
Spain - Italia 2
Brazil - Spain x
18
France - Germany x
Spain - Italia 2
Brazil - Spain 2
19
France - Germany 2
Spain - Italia 1
Brazil - Spain 1
20
France - Germany 2
Spain - Italia 1
Brazil - Spain x
21
France - Germany 2
Spain - Italia 1
Brazil - Spain 2
22
France - Germany 2
Spain - Italia x
Brazil - Spain 1
23
France - Germany 2
Spain - Italia x
Brazil - Spain x
24
France - Germany 2
Spain - Italia x
Brazil - Spain 2
25
France - Germany 2
Spain - Italia 2
Brazil - Spain 1
26
France - Germany 2
Spain - Italia 2
Brazil - Spain x
27
France - Germany 2
Spain - Italia 2
Brazil - Spain 2

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