4
x_list = ['a', 'b', 'c', 'd', 'e']
t_list = ['z', 'y', 'u']

I want replace elements of t_list with elements of x_list, "randomly". I researched and I tried something but replace() didn't work for lists. For example; x_new0 = ['a', 'z', 'c', 'y', 'u], x_new1 = ['z', 'y', 'c', 'd','u']. How can I do this?

4
  • You can assign element directly, e.g., x_list[2] = t_list[0] Commented Sep 11, 2017 at 14:13
  • Is it important that a) All the elements of t_list are included in the new list, and b) That those elements are in order? Commented Sep 11, 2017 at 14:15
  • Can you please show your attempts? What have tried? What didn't work? Can you provide a minimal reproducible example of your problem. Commented Sep 11, 2017 at 14:15
  • 1
    Is this by position or value? If you had another a in x_list and it got replaced with z - do all values get replaced by z? If the length of the two lists are the same - is a straight replacement a correct answer. What if there's less elements in x than t... Do the elements of t have to appear in order in x... etc...? Commented Sep 11, 2017 at 14:26

3 Answers 3

4

You can use the random module and use the random.randint() method to generate a random number between 1 and 100 as a heads and tails coin toss to decide whether to replace the value or not and then use the random.choice() method on the 2nd list to return a random element of the list

import random

first = ['a', 'b', 'c', 'd', 'e']
second = ['z', 'y', 'u']

print(first)

for index, x in enumerate(first):
    if random.randint(0, 1):
        first[index] = random.choice(second)

print(first)

First run stdout:
>> ['a', 'b', 'c', 'd', 'e']
>> ['y', 'z', 'u', 'y', 'y']

Second run stdout:
>> ['a', 'b', 'c', 'd', 'e']
>> ['a', 'u', 'c', 'u', 'y']
Sign up to request clarification or add additional context in comments.

7 Comments

Its replacing all the elements of first list, might he don't want that @AK47
It's only replacing all of the elements if the 'heads and tails' toss returns true each time
This allows duplicate replacements
He never specified that he didnt want duplicates, and allowing for duplicates means the solution is more 'random' since theres more possible results
And also since his 2nd list is smaller than the first list
|
1

You could use numpy.random.choice to randomly select elements where you want the replacements to occur in the original list. Then zip those indexes against the values you want to use for the substitution and apply the replacements.

from numpy.random import choice

def swap_elements(x, t):
    new_x = x[:]
    for idx, value in zip(choice(range(len(x)), size=len(t), replace=False), t_list):
        new_x[idx] = value
    return new_x

Example usage

>>> x_list = ['a', 'b', 'c', 'd', 'e']
>>> t_list = ['z', 'y', 'u']
>>> swap_elements(x_list, t_list)
['y', 'u', 'z', 'd', 'e']
>>> swap_elements(x_list, t_list)
['y', 'b', 'z', 'u', 'e']
>>> swap_elements(x_list, t_list)
['y', 'b', 'z', 'u', 'e']
>>> swap_elements(x_list, t_list)
['a', 'u', 'z', 'y', 'e']

1 Comment

The t_list in swap_elements(x,t) is that supposed to be just t? or must you pass the original t_list?
0

You can choose a random index of one list, and a random index of the other list and take the value at the index of one and place it at the index of the other. For example:

from random import randint

a = [1 ,2, 3, 4]
b = [5, 6, 7, 8]

RandomIndexA = randint(0, len(a) - 1)
RandomIndexB = randint(0, len(b) - 1)

a[RandomIndexA] = b[RandomIndexB]

Alternatively, you could only calculate one random index and then randomly choose an element from the other list using random.choice(b) for example:

from random import randint, choice

a = [1 ,2, 3, 4]
b = [5, 6, 7, 8]

RandomIndex = randint(0, len(a) - 1)

a[RandomIndex] = choice(b)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.