python - Duplicate strings in nested lists -
set - have list nested lists (call relay sets) looks this:
relay_sets = [ [[80100, 'a']], [[75000, 'b']], [[64555, 'c']], [[55000, 'd'], [44000, 'e']], [[39000, 'f'], [2000, 'g']], [[2000, 'h'], [999, 'i'], [999, 'j']], [[999, 'k'], [999, 'l'], [343, 'm']] ]
a user put in 1 of relay sets (with uniform probability) , again uses 1 of nested lists in sublist equal probability. example user has 1/7 chance of being put in [[80100, 'a']]
guaranteed use [80100, 'a']
, whereas user has 1/7 chance of being put in [[55000, 'd'], [44000, 'e']]
, has 1/2 chance of using [55000, 'd']
.
i wrote script finds probability of using relay:
def prob_using_relay(relay, relaysets): prob1 = 1 / float(len(relaysets)) index, item in enumerate(relaysets): in item: if relay in i: num_relays_in_set = len(relaysets[index]) prob2 = 1 / float(num_relays_in_set) total_prob = float(prob1 * prob2) return total_prob print prob_using_relay(80100, relay_sets) # gives correct answer of 0.142857142857
my problem don't know how account duplicates. if wanted know chance of using relay 999 bandwidth answer should (2/3 * 1/7 + 2/3 * 1/7)
script gives 1/21
. or if wanted know chance of using relay 2000 bandwidth answer should (1/2 * 1/7 + 1/3 * 1/7)
instead of (1/2 * 1/7)
.
could altering function account duplicates?
there lots of problems code. doing useless casts floats. being can't handle probabilities more 2 levels deep. here's recursive approach can handle this:
relay_sets = [ [[80100, 'a']], [[75000, 'b']], [[64555, 'c']], [[55000, 'd'], [44000, 'e']], [[39000, 'f'], [2000, 'g']], [[2000, 'h'], [999, 'i'], [999, 'j']], [[999, 'k'], [999, 'l'], [343, 'm']] ] def prob_using_relay(relay, relaysets): if isinstance(relaysets[0], int): return int(relaysets[0] == relay) return sum(prob_using_relay(relay, relayset) relayset in relaysets) / len(relaysets) print(prob_using_relay(80100, relay_sets))
Comments
Post a Comment