如何从配对列表中获取python上的随机元素?

时间:2019-12-27 05:34:52

标签: python list random

我试图比较跌落高度与反弹高度,并在此处得到一些数据:

drop_heights = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.7, 2.0]
rebound_heights = [0.16, 0.30, 0.46, 0.6, 0.74, 0.88, 1.02, 1.15, 1.34, 1.51]

我想从这些变量中选择5个随机数据点,所以我尝试了

smol_drop_heights = []
smol_rebound_heights = []

for each in range(0,5):
     smol_drop_heights.append(drop_heights[randint(0, 9)])
     smol_rebound_heights.append(rebound_heights[randint(0, 9)])
print(smol_drop_heights)
print(smol_rebound_heights)

当他们打印时,他们打印不同的数据集,有时甚至重复数据,我该如何解决?

[0.8, 1.6, 0.6, 0.2, 0.12]
[1.02, 1.15, 0.88, 0.88, 0.6]

这是示例输出,您可以看到重复的.88。

7 个答案:

答案 0 :(得分:2)

一种方法是:

drop_heights = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.7, 2.0]
rebound_heights = [0.16, 0.30, 0.46, 0.6, 0.74, 0.88, 1.02, 1.15, 1.34, 1.51]
indices = [*range(len(drop_heights))]
from random import shuffle
shuffle(indices)
smol_drop_heights = []
smol_rebound_heights = []
for each in indices:
    smol_drop_heights.append(drop_heights[each])
    smol_rebound_heights.append(rebound_heights[each])
print(smol_drop_heights)
print(smol_rebound_heights)

输出:

[1.7, 0.8, 1.6, 1.2, 0.2, 0.4, 1.4, 2.0, 1.0, 0.6]
[1.34, 0.6, 1.15, 0.88, 0.16, 0.3, 1.02, 1.51, 0.74, 0.46]

或更短:

from random import sample

drop_heights = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.7, 2.0]
rebound_heights = [0.16, 0.30, 0.46, 0.6, 0.74, 0.88, 1.02, 1.15, 1.34, 1.51]

paired = [*zip(drop_heights, rebound_heights)]
smol_drop_heights, smol_rebound_heights = zip(*sample(paired,5))

print(smol_drop_heights[:5])
print(smol_rebound_heights[:5])

答案 1 :(得分:2)

一种避免重复并保持数据点成对并随机对的方式:

from random import random

drop_heights = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.7, 2.0]
rebound_heights = [0.16, 0.30, 0.46, 0.6, 0.74, 0.88, 1.02, 1.15, 1.34, 1.51]

pairs = list(sorted(zip(drop_heights, rebound_heights), key=lambda _: random()))[:5]

smol_drop_heights = [d for d, _ in pairs]
smol_rebound_heights = [r for _, r in pairs]

答案 2 :(得分:2)

这就是我要做的。

import random
import numpy as np
k=5
drop_heights = np.array([0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.7, 2.0])
rebound_heights = np.array([0.16, 0.30, 0.46, 0.6, 0.74, 0.88, 1.02, 1.15, 1.34, 1.51])
idx = random.sample(range(len(drop_heights )), k)

print(drop_heights[idx])
print(rebound_heights [idx])

答案 3 :(得分:1)

您的问题是,当您致电randint时,每次都会给它一个不同的随机数。为了解决这个问题,您需要在每次代码循环时将索引变量保存为一个随机数,以便每次添加相同的随机变量。

for each in range(0, 4):  
    index = randint(0, 9)  
    smol_drop_heights.append(drop_heights[index])  
    smol_rebound_heights.append(rebound_heights[index])  
print(smol_drop_heights)  
print(smol_rebound_heights)

要解决有关重复的问题,只需检查列表中是否已包含要添加的变量,就可以对其中一个变量进行操作,因为它们都没有重复,并且由于可能存在重复,因此循环是不够的,因此您将不得不重复执行,直到列表已满。 所以我的最终解决方案是:

while True:  
    index = randint(0, 9)  
    if drop_heights[index] not in smol_drop_heights:  
        smol_drop_heights.append(drop_heights[index])  
        smol_rebound_heights.append(rebound_heights[index])  
    if len(smol_drop_heights) == 4:  
        break  
print(smol_drop_heights)  
print(smol_rebound_heights)

由于您可能想按顺序排列这些值,因此可以这样做:


smol_drop_heights = []  
smol_rebound_heights = []  

while True:  
    index = randint(0, 9)  
    if drop_heights[index] not in smol_drop_heights:  
        smol_drop_heights.append(drop_heights[index])  
        smol_rebound_heights.append(rebound_heights[index])  
    if len(smol_drop_heights) == 4:  
        smol_drop_heights.sort()  
        smol_rebound_heights.sort()  
        break  
print(smol_drop_heights)  
print(smol_rebound_heights)

答案 4 :(得分:1)

您可以尝试改组,然后使用原始项目的index,例如

>>> drop_heights = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.7, 2.0]
>>> rebound_heights = [0.16, 0.30, 0.46, 0.6, 0.74, 0.88, 1.02, 1.15, 1.34, 1.51]
>>> 
>>> import random 
>>> d = drop_heights[:] # keep a copy to get index for making pairs later
>>> random.shuffle(drop_heights)
>>> # iterate through the new list and get the index of the item 
>>> # from the original lists
>>> nd, nr = zip(*[(x,rebound_heights[d.index(x)]) for x in drop_heights])
>>> nd[:5]
(1.4, 0.6, 1.7, 0.2, 1.0)
>>> nr[:5]
(1.02, 0.46, 1.34, 0.16, 0.74)

或仅使用operator.itemgetterrandom.sample

>>> drop_heights = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.7, 2.0]
>>> rebound_heights = [0.16, 0.30, 0.46, 0.6, 0.74, 0.88, 1.02, 1.15, 1.34, 1.51]
>>> 
>>> import random, operator
>>> indexes = random.sample(range(len(drop_heights)), 5)
>>> indexes
[5, 0, 4, 7, 3]
>>> f = operator.itemgetter(*indexes)
>>> f(drop_heights)
(1.2, 0.2, 1.0, 1.6, 0.8)
>>> f(rebound_heights)
(0.88, 0.16, 0.74, 1.15, 0.6)

答案 5 :(得分:0)

好吧,您想做两件事,将您的清单配对。惯用的方法是使用zip

drop_heights = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.7, 2.0]
rebound_heights = [0.16, 0.30, 0.46, 0.6, 0.74, 0.88, 1.02, 1.15, 1.34, 1.51]
paired = list(zip(drop_heights, rebound_heights))

然后,您要从中采样五对。因此,请使用random.sample

sampled = random.sample(paired, 5)

最后,如果您需要将它们放在单独的列表中(可能不需要,但是如果必须),可以像下面这样解压:

smol_drop_heights, smol_rebound_heights = zip(*sampled)

您实际上可以一次全部执行此操作,尽管它可能会变得有点难以理解:

smol_drop_heights, smol_rebound_heights = zip(*random.sample(list(zip(drop_heights, rebound_heights)), 5))

答案 6 :(得分:-1)

您如何尝试使用集合,然后使用.intersect方法获取常见的集合呢?