This question already has answers here:
Selecting a random list element of length n in Python
(4 answers)
How do you pick "x" number of unique numbers from a list in Python?
(7 answers)
Closed 5 years ago.
Let's say i have a list. I want to iterate over that list and add 2 random strings from it to an empty list. However, because this is a random choice, there is a possibility that the same string will be picked twice (As it is crucial not to pop or delete from that list the selected item). something like this:
import random
emptylist = []
somelist = ["a","b","c","d","e",]
for item in somelist:
emptylist.append(random.choice(somelist))
emptylist.append(random.choice(somelist))
How do i make sure that it won't pick, for example, "a" twice?
I know it is possible in many ways but im looking for the most efficient one.
Related
This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 3 months ago.
Given the following dictionaries:
dict_first_attempt = {'Offense': ['Jack','Jill','Tim'],
'Defense':['Robert','Kevin','Sam']}
dict_second_attempt = {'Offense': ['Jack','McKayla','Heather'],
'Defense':['Chris','Tim','Julia']}
From this dictionaries, my focus is just the offense, so if I just wanted the list of those, I would do this:
first = dict_first_attempt['Offense']
second = dict_second_attempt['Offense']
For each of those lists, I am trying to create a code that can do the following:
Tell me all the possible combinations of first attempt offense and second attempt offense.
Outputs it in a list, with lists of the combinations.
The first element within the list has to be from the first attempt offense, and the second element has to be from the second attempt offense.
An example of the type of output I want is:
[['Jack','Jack'],['Jack','McKayla'],['Jack','Heather'],
['Jill','Jack'],['Jill','McKayla'],['Jill','Heather'],
['Tim','Jack'],['Tim','McKayla'],['Tim','Heather']]
import itertools
list(itertools.product(first, second))
This question already has answers here:
Nested List and count()
(8 answers)
Closed 4 years ago.
I have a list with one list inside and I would like to count how many times is one element repeat. for example:
list = ['a','b','c',['a','d']]
find = 'a'
list.count(find)
The ouptput is 1, but I'm looking for 2.
There is any easy way to do it?
thanks
Archive with chain.from_iterable
from itertools import chain
print(list(chain.from_iterable(lst)).count('a'))
First make your list flatten and get the count.
This question already has answers here:
How can I randomly select an item from a list?
(17 answers)
Randomly select x number of items from class list in python
(3 answers)
Closed 4 years ago.
I need to generate a random amount of string for a certain amount of times;
so for example:
for k in range(5):
print("Country", location)
print("Animal", animal)
so location and animal will be randomly generated from a list, and each of the 5 times it gets printed, it will have random number of locations and animal, so for every iteration a random number of locations and animals will be printed from a given list
This question already has answers here:
Even numbers a list?
(9 answers)
Closed 5 years ago.
If I have a list of random ints and then check each individual value of it being even or odd, how would I do that?
for i in list:
if i % 2 == 0:
print('even!')
else:
print('odd!')
What I'm doing here is using a for loop to go through the list. For each element in the list, i is assigned to be the current element, then I check if i is divisible by 2 (i.e. if i is even). If it is, I print that the element is even, and if not, then I print that the element is odd.
This question already has answers here:
Generate 'n' unique random numbers within a range [duplicate]
(4 answers)
Closed 7 years ago.
I have a list that looks like this:
l = [random.randint(0,9),random.randint(0,9),random.randint(0,9),random.randint(0,9)]
but if it outputs something like this[9,0,5,5] what can I do replace the repeating integer?
If the goal is to get four unique items in random order, let Python do the work for you:
l = random.sample(range(10), 4)
random.sample is intended specifically for "random sampling without replacement", which appears to be the goal of your code.