如何解决生成一个包含 100 个元素的列表,每个元素有 50% 的机会是 0,50% 的机会是 0 到 1 之间的随机数
我在这方面很新,我正在尝试自己学习。正如我在标题中所说,我试图创建一个包含 100 个数字的列表,其元素要么有 50% 的机会是 0,要么有 50% 的变化是介于 0 和 1 之间的数字。我把它做成了下面的那个。它有效,但它是一个非常乏味且编码不佳的程序。任何关于如何使它变得更好的提示?
import random
import numpy as np
#define a list of 100 random numbers between 0 and 1
randomlist = []
for i in range(0,100):
n = random.uniform(0,1)
randomlist.append(n)
print(randomlist)
#create a list of 100 numbers of 0's and 1's
def random_binary_string(length):
sample_values = '01' # pool of strings
result_str = ''.join((random.choice(sample_values) for i in range(length)))
return (result_str)
l=100
x=random_binary_string(l)
x1=np.array(list(map(int,x)))
print(x1)
#combine both lists. Keep value if of the binary list if it is equal to zero. Else,substitute it by the value of randomlist
#corresponding to the index position
finalist=[]
for i in range(len(x1)):
if x1[i]==0:
finalist.append(x1[i])
else:
finalist.append(randomlist[i])
print(finalist)
非常感谢!
解决方法
您可以通过嵌套这两个条件来简化代码。这避免了需要在内存中保留两个单独的列表,然后在最后合并它们。
randomlist = []
for i in range(0,100):
if random.choice((0,1)) == 1:
randomlist.append(random.uniform(0,1))
else:
randomlist.append(0)
这足够简单和简洁,您可以将其重构为单个列表推导式。这更紧凑,但不太清晰。
randomlist = [random.uniform(0,1) if random.choice((0,1)) else 0 for i in range(0,100)]
在这里,我们还利用 Python 中 0 是 falsey 和 1 是 truthy 的事实稍微缩短了代码;即它们在布尔上下文中分别评估为 False
和 True
。所以 if random.choice((0,1)) == 1
可以简写为 if random.choice((0,1))
。
有点晦涩,您可以通过观察表达式 B if not A else A
可以短路为表达式 A and B
来进一步简化(从某种意义上说,使用更少的代码)。如果您对布尔逻辑不是很熟悉,这不是很明显,但我认为您可以在纸上解决。
randomlist = [random.choice((0,1)) and random.uniform(0,1) for i in range(0,100)]
,
你可以尝试做这样的事情:
A <- c(35,35,2609,917,0)
B <- c(8,6,9,24,27,35)
C <- c(1,45,91,24)
D <- c(927,38,22,9)
E <- c(6361,7,43)
my.list <- list(A,B,C,D,E)
group1 <- c(1,2)
group2 <- c(3,5)
remove <- c(35,6361)
my.list[group1] <- my.list[group1] %>% subset(.,!.%in% remove)
my.list
###final expected output
my.list
[[1]]
[1] 2609 917 0
[[2]]
[1] 8 6 9 27
[[3]]
[1] 1 45 91 24
[[4]]
[1] 927 38 22 9
[[5]]
[1] 6361 7 43
,
我提出这个方法:
- 首先用random.choice生成一个'A'和'B'的随机列表。 50% 'A' 和 50% 'B'
- 然后用 0 到 1 之间的随机数替换 'A'
- 并将“B”替换为 0
代码在这里:
import random
ll = [ random.choice(['A','B']) for x in range(200)]
print(ll,len(ll))
for i in range(len(ll)):
if ll[i] == 'A':
ll[i]=random.random()
else:
ll[i]=0
print(ll,len(ll))
这里的代码更短:
import random
ll = [ random.choice([0,random.random()]) for x in range(200)]
print(ll,len(ll),ll.count(0))
,
由于您使用的是 Numpy,我可能会执行以下操作:
- 使用 random.uniform 创建
num_el
元素的数组- 考虑排除上限的问题:
[low,high)
- 考虑排除上限的问题:
- 创建一个布尔矩阵,其真假概率为
p=0.5
:random.choice - 使用矩阵将数组的某些元素设置为零,通过
代码如下:
num_el = 10
p = 0.5
res = np.random.uniform(0.,1.,size=(1,num_el))
bool_mat = np.random.choice(a=[False,True],num_el),p=[p,1-p])
res[bool_mat] = 0.
res
# array([[0.,0.51213168,0.,0.68230528,0.5287728,# 0.9072587,0.43078057,0.89735872,0. ]])
,
使用的方法取决于您的目标是让恰好一半的结果为零,还是将预期的零数设为总数的一半。从您的问题中不清楚您是如何看待问题的,因此我已将这两种方法都实现为函数。
如果您想要零/非零的确定性固定比例,下面代码中的第一个函数可以解决问题。它创建一个包含所需数量的零和非零的列表,然后使用改组(我计时比采样快)。如果你想要一半,那么显然参数 n
必须是偶数。
如果您的目标是概率为 50% 的零,请使用第二个函数。
import random
# Exactly floor(n / 2) outcomes are zeros,i.e.,exactly half when n is even.
# This version is trivial to modify to give any desired proportion of zeros.
def make_rand_list_v1(n = 100):
m = n // 2
n -= m
ary = [random.random() for _ in range(n)] + [0] * m
random.shuffle(ary)
return ary
# Each outcome has probability 0.5 of being zero
def make_rand_list_v2(n = 100):
return [random.getrandbits(1) and random.uniform(0,1) for _ in range(n)]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。