【问题标题】:Random replace items between lists [closed]列表之间的随机替换项目[关闭]
【发布时间】:2021-12-09 14:49:36
【问题描述】:

我有 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 中随机选择,例如 list_2 = [**5**,**33**,35,48,19]

【问题讨论】:

  • 欢迎来到 SO,请向我们展示您迄今为止所做的尝试。 SO 不是代码编写平台。

标签: python list random replace


【解决方案1】:
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 所说,这里不适合提出此类问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 2014-07-22
    • 2023-03-04
    • 2021-11-29
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    相关资源
    最近更新 更多