随机替换列表之间的项目
Random replace items between lists
我有 2 个数据列表 list_A = [2,3,4,5,33,42,21]
和 list_B = [1,11,35,48,19]
。我有两种类型的问题。
第 1 条:如何将 list_B
中的两个项目随机替换为 list_A
进行 200 次迭代,并且创建的 200 个列表必须是唯一的。例如 List_1 = [**2**,11,35,48,**42**]
数字 2:如何替换 list_B
的前两个项目并从 list_A
中随机替换 select 例如 list_2 = [**5**,**33**,35,48,19]
import random
list_A = [2,3,4,5,33,42,21]
list_B = [1,11,35,48,19]
x = random.randrange(0,5)
i = list_A[x]
list_A[x] = list_B[x]
list_B[x] = i
print(list_A)
print(list_B)
import random
list_A = [2,3,4,5,33,42,21]
list_B = [1,11,35,48,19]
x = random.randrange(0,7)
list_B[0] = list_A[x]
list_A.remove(list_A[x])
x = random.randrange(0,6)
list_B[1] = list_A[x]
print(list_B)
正如 Subhashis Pandey 所说,这里不是提出此类问题的地方。
我有 2 个数据列表 list_A = [2,3,4,5,33,42,21]
和 list_B = [1,11,35,48,19]
。我有两种类型的问题。
第 1 条:如何将 list_B
中的两个项目随机替换为 list_A
进行 200 次迭代,并且创建的 200 个列表必须是唯一的。例如 List_1 = [**2**,11,35,48,**42**]
数字 2:如何替换 list_B
的前两个项目并从 list_A
中随机替换 select 例如 list_2 = [**5**,**33**,35,48,19]
import random
list_A = [2,3,4,5,33,42,21]
list_B = [1,11,35,48,19]
x = random.randrange(0,5)
i = list_A[x]
list_A[x] = list_B[x]
list_B[x] = i
print(list_A)
print(list_B)
import random
list_A = [2,3,4,5,33,42,21]
list_B = [1,11,35,48,19]
x = random.randrange(0,7)
list_B[0] = list_A[x]
list_A.remove(list_A[x])
x = random.randrange(0,6)
list_B[1] = list_A[x]
print(list_B)
正如 Subhashis Pandey 所说,这里不是提出此类问题的地方。