I have a list:
lst = ['ab', 'cd','ef', 'gh', 'ij', 'mn', 'op', 'qr', 'st', 'uv', 'wx', 'yz']
I would like to take 2 random values from this list and put them in to a new list as pairs until the original list is empty.
For example:
new_list = [('ab', 'ef'), ('ij', 'yz') exc. ] lst = []
How can I do this using a while and for loop?
I've tried using this method to generate a random pair from the list:
random_lst = random.randint(0,len(lst)-1)
However I'm not sure how to remove the values from the original lsit and then add them to the new list as pairs.
CodePudding user response:
I'm sure there are lots of ways. Here's a simple one.
lst = ['ab', 'cd','ef', 'gh', 'ij', 'mn', 'op', 'qr', 'st', 'uv', 'wx', 'yz']
result = []
random.shuffle(lst)
for i in range(0, len(lst), 2):
result.append((lst[i], lst[i 1]))
CodePudding user response:
Try this
import random
lst = ['ab', 'cd','ef', 'gh', 'ij', 'mn', 'op', 'qr', 'st', 'uv', 'wx', 'yz']
new_list = []
for i in range(len(lst)//2):
# Get a random index in the current list
idx = random.randint(0,len(lst)-1)
# Remove the respective element and store it
element1 = lst.pop(idx)
# Get another random index in the current list
idx = random.randint(0,len(lst)-1)
# Remove and store that element as well
element2 = lst.pop(idx)
# Create an entry (tuple) in the final list
new_list.append((element1, element2))
print(new_list)
The output for me is [('yz', 'ij'), ('wx', 'ab'), ('st', 'cd'), ('gh', 'uv'), ('qr', 'ef'), ('op', 'mn')]
CodePudding user response:
The sample() function from the random module is ideal for this
from random import sample as SAMPLE
lst = ['ab', 'cd','ef', 'gh', 'ij', 'mn', 'op', 'qr', 'st', 'uv', 'wx', 'yz']
output = []
while lst:
output.append(SAMPLE(lst, min(2, len(lst))))
for e in output[-1]:
lst.remove(e)
print(output)
Note:
min() function is essential for the case where the input list contains an odd number of elements
CodePudding user response:
There are a lot of approaches. With your method, you could try:
import random
lst = ['ab', 'cd','ef', 'gh', 'ij', 'mn', 'op', 'qr', 'st', 'uv', 'wx', 'yz']
random_lst = []
while lst:
random_lst.append((lst.pop(random.randint(0,len(lst)-1)), lst.pop(random.randint(0,len(lst)-1))))
print(random_lst)
A slight modification to be a bit faster would be:
import random
lst = ['ab', 'cd','ef', 'gh', 'ij', 'mn', 'op', 'qr', 'st', 'uv', 'wx', 'yz', 'xr']
random_lst = []
random.shuffle(lst)
while lst:
random_lst.append((lst.pop(), lst.pop()))
print(random_lst)
This example accounts for odd length of lists:
import random
lst = ['ab', 'cd','ef', 'gh', 'ij', 'mn', 'op', 'qr', 'st', 'uv', 'wx', 'yz', 'xr']
random_lst = []
random.shuffle(lst)
while lst:
if len(lst) == 1:
# The list is not even
random_lst.append(lst.pop())
break
random_lst.append((lst.pop(), lst.pop()))
print(random_lst)
Check out the results: https://colab.research.google.com/drive/19gwP5zPGPXXjGx1AR6VjNkXIIqa16oep#scrollTo=ySBobtc3rtsj
If you want to work with larger lists, I'll suggest checking out numpy.
CodePudding user response:
You can try something using np.random.choice
:
new_list = []
while lst:
tmp=np.random.choice(lst, 2).tolist()
for t in tmp:
lst.remove(t)
new_list.append(tmp)
print(new_list)
[['op', 'wx'], ['yz', 'cd'], ['ef', 'qr'], ['mn', 'ij'], ['uv', 'gh'], ['ab', 'st']]