1

edited:

to make simpler let imagine that I have card game that has 60 cards I want ti=o distribute this card for 6 players.

I want to card to be distributed 1 by 1 or 2 by 2 or 3 by 3 or 10 by 10

Code

import random

Indtester =0
Testword = []

while Indtester < 60:
   item = random.randint(0,100)
   Testword.append(str(item))
   Indtester=Indtester+1

words=Testword

#---------------Ini variable befeore the build
limt =5
ind = 1
Comb1=[]
Comb2=[]
Comb3=[]
Comb4=[]
Comb5=[]
Comb6=[]

total =len(words)

PlayerHods = total/6
print("total ",total,"PlayerHods ",PlayerHods)
MaxRound =PlayerHods/limt

Round =1

#---------------Ini variable befeore the build
print("Sarting Buulid the combo lists")
print("The max rounds is ",MaxRound)
for line in words:
   if ind <= limt :
        word = line
        word = word.replace("\n","")
        lastwordcomb1 =word
        lastindc1=ind
        Comb1.append(word)
   if ind > limt and ind <= limt*2:
            word = line
            word = word.replace("\n","")
            lastwordcomb2 =word
            lastindc2=ind
            Comb2.append(word)
   if ind > limt*2 and ind <= limt*3:
            word = line
            word = word.replace("\n","")
            lastwordcomb3 =word
            lastindc3=ind
            Comb3.append(word)
   if ind >  limt*3 and  ind <=  limt*4:
            word = line
            word = word.replace("\n","")
            lastwordcomb4 =word
            lastindc4=ind
            Comb4.append(word)
   if ind >  limt*4 and  ind <=  limt*5:
            word = line
            lastwordcomb5 =word
            lastindc5=ind
            word = word.replace("\n","")
            Comb5.append(word)
    if ind >  limt*5 and ind <= limt*6 :
            word = line
            word = word.replace("\n","")
            lastwordcomb6 =word
            lastindc6=ind
            Comb6.append(word)
    ind =ind+1
    if ind >  limt*6:
            word = line
            word = word.replace("\n","")
            lastwordcomb1 =word
            lastindc1=ind
            Comb1.append(word)
            ind =1
            Round=Round+1



 print(Comb1)
 print(Comb2)
 print(Comb3)
 print(Comb4)
 print(Comb5)
 print(Comb6)
print(len(Comb1),len(Comb2),len(Comb3),len(Comb4),len(Comb5),len(Comb6))

As you can see I have the 'Comb1' with 12 and I can see the 30 being part of the comb6 and also in the Comb1 ir should be only in Comb1 same with 60

Here is the result of my code:

Output

Uploading the file tolist
total  60 PlayerHods  10.0
Sarting Buulid the combo lists
The max rounds is  2.0
['1', '2', '3', '4', '5', '30', '31', '32', '33', '34', '35', '60']
['6', '7', '8', '9', '10', '36', '37', '38', '39', '40']
['11', '12', '13', '14', '15', '41', '42', '43', '44', '45']
['16', '17', '18', '19', '20', '46', '47', '48', '49', '50']
['21', '22', '23', '24', '25', '51', '52', '53', '54', '55']
['26', '27', '28', '29', '30', '56', '57', '58', '59', '60']
12 10 10 10 10 10
Djalil lounis
  • 121
  • 1
  • 1
  • 15
  • 1
    I'm not sure what all that code is doing, but is your question basically how to group the elements in a list into sub-groups of a given size? – martineau Jan 12 '19 at 15:07
  • Like this: https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks? Except instead of pulling from one list, they pull from a collection of lists? – Kenny Ostrom Jan 12 '19 at 15:17
  • Not exactly, it is simile when you are going to shuffle cards so I have 6 players and I have a game of card that has 600 cards I want to distribute the card for the player 2 by 2 or 3 by 3 or 6 by 6 – Djalil lounis Jan 12 '19 at 15:49
  • @Djalil - if we have 4 Cards, `[A, B, C, D]` and 2 players - you can distribute 1 by 1 OR 2 by 2. In the first case you will get `[AC, BD]` and in the second case `[AB, CD]`. Am I understanding this right? – Mortz Jan 12 '19 at 16:07
  • @Mortz yes but in my case I have 6 players so I and a total of 6 cards so I can distribute only by 2 or 5 to have an even number of cards – Djalil lounis Jan 12 '19 at 16:30
  • 1
    @Djalillounis can you explain better what are you trying do to? I can not understand anything about your problem looking at the code and it does not even execute because in the first iteration it throws a NameError (lastwordcombo2 is not defined) – JoshuaCS Jan 12 '19 at 16:38
  • @JosuéCortina I have a list that has 60 elements , I want to distribute the 60 element to 6 lists take 2 or 5 each time. You can remove all the prints and just print the lists, I will edit the question – Djalil lounis Jan 12 '19 at 16:47
  • @Djalillounis Randomly or in the order in which they appear in the original list? And how many elements should be on each sub list? 10? or it doesnt matter? – JoshuaCS Jan 12 '19 at 16:49
  • No not randomly they will be 2 for list1 then next 2 for list2 the next 2 for list3 ...etc the len(sublist) = the total of the big list / number of sublist – Djalil lounis Jan 12 '19 at 16:52
  • @Djalillounis if the list is [1,2,3,4,5,6,7,8,9] and there are 3 players, is this partition correct? 1: [1,2,7,8] 2: [3,4, 9] 3: [5,6] – JoshuaCS Jan 12 '19 at 17:02
  • @JosuéCortina no they all have to have the same number of cards len(list1)=len(list2)=len(list3) – Djalil lounis Jan 12 '19 at 17:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186601/discussion-between-josue-cortina-and-djalil-lounis). – JoshuaCS Jan 12 '19 at 17:24

3 Answers3

0

Something like this

cards = [str(num) for num in range(60)]

num_players = 6

player_hands = []

for player in range(num_players):
    player_hands.append([])

# 1 by 1 or 2 by 2, etc
amount_at_a_time = 5

for card_idx, card in enumerate(cards):
    # Use % to cycle through players 0-5
    current_player = int(card_idx//amount_at_a_time) % num_players

    player_hands[current_player].append(card)

[print(hand) for hand in player_hands]

Output:

['0', '1', '2', '3', '4', '30', '31', '32', '33', '34']
['5', '6', '7', '8', '9', '35', '36', '37', '38', '39']
['10', '11', '12', '13', '14', '40', '41', '42', '43', '44']
['15', '16', '17', '18', '19', '45', '46', '47', '48', '49']
['20', '21', '22', '23', '24', '50', '51', '52', '53', '54']
['25', '26', '27', '28', '29', '55', '56', '57', '58', '59']
Default picture
  • 710
  • 5
  • 12
  • hello , not exactly what I want , because the 1st player should 5 successive card 0,1,2,3,4,5 then the next 5 for player 2 till it get to player6 and then again player1 will have 5 successive cards – Djalil lounis Jan 12 '19 at 17:43
  • It's 2 by 2 because you said "I want to card to be distributed 1 by 1 or 2 by 2 or 3 by 3 or 10 by 10". For it to be 5 at a time then change amount_at_a_time to 5. – Default picture Jan 12 '19 at 17:45
0

In case it will help someone hace found the solution i just needed to put next in the last if block without changing any thing:

for line in words:

    if ind <= limt :
        word = line
        word = word.replace("\n","")
        lastwordcomb1 =word
        lastindc1=ind
        Comb1.append(word)



    if ind > limt and ind <= limt*2:
            word = line
            word = word.replace("\n","")
            lastwordcomb2 =word
            lastindc2=ind
            Comb2.append(word)

    if ind > limt*2 and ind <= limt*3:
            word = line
            word = word.replace("\n","")
            lastwordcomb3 =word
            lastindc3=ind
            Comb3.append(word)


    if ind >  limt*3 and  ind <=  limt*4:
            word = line
            word = word.replace("\n","")
            lastwordcomb4 =word
            lastindc4=ind
            Comb4.append(word)

    if ind >  limt*4 and  ind <=  limt*5:
            word = line
            lastwordcomb5 =word
            lastindc5=ind
            word = word.replace("\n","")
            Comb5.append(word)

    if ind >  limt*5 and ind <= limt*6 :
            word = line
            word = word.replace("\n","")
            lastwordcomb6 =word
            lastindc6=ind
            Comb6.append(word)

    ind =ind+1
    if ind >  limt*6:
            next
            ind =1
            Round=Round+1
Djalil lounis
  • 121
  • 1
  • 1
  • 15
0

Finally I think I could understand what you want. This is my solution:

Code

import random

TOTAL = 18
N = 3
K = 3
ROUNDS_SIZE = N * K

words = [random.randint(0, 100) for _ in range(TOTAL)]

partition = [[words[i] for i in (j + n * K + k for j in range(0, TOTAL, ROUNDS_SIZE) for k in range(K))] for n in range(N)]

print(partition)

it is extremely short and makes an exhaustive use of comprehensions.

Assuming TOTAL = 18, N = 3, K = 3 and words = list(range(TOTAL)) (that is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]), this is the output:

[[0, 1, 2, 9, 10, 11], [3, 4, 5, 12, 13, 14], [6, 7, 8, 15, 16, 17]]

Let me know if this worked for you.

JoshuaCS
  • 2,524
  • 1
  • 13
  • 16
  • @DjalilIounis did my answer work for you? If so, please, mark it as **accepted** (green checkmark), otherwise, let me know what went wrong. Thnx in advance! – JoshuaCS Jan 14 '19 at 14:30