0

I need to create 3 lists from my list1. One with 70% of the values and two with 20% and 10%.

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# listOutput70 = select  70% of list1 items(randomly)
# with the remaining create two lists of 20% and 10%

#the output can be something like:

#listOutput70 = [2,7,9,8,4,10,3]
#listOutput20 = [1,5]
#listOutput10 = [6]

I already have some code to generate a percentage output, but works only for one list.

import random


def selector():

    RandomSelection = []
    mySel = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    PercentEnter= 70
    
    New_sel_nb = len(mySel)*int(PercentEnter)/100
        
    
    while len(RandomSelection) < New_sel_nb:
        randomNumber = random.randrange(0, len(mySel),1)
    
        RandomSelection.append(mySel[randomNumber])
    
        RandomSelection = list(set(RandomSelection))
        
    print(RandomSelection)


selector()
#[2, 3, 6, 7, 8, 9, 10]
5
  • 2
    What's the point of PercentEnter= PercentEnter? Commented Jul 7, 2022 at 20:43
  • Why don't you use the same code 2 more times? Commented Jul 7, 2022 at 20:45
  • it is unclear what you want, because the code you have seems to work as it is, do you want to make a random partitioning of the list into lists of length 1, 2, and 7? Commented Jul 7, 2022 at 20:47
  • @Barmar, I changed the code to be more consistent. Commented Jul 7, 2022 at 20:47
  • @L.Grozinger, I needed it to be in percentage so I can change them later if needed. basically output 3 lists, one with 70% of the values, another one with 20% and the last one with 10%. The code I have only works for one list. Then if I run the function again it will select some of the same elements. Commented Jul 7, 2022 at 20:51

2 Answers 2

3

Shuffle the list with random.shuffle(). Then use slices to get each percentage.

def selector(percents):
    RandomSelection = []
    mySel = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    random.shuffle(mySel)
    
    start = 0
    for cur in percents:
        end = start + cur * len(mySel) // 100
        RandomSelection.append(mySel[start:end])
        start = end

    return RandomSelection

print(selector([70, 20, 10]))
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but I am getting an error: # SyntaxError: unexpected EOF while parsing #
Sorry, missing ) at the end.
@DanielHao If you do that, you need to remove the choices from the list between each iteration.
1

Using numpy.split function:

from random import shuffle
from numpy import split as np_split


def selector(og_list, percentages):
    if sum(percentages) != 100:
        raise ValueError("Percentages must sum to 100!")
    percentages.sort()
    splits = [round(len(og_list) * percentages[0] / 100),
              round(len(og_list) * (percentages[0] + percentages[1]) / 100)]
    shuffle(og_list)
    return [list(subset) for subset in np_split(og_list, splits)]

Usage:

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

my_percentages_1 = [10, 20, 70]
my_percentages_2 = [40, 10, 50]
my_percentages_3 = [63, 31, 6]

result_1 = selector(my_list, my_percentages_1)
result_2 = selector(my_list, my_percentages_2)
result_3 = selector(my_list, my_percentages_3)

print(result_1)
print(result_2)
print(result_3)
[[2], [8, 3], [4, 9, 7, 5, 0, 1, 6]]
[[8], [0, 2, 7, 1], [9, 4, 6, 3, 5]]
[[1], [0, 3, 2], [4, 8, 5, 9, 7, 6]]

3 Comments

The question is about splitting the list into multiple segments: 70%, 20%, 10%. Your code only works for one, just like the OP's code.
Thanks, but this only outputs one list, I need 3. One with 70%, and with the remaining items one with 20% and another with 10.
Oh, let me edit my code to comply with OP's needs and I'll update it right away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.