0

I am looping through a list of tuples and I want to add a conditional so that if the condition has multiple successes only one of them is chosen with an equal percentage of chance.

In [1]:
junctions = ([1,2], [1,3], [2,3])
roads = ("a", "b", "c", "d")
for a, b in junctions:
  if a == 1:
    print(roads[b])
Out [1]:
c
d

In this example, I would want it to return either c or d with a 50% chance. Is this possible?

PharmDataSci
  • 115
  • 7
  • 2
    Do you want to randomly select items *while looping*, or just select all items and *then* randomly reduce items with the same key? – MisterMiyagi Jan 26 '20 at 16:20
  • "Do you want to randomly select items while looping, or just select all items and then randomly reduce items with the same key?" I think it makes more sense to randomly select them while looping? I am going to use the result of this loop to select from another list. i.e. instead of print[b] I would be using newlist[b] = "New Value" I hope that makes sense? – PharmDataSci Jan 26 '20 at 16:31
  • 1
    Is your data restricted/structured in any way to make an early decision while looping? E.g. is it sorted by the primary key/condition, is the number of successes known ahead of time? Do you have more than one condition? Is your data reasonably small to fit in memory? – MisterMiyagi Jan 26 '20 at 16:38
  • 1
    @ScientistStats Why would it make more sense to do it while looping? It's possible and fairly easy, but selecting them afterwards is *trivial*. – Kelly Bundy Jan 26 '20 at 16:39
  • What is the issue, exactly? There are plenty of resources on how to select items from a list. – AMC Jan 26 '20 at 21:10
  • 1
    Does this answer your question? [How to randomly select an item from a list?](https://stackoverflow.com/questions/306400/how-to-randomly-select-an-item-from-a-list) – AMC Jan 26 '20 at 21:11

2 Answers2

3

You may try this:

from random import choice

possibilities = []
junctions = ([1,2], [1,3], [2,3])
roads = ("a", "b", "c", "d")
for a, b in junctions:
  if a == 1:
     possibilities.append(roads[b])

print(choice(possibilities))
lenik
  • 23,228
  • 4
  • 34
  • 43
  • 1
    (Not asked, but the loop can be avoided with a plain list comprehension doing exactly the same thing: `possibilities = [roads[b] for a,b in junctions if a==1]`) – Jongware Jan 26 '20 at 16:36
  • 1
    @usr2564301 i usually try to make the smallest possible change to the OP code, makes it easier to understand the answer and figure out why the changes were necessary. – lenik Jan 26 '20 at 17:29
  • Yep `:)` That's why I only commented, it'd not add anything new to your solution. – Jongware Jan 26 '20 at 17:32
-1

You could append these to a new list, and use random.randint() to pick from the size of the new list

import random
sampleList = []
x = random.randint(0,len(sampleList))
print(sampleList[x])
Llione
  • 49
  • 5
  • 5
    The ``random`` module already provides means to pick from a list. There is no need to wrongly reimplement it (your version may pick an index one after the end of the list). – MisterMiyagi Jan 26 '20 at 16:40