0

How do i choose to pick random choice from the list given below.

colours = ['red', 'blue', 'green', 'yellow', 'black', 'purple', 'Brown', 'Orange', 'violet', 'gray'] 
  1. now pick 1 item from above 10 items list.
  2. print

  3. pick 2 items from remaining 9 items.

  4. print

  5. finally pick 3 items from remaining 7 items.

  6. print
  7. So final result will be like this

Brown

green and violet

red black and gray

3 Answers 3

2

A simple way would just be to delete the chosen values from the list. It is slightly simpler if you use sets:

In []: colours = {'red', 'blue', 'green', 'yellow', 'black', 'purple', 'Brown', 'Orange', 'violet', 'gray'} for n in [1, 2, 3]: cs = random.sample(colours, k=n) colours -= set(cs) print(cs) Out[]: ['Brown'] ['Orange', 'red'] ['purple', 'gray', 'blue'] 
Sign up to request clarification or add additional context in comments.

2 Comments

@Demotry what you ask in your comment is obviosly another question! Please judge the (good) answers you have received with respect to the question you actually asked and move on to ask another question for your "discordation" problem
yes but if i create another question it to work that in discord does it refers to duplicate question ?
0
colors = ['red', 'blue', 'green', 'yellow', 'black', 'purple','Brown', 'Orange', 'violet', 'gray'] for n in range(1,4): select=np.random.choice(colors,n) print(select) colors=list(set(colors).difference(set(select))) output:-['Brown'] ['red' 'violet'] ['yellow' 'Orange' 'black'] 

2 Comments

@Demotry i am beginner,I dont have any idea about your question.
Its ok. I got answer from Tristo.
0

The method I use consist in shuffling your input vector take the selected number of elements you need.

import random colours = ['red', 'blue', 'green', 'yellow', 'black', 'purple', 'Brown', 'Orange', 'violet', 'gray'] random.shuffle(colours) for i in range(1, 4): n, colours = colours[0:i], colours[i:] print(n) 

1 Comment

No need to re-shuffle each time. Shuffle once.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.