Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- all_data = [1, 2, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 15]
- >>> random.sample(range(1, 16), 3)
- [11, 10, 2]
- population = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]
- population = set(population)
- samples = random.sample(population, 3)
- all_data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
- from random import shuffle
- shuffle(all_data)
- res = all_data[:3]# or any other number of items
- from random import sample
- number_of_items = 4
- sample(all_data, number_of_items)
- all_data = list(set(all_data))
- shuffle(all_data)
- res = all_data[:3]# or any other number of items
- import random
- L = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
- random.sample(set(L), x) # where x is the number of samples that you want
- all_data = [1,2,2,3,4,5,6,7,8,8,9,10,11,11,12,13,14,15,15]
- choices = []
- while len(choices) < 3:
- selection = random.choice(all_data)
- if selection not in choices:
- choices.append(selection)
- print choices
- all_data = [1,2,2,3,4,5,6,7,8,8,9,10,11,11,12,13,14,15,15]
- # Remove duplicates
- unique_data = set(all_data)
- # Generate a list of combinations of three elements
- list_of_three = list(itertools.combinations(unique_data, 3))
- # Shuffle the list of combinations of three elements
- random.shuffle(list_of_three)
- [(2, 5, 15), (11, 13, 15), (3, 10, 15), (1, 6, 9), (1, 7, 8), ...]
- import random
- fruits_in_store = ['apple','mango','orange','pineapple','fig','grapes','guava','litchi','almond']
- print('items available in store :')
- print(fruits_in_store)
- my_cart = []
- for i in range(4):
- #selecting a random index
- temp = int(random.random()*len(fruits_in_store))
- # adding element at random index to new list
- my_cart.append(fruits_in_store[temp])
- # removing the add element from original list
- fruits_in_store.pop(temp)
- print('items successfully added to cart:')
- print(my_cart)
- items available in store :
- ['apple', 'mango', 'orange', 'pineapple', 'fig', 'grapes', 'guava', 'litchi', 'almond']
- items successfully added to cart:
- ['orange', 'pineapple', 'mango', 'almond']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement