divide items between two lists randomly












0















I must get items from a list by using class and inheritance and randomly append them to two lists equally,so I don`t know how to append them to lists randomly



import random

players = ['a', 'b', 'c','d','e', 'f']

A =
B =
teams =



class Human:
def __init__(self,name):
self.name = name



class Player(Human):

def __init__(self, name, team):
self.team = team
Human.__init__(self, name)

for i in range(6):
p = Player(random.choice(players))
a = p.team
print(p.name)
players.remove(p.name)
print(teams)









share|improve this question























  • What is the expected output?

    – Daniel Mesejo
    Jan 1 at 19:16






  • 3





    Shuffle, then halve the list.

    – deceze
    Jan 1 at 19:17











  • your code will give you duplicate players.

    – Patrick Artner
    Jan 1 at 19:20











  • @DanielMesejo two lists

    – EhsanK
    Jan 1 at 19:23
















0















I must get items from a list by using class and inheritance and randomly append them to two lists equally,so I don`t know how to append them to lists randomly



import random

players = ['a', 'b', 'c','d','e', 'f']

A =
B =
teams =



class Human:
def __init__(self,name):
self.name = name



class Player(Human):

def __init__(self, name, team):
self.team = team
Human.__init__(self, name)

for i in range(6):
p = Player(random.choice(players))
a = p.team
print(p.name)
players.remove(p.name)
print(teams)









share|improve this question























  • What is the expected output?

    – Daniel Mesejo
    Jan 1 at 19:16






  • 3





    Shuffle, then halve the list.

    – deceze
    Jan 1 at 19:17











  • your code will give you duplicate players.

    – Patrick Artner
    Jan 1 at 19:20











  • @DanielMesejo two lists

    – EhsanK
    Jan 1 at 19:23














0












0








0


2






I must get items from a list by using class and inheritance and randomly append them to two lists equally,so I don`t know how to append them to lists randomly



import random

players = ['a', 'b', 'c','d','e', 'f']

A =
B =
teams =



class Human:
def __init__(self,name):
self.name = name



class Player(Human):

def __init__(self, name, team):
self.team = team
Human.__init__(self, name)

for i in range(6):
p = Player(random.choice(players))
a = p.team
print(p.name)
players.remove(p.name)
print(teams)









share|improve this question














I must get items from a list by using class and inheritance and randomly append them to two lists equally,so I don`t know how to append them to lists randomly



import random

players = ['a', 'b', 'c','d','e', 'f']

A =
B =
teams =



class Human:
def __init__(self,name):
self.name = name



class Player(Human):

def __init__(self, name, team):
self.team = team
Human.__init__(self, name)

for i in range(6):
p = Player(random.choice(players))
a = p.team
print(p.name)
players.remove(p.name)
print(teams)






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 19:15









EhsanKEhsanK

287




287













  • What is the expected output?

    – Daniel Mesejo
    Jan 1 at 19:16






  • 3





    Shuffle, then halve the list.

    – deceze
    Jan 1 at 19:17











  • your code will give you duplicate players.

    – Patrick Artner
    Jan 1 at 19:20











  • @DanielMesejo two lists

    – EhsanK
    Jan 1 at 19:23



















  • What is the expected output?

    – Daniel Mesejo
    Jan 1 at 19:16






  • 3





    Shuffle, then halve the list.

    – deceze
    Jan 1 at 19:17











  • your code will give you duplicate players.

    – Patrick Artner
    Jan 1 at 19:20











  • @DanielMesejo two lists

    – EhsanK
    Jan 1 at 19:23

















What is the expected output?

– Daniel Mesejo
Jan 1 at 19:16





What is the expected output?

– Daniel Mesejo
Jan 1 at 19:16




3




3





Shuffle, then halve the list.

– deceze
Jan 1 at 19:17





Shuffle, then halve the list.

– deceze
Jan 1 at 19:17













your code will give you duplicate players.

– Patrick Artner
Jan 1 at 19:20





your code will give you duplicate players.

– Patrick Artner
Jan 1 at 19:20













@DanielMesejo two lists

– EhsanK
Jan 1 at 19:23





@DanielMesejo two lists

– EhsanK
Jan 1 at 19:23












1 Answer
1






active

oldest

votes


















5














Your code will give you the same players:




for i in range(6):
p = Player(random.choice(players)) # you can draw duplicates here



to avoid that either use random.sample(players, k=len(players)) over your whole names (this returns a new, shuffled list - players is unmodified) or random.shuffle(players) it in-place. Afterwards you divide the result into two parts and create the teams from that data:



You should probably overload the __repr__(self) methods of your class to make outputting neater.



import random

class Human:
def __init__(self,name):
self.name = name
def __str__(self):
return f"{self.name}"
def __repr__(self): return str(self)

class Player(Human):
def __init__(self, name, team):
super().__init__(name)
self.team = team
def __str__(self):
return f"{self.name} in team {self.team}"
def __repr__(self): return str(self)

players = ['a', 'b', 'c','d','e', 'f']
random.shuffle(players) # inplace shuffling

player_length = len(players) // 2 # make sure you got even player counts - else uneven teams
A = [Player(k,"A") for k in players[:player_length]] # first 3 are Team A
B = [Player(k,"B") for k in players[player_length:]] # rest is Team B
teams = [A,B]

print(teams)


Output:



[[d in team A, f in team A, b in team A],[a in team B, e in team B, c in team B]]


Doku:




  • random.sample

  • random.shuffle






share|improve this answer





















  • 1





    Would like to see programmatic approach to determining the halfway index rather than hard coding the 3

    – Dan Farrell
    Jan 1 at 19:39








  • 1





    @DanFarrell trivialy done :) thx

    – Patrick Artner
    Jan 1 at 19:43











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998237%2fdivide-items-between-two-lists-randomly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














Your code will give you the same players:




for i in range(6):
p = Player(random.choice(players)) # you can draw duplicates here



to avoid that either use random.sample(players, k=len(players)) over your whole names (this returns a new, shuffled list - players is unmodified) or random.shuffle(players) it in-place. Afterwards you divide the result into two parts and create the teams from that data:



You should probably overload the __repr__(self) methods of your class to make outputting neater.



import random

class Human:
def __init__(self,name):
self.name = name
def __str__(self):
return f"{self.name}"
def __repr__(self): return str(self)

class Player(Human):
def __init__(self, name, team):
super().__init__(name)
self.team = team
def __str__(self):
return f"{self.name} in team {self.team}"
def __repr__(self): return str(self)

players = ['a', 'b', 'c','d','e', 'f']
random.shuffle(players) # inplace shuffling

player_length = len(players) // 2 # make sure you got even player counts - else uneven teams
A = [Player(k,"A") for k in players[:player_length]] # first 3 are Team A
B = [Player(k,"B") for k in players[player_length:]] # rest is Team B
teams = [A,B]

print(teams)


Output:



[[d in team A, f in team A, b in team A],[a in team B, e in team B, c in team B]]


Doku:




  • random.sample

  • random.shuffle






share|improve this answer





















  • 1





    Would like to see programmatic approach to determining the halfway index rather than hard coding the 3

    – Dan Farrell
    Jan 1 at 19:39








  • 1





    @DanFarrell trivialy done :) thx

    – Patrick Artner
    Jan 1 at 19:43
















5














Your code will give you the same players:




for i in range(6):
p = Player(random.choice(players)) # you can draw duplicates here



to avoid that either use random.sample(players, k=len(players)) over your whole names (this returns a new, shuffled list - players is unmodified) or random.shuffle(players) it in-place. Afterwards you divide the result into two parts and create the teams from that data:



You should probably overload the __repr__(self) methods of your class to make outputting neater.



import random

class Human:
def __init__(self,name):
self.name = name
def __str__(self):
return f"{self.name}"
def __repr__(self): return str(self)

class Player(Human):
def __init__(self, name, team):
super().__init__(name)
self.team = team
def __str__(self):
return f"{self.name} in team {self.team}"
def __repr__(self): return str(self)

players = ['a', 'b', 'c','d','e', 'f']
random.shuffle(players) # inplace shuffling

player_length = len(players) // 2 # make sure you got even player counts - else uneven teams
A = [Player(k,"A") for k in players[:player_length]] # first 3 are Team A
B = [Player(k,"B") for k in players[player_length:]] # rest is Team B
teams = [A,B]

print(teams)


Output:



[[d in team A, f in team A, b in team A],[a in team B, e in team B, c in team B]]


Doku:




  • random.sample

  • random.shuffle






share|improve this answer





















  • 1





    Would like to see programmatic approach to determining the halfway index rather than hard coding the 3

    – Dan Farrell
    Jan 1 at 19:39








  • 1





    @DanFarrell trivialy done :) thx

    – Patrick Artner
    Jan 1 at 19:43














5












5








5







Your code will give you the same players:




for i in range(6):
p = Player(random.choice(players)) # you can draw duplicates here



to avoid that either use random.sample(players, k=len(players)) over your whole names (this returns a new, shuffled list - players is unmodified) or random.shuffle(players) it in-place. Afterwards you divide the result into two parts and create the teams from that data:



You should probably overload the __repr__(self) methods of your class to make outputting neater.



import random

class Human:
def __init__(self,name):
self.name = name
def __str__(self):
return f"{self.name}"
def __repr__(self): return str(self)

class Player(Human):
def __init__(self, name, team):
super().__init__(name)
self.team = team
def __str__(self):
return f"{self.name} in team {self.team}"
def __repr__(self): return str(self)

players = ['a', 'b', 'c','d','e', 'f']
random.shuffle(players) # inplace shuffling

player_length = len(players) // 2 # make sure you got even player counts - else uneven teams
A = [Player(k,"A") for k in players[:player_length]] # first 3 are Team A
B = [Player(k,"B") for k in players[player_length:]] # rest is Team B
teams = [A,B]

print(teams)


Output:



[[d in team A, f in team A, b in team A],[a in team B, e in team B, c in team B]]


Doku:




  • random.sample

  • random.shuffle






share|improve this answer















Your code will give you the same players:




for i in range(6):
p = Player(random.choice(players)) # you can draw duplicates here



to avoid that either use random.sample(players, k=len(players)) over your whole names (this returns a new, shuffled list - players is unmodified) or random.shuffle(players) it in-place. Afterwards you divide the result into two parts and create the teams from that data:



You should probably overload the __repr__(self) methods of your class to make outputting neater.



import random

class Human:
def __init__(self,name):
self.name = name
def __str__(self):
return f"{self.name}"
def __repr__(self): return str(self)

class Player(Human):
def __init__(self, name, team):
super().__init__(name)
self.team = team
def __str__(self):
return f"{self.name} in team {self.team}"
def __repr__(self): return str(self)

players = ['a', 'b', 'c','d','e', 'f']
random.shuffle(players) # inplace shuffling

player_length = len(players) // 2 # make sure you got even player counts - else uneven teams
A = [Player(k,"A") for k in players[:player_length]] # first 3 are Team A
B = [Player(k,"B") for k in players[player_length:]] # rest is Team B
teams = [A,B]

print(teams)


Output:



[[d in team A, f in team A, b in team A],[a in team B, e in team B, c in team B]]


Doku:




  • random.sample

  • random.shuffle







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 19:40

























answered Jan 1 at 19:28









Patrick ArtnerPatrick Artner

24.8k62443




24.8k62443








  • 1





    Would like to see programmatic approach to determining the halfway index rather than hard coding the 3

    – Dan Farrell
    Jan 1 at 19:39








  • 1





    @DanFarrell trivialy done :) thx

    – Patrick Artner
    Jan 1 at 19:43














  • 1





    Would like to see programmatic approach to determining the halfway index rather than hard coding the 3

    – Dan Farrell
    Jan 1 at 19:39








  • 1





    @DanFarrell trivialy done :) thx

    – Patrick Artner
    Jan 1 at 19:43








1




1





Would like to see programmatic approach to determining the halfway index rather than hard coding the 3

– Dan Farrell
Jan 1 at 19:39







Would like to see programmatic approach to determining the halfway index rather than hard coding the 3

– Dan Farrell
Jan 1 at 19:39






1




1





@DanFarrell trivialy done :) thx

– Patrick Artner
Jan 1 at 19:43





@DanFarrell trivialy done :) thx

– Patrick Artner
Jan 1 at 19:43




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998237%2fdivide-items-between-two-lists-randomly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas