Hi

I am struggling with assigning values to a list of a list.

This is my full code.

import random

#get user input as to size of range
range_size = 0
if range_size == 0:
    try:
        range_size = int(input('Size of Range?: '))
    except ValueError:
        print('That was not an integer!')
        range_size = 0

#set base value for each number in range_size
base_value = int(100 / range_size)

print(base_value)

#create list of base values
num_weighted = [base_value] * range_size
num_weighted[0] = round(base_value * 2.2, 1)
num_weighted[1] = round(base_value * 1.8, 1)
num_weighted[2] = round(base_value * 1.8, 1)
num_weighted[3] = round(base_value * 1.5, 1)
num_weighted[4] = round(base_value * 1.4, 1)
num_weighted[5] = round(base_value * 1.3, 1)

# redistribute the difference of top 6 and rest of range
top6 = (sum(num_weighted[0:6]))
not_top6 = range_size - 6
pts_alloc = round((100 - top6) / not_top6, 1)

num_weighted[6:] = [pts_alloc for i in range(len(num_weighted) - 6)]

keys = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight',
        'Nine', 'Ten', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen']

dictionary = dict(zip(keys, num_weighted))
print(dictionary)
#my_sample = random.sample(random.choice(dictionary), 3)
#print(my_sample)


#def mySamples(dictionary):
#    """Call sets of 3, 5 times"""
#total_weight = sum(dictionary.values())
#n_items = 3
#random_sample = list()

d_mod = dict(dictionary)

user_range = (range(5))
arr = [[] for _ in user_range]

for y in range(3):
    b = tuple(random.choice(d_mod))
    arr[y].append(b)
print(arr)

It's the very last bit I can't do. I am using random choice to pull my weighted values out, and I want to assign them my list. I want the amount of lists and the length of the lists to be variable so don't want to hard code the assignment.

random.choice() will return a single element. Why are you making it a tuple? If you are trying to get the key and value out, you will need to do it explicitly:
k=random.choice(d_mod); v=d_mod[k]; arr[y].append([k,v])
if that's what you really want.

Hmm, if you want to assign by value, why not just store your lists in a dictionary. IE, a dictionary of lists. Something like:

dict = {}
dict['list1'] = ['foo', 'bar']
dict['list2'] = ['baz', 'qi']

Hi

I don't want to get the values just the keys selected randomly. The values are there to created a weighted randomisation.

So I want the the lists to be results from the random.choice. I cannot figure out how to assign to nested lists.

so for example.

Dict = {'One': 3, 'Two': 5, 'Three': 1, 'Four': 4, 'Five':7 }

# so from this create  2 lists of 2 choices both with unique values
# eg [One, One] would not be valid.

while list < 2:
    while items < 2:
        newList.append(random.choice(Dict))
        item += 1
    newlist.append([])
    list += 1

print(newlist)
# hopefully get something like
[['One','Four'],['Two','One']]

But I cannot get that to happen.

Use random.sample half the size of dictionary and then find it's complement in keys for the other half.

Your code is a good example of why we use the Python Style Guide You use an upper and lower case "L" for the same field name so the code you posted does not run. The style guide says to use lower_case_with_underscores so to avoid that problem and make your code more readable by others. Try the following code and see if it does the random part. It does not matter if you have the keys in 2 lists or use [0][1] and [2][3] so it is up to you if you want to divide it into two lists or use as is. Note on the first print that the keys are in "dictionary order" not the order they were added.

import random
test_dict = {'One': 3, 'Two': 5, 'Three': 1, 'Four': 4, 'Five':7 }
dict_keys = test_dict.keys()
print dict_keys
random.shuffle(dict_keys)
print dict_keys

Edited 3 Years Ago by woooee

Comments
clear thinking

But how can I get the answered selections to become a list of lists

hopefully get something like

[['One','Four'],['Two','One']]

So that if the users choose 4 lists by 4 entries in each list that's what they'll get

Shuffling as I read it in the docs doesn't include the ability for weighted random. Which is what I am going for.

In my first code example it's the very last block where I want to create a list of lists is where I am struggling. So say the users says give me 4 lists each with 3 elements in it.

Can't get my head around how to achieve it.

I would prepare in this case cumulative sum of weights for the key and do own version of random.choice, which chooses biggest key with cumulative sum less than random value upto total of weights. Then you have to remove the value before next choice to make your own random.sample style function.

Thank you can I get clarification on

chooses biggest key with cumulative sum less than random value upto total of weights.

Specifically sum less than random value, what is that in action?

Edited 3 Years Ago by flebber: fix

This question has already been answered. Start a new discussion instead.