HARSH ASHRA
HARSH ASHRA

Reputation: 316

How to select 11 best players from a list of players in python?

I am trying to create a program where I should be able to randomly select 11 players from 4 lists of players. Here is what I have tried.

import random
                                                                   # PLayers   Min   Max
                                                                   # ---------------------
wk = ['Rahul', 'Siefiet']                                          # wk         1     4
batsman = ['Rohit', 'Iyer', 'Munro', 'Pandey', 'Guptil', 'Taylor'] # batsman    3     6
bowler = ['Bumrah', 'Southee', 'Chahal', 'Sodhi', 'Dube']          # bowler     3     6
allRounder = ['jadeja', 'Santer', 'Dube']                          # allrounder 1     4
counter = 0

bat = random.randint(3, 6)
allr = random.randint(1, 4)
print(bat)

while counter != 11:
    for a in range(bat):
        print("Batsman:", set(random.choice(batsman)))
        counter += bat #adding random generated number to counter
    for a in range(bat):
        print("Bowler", set(random.choice(bowler)))
        counter += bat
    for a in range(allr):
        print("Allrounder:", set(random.choice(allRounder)))
        counter += allr
    for a in range(allr):
        print("Allrounder:", set(random.choice(wk)))
        counter += allr

When I try to run it. It goes to an infinite loop, Here is a screenshot.

enter image description here

Here is something I want to add to this.

if the randomly generated value for the batsman is 6 then 3 bowlers 1 all-rounder 1 wk
if the randomly generated value for the (batsman is 5) then (3 or 4 bowlers), (1 or 2 all-rounder depending upon if 3 bowlers are opted then 2 all rounders if 4 bowlers are opted then 1 allrounder), (1 or 2 wk depending upon if 2 AR are opted then 1 wk if 1 AR is opted then 2 wk)
And Many more conditions Like This

Any Idea how can I implement it???

Upvotes: 0

Views: 608

Answers (2)

alec
alec

Reputation: 6112

In addition to causing an infinite loop, the while loop does not guarantee you will get exactly 11 players each time. Here is another way you can do it. Shuffle each list, and add the minimum number of players from each list to a new list chosen. This starts you off with 8 players. Then combine and shuffle the remaining players from all 4 lists, and add 3 more. Now you've got 11 randomly chosen players with the requirements you set, and you can use a dictionary to print them according to category.

wk = ['Rahul', 'Siefiet']
batsman = ['Rohit', 'Iyer', 'Munro', 'Pandey', 'Guptil', 'Taylor']
bowler = ['Bumrah', 'Southee', 'Chahal', 'Sodhi', 'Dube']
allRounder = ['jadeja', 'Santer']

random.shuffle(batsman)
random.shuffle(bowler)
random.shuffle(allRounder)
random.shuffle(wk)

chosen = batsman[:3] + bowler[:3] + allRounder[:1] + wk[:1]
remainder = batsman[3:] + bowler[3:] + allRounder[1:] + wk[1:]
random.shuffle(remainder)
chosen.extend(remainder[:3])

players = {'Batsman': [x for x in chosen if x in batsman],
           'Bowler': [x for x in chosen if x in bowler],
           'AllRounder': [x for x in chosen if x in allRounder],
           'Wk': [x for x in chosen if x in wk]}

for key in players:
    for name in players[key]:
        print(f'{key}: {name}')

Upvotes: 1

kederrac
kederrac

Reputation: 17322

I am trying to create a program where I should be able to randomly select 11 players from 4 lists of players

you can use random.sample:

import random

final = []
min_to_extract = [1, 3, 4, 3]
for m , l in zip(min_to_extract, (wk, batsman, bowler, allRounder)):
    index_to_extract = random.sample(range(len(l)), m)
    final.extend([l[i] for i in index_to_extract])

    l[:] = [e for i, e in enumerate(l) if i not in index_to_extract]


final.extend(random.sample([*wk, *batsman, *bowler, *allRounder], 11 - len(final)))

random.shuffle(final)

output:

['Rohit',
 'Dube',
 'Guptil',
 'Chahal',
 'Pandey',
 'Santer',
 'jadeja',
 'Southee',
 'Siefiet',
 'Bumrah',
 'Dube']

Upvotes: 0

Related Questions