# Random sampling in python3

86
April 14, 2022, at 08:40 AM

Suppose I have two lists A = [1,2,3,4] and B=[a,b,c,d]. I have to do random sampling such that my new list could look like this C= [1 or a , 2 or b , 3 or c , 4 or d], there is also a constrain that that the list C should have 50 % data from both A and B.
Input:

``````A = [1,2,3,4]
B = ['a','b','c','d']
``````

You could randomly `sample` the indices to ensure having half and half:

``````A = [1,2,3,4]
B = ['a','b','c','d']
import random
idx = set(random.sample(range(len(A)), len(A)//2))
C = [a if i in idx else b for i,(a,b) in enumerate(zip(A, B))]
``````

Example output: `['a', 2, 3, 'd']`

POPULAR ONLINE

### Autoclicker that clicks left click and uses left click as the hot key

This seems really simple but, I realized the issue is that when pyautogui left clicks the key state is reset to up even though I may still be holding down left click

95

### Breaking down np.where with nested for-loop and if/else statement

Write Python code equivalent to the following:

117

### how to merge two lists with duplacated factors into a dict in python? [duplicate]

I know there's a way to merge lists into a dict by zip function, but once the 'key-list' contains duplicated factors it doesn't works well since many values will be cutI wonder if there is a good solution to merge lists with the same keys?

85

### Tkinter Button Command switches when new Button is used [duplicate]

I face an issue which is that my first button is using the second button's commandI have faced this logic error multiple times when trying to create buttons programmatically with different functions, is there a way to resolve this or is this a limitation...

139