List Repetition

List Repetition

VickiLanger

Asks "why?" too much
Gold Member
Local time
00:00
Joined
Oct 7, 2019
Messages
189
Pronouns
she/her

I’ve been taking a beginner python class to help cement what I already know and fill in the gaps of what I don’t know.

Tonight, we learned about data structures. The instructor mentioned that you can multiply lists. Like, some_list * 5

Can someonehelp me out with an example of how you could use this?

 

Gummibeer

Astroneer
Moderator
Local time
05:00
Joined
Oct 5, 2019
Messages
1,161
Pronouns
he/him

For me it seems that this isn't a plain python list but numpy.


The result will be:
Python:
list = [1, 2, 3]
result = list * 2

# result = [2, 4, 6]

 

VickiLanger

Asks "why?" too much
Gold Member
Local time
00:00
Joined
Oct 7, 2019
Messages
189
Pronouns
she/her

For me it seems that this isn't a plain python list but numpy.

The result will be:
Python:
list = [1, 2, 3]
result = list * 2

# result = [2, 4, 6]
numpy would give that result, but plain python should make the list repeat.

Python:
numbers = [0, 1, 2] * 3 # prints [0, 1, 2, 0, 1, 2, 0, 1, 2]

 

Gummibeer

Astroneer
Moderator
Local time
05:00
Joined
Oct 5, 2019
Messages
1,161
Pronouns
he/him

numpy would give that result, but plain python should make the list repeat.

Python:
numbers = [0, 1, 2] * 3 # prints [0, 1, 2, 0, 1, 2, 0, 1, 2]
Why should I want this? 😅 Really, I NEVER had the case, in my whole 13 years, to repeat a list. 🤔🤯

 

Gummibeer

Astroneer
Moderator
Local time
05:00
Joined
Oct 5, 2019
Messages
1,161
Pronouns
he/him

Yeah I can't think why you would want this. Lots of things are possible without being useful I suppose.
Okay, i found a case when this could be useful - weighted random list retrieve.

Python:
common * 5 + epic * 2 + legendary
No idea if this code works, but this kind of faked weighted randomness is the only case I could imagine.^^

 

Adam

Mr. Webwide
Administrator
Local time
04:00
Joined
Sep 24, 2019
Messages
1,252
Pronouns
he/him

Okay, i found a case when this could be useful - weighted random list retrieve.

Python:
common * 5 + epic * 2 + legendary
No idea if this code works, but this kind of faked weighted randomness is the only case I could imagine.^^
Very clever. 🧙‍♂️

 
Last edited:

VickiLanger

Asks "why?" too much
Gold Member
Local time
00:00
Joined
Oct 7, 2019
Messages
189
Pronouns
she/her

Yeah I can't think why you would want this. Lots of things are possible without being useful I suppose.

Yeah, I suppose it's there because there may be a weird use for it, for someone.

 

VickiLanger

Asks "why?" too much
Gold Member
Local time
00:00
Joined
Oct 7, 2019
Messages
189
Pronouns
she/her

Okay, i found a case when this could be useful - weighted random list retrieve.

Python:
common * 5 + epic * 2 + legendary
No idea if this code works, but this kind of faked weighted randomness is the only case I could imagine.^^

Like characters = [common, common, common, common, common, epic, epic, legendary] ?

I think I could imagine that being potentially useful.

 

Gummibeer

Astroneer
Moderator
Local time
05:00
Joined
Oct 5, 2019
Messages
1,161
Pronouns
he/him

Like characters = [common, common, common, common, common, epic, epic, legendary] ?

I think I could imagine that being potentially useful.

Yeah, it's the most basic approach to code item drop chances. We do this in game-jams.

Python:
common = [ 'stick', 'heal-pot', 'mana-pot'  ]
epic = [ 'epic-sword', 'epic-shield' ]
legendary = [ 'deathbringer' ]

pool = common * 5 + epic * 2 + legendary

# pool = stick, stick, stick, stick, stick, heal-pot, heal-pot, heal-pot, heal-pot, heal-pot, mana-pot, mana-pot, mana-pot, mana-pot, mana-pot, epic-sword, epic-sword, epic-shield, epic-shield, deathbringer

item = pool.shuffle().random()

shuffle() will mix all items in list and random() will take a random item from list.
This way you don't have to care about really complex random algorithms and it's pretty readable.

 

VickiLanger

Asks "why?" too much
Gold Member
Local time
00:00
Joined
Oct 7, 2019
Messages
189
Pronouns
she/her

Yeah, it's the most basic approach to code item drop chances. We do this in game-jams.

Python:
common = [ 'stick', 'heal-pot', 'mana-pot'  ]
epic = [ 'epic-sword', 'epic-shield' ]
legendary = [ 'deathbringer' ]

pool = common * 5 + epic * 2 + legendary

# pool = stick, stick, stick, stick, stick, heal-pot, heal-pot, heal-pot, heal-pot, heal-pot, mana-pot, mana-pot, mana-pot, mana-pot, mana-pot, epic-sword, epic-sword, epic-shield, epic-shield, deathbringer

item = pool.shuffle().random()

shuffle() will mix all items in list and random() will take a random item from list.
This way you don't have to care about really complex random algorithms and it's pretty readable.

That seems like a totally legitimate use case for this odd list repeating ability. I like it.

 

Gummibeer

Astroneer
Moderator
Local time
05:00
Joined
Oct 5, 2019
Messages
1,161
Pronouns
he/him

That seems like a totally legitimate use case for this odd list repeating ability. I like it.
We do it another way to have more control - we use an array:
PHP:
$items = [
    'stick' => 6,
    'heal-pot' => 5,
    'mana-pot' => 4,
    'shield' => 2,
    'sword' => 3,
    'deathbringer' => 1,
    'clothing' => 4,
];

$pool = [];
foreach($items as $item => $amount) {
    $pool = $pool + array_fill(0, $amount, $item);
}

 
Top