简体   繁体   English

Python - 从其他两个列表随机生成一个新列表

[英]Python - Generating a New List Randomly From Two Other Lists

I have two lists, high and low bounds, that I would like to use as bounds to create a new list randomly between those values.我有两个列表,上限和下限,我想将它们用作边界来在这些值之间随机创建一个新列表。

LOW     HIGH
19.35   45.04
18.09   44.05
17.44   42.07
16.46   41.95
18.38   43.45
20.78   46.04
24.99   49.12
25.22   50.94
24.67   49.32

I am trying to create a new list that has the length of list but has random values between each LOW and HIGH pair.我正在尝试创建一个具有列表长度但在每个 LOW 和 HIGH 对之间具有随机值的新列表。

A new list might look like:新列表可能如下所示:

NEW LIST
38.26
33.70
18.47
41.35
36.77
21.95
25.23
29.18
49.32

Any help is appreciated, thanks!任何帮助表示赞赏,谢谢!

So far this is what I have but it doesn't step down a row to update the lower and upper bounds:到目前为止,这就是我所拥有的,但它并没有降级来更新下限和上限:

Low = pd.Series(data['LOW']).values
High = pd.Series(data['HIGH']).values

newlist = []

for i in range(0, 8):
        newlist = random.uniform(Low, High)

All hail the power of broadcasting!所有人都为广播的力量欢呼!

import pandas as pd
import random
import numpy as np

# example data
lows = pd.Series(np.random.uniform(0, 50, 10))
highs = pd.Series(np.random.uniform(0, 50, 10))

nums_arr = random.uniform(low=lows, high=highs)

I especially like this solution because it directly creates a Series, no conversions necessary.我特别喜欢这个解决方案,因为它直接创建一个系列,不需要转换。


A few comments on your code:对您的代码的一些评论:

1) 1)

Variable names should generally follow the lower_case_with_underscores style.变量名称通常应遵循lower_case_with_underscores样式。 See PEP 8 for more on Python style.有关 Python 样式的更多信息,请参阅PEP 8

2) 2)

Low = pd.Series(data['LOW']).values
High = pd.Series(data['HIGH']).values

From the rest of the code you showed me, you already know that data is a DataFrame.从您给我看的代码的 rest 中,您已经知道data是 DataFrame。 Getting a column from a DataFrame (ex: data['LOW'] ) returns a Series, so the call to pd.Series() is redundant.从 DataFrame (例如: data['LOW'] )获取列会返回一个系列,因此对pd.Series()的调用是多余的。 It also isn't clear why you then call .values() .也不清楚为什么你然后调用.values()

Bear in mind that using Series.values is discouraged.请记住,不鼓励使用Series.values From the docs :文档

Warning: We recommend using Series.array or Series.to_numpy(), depending on whether you need a reference to the underlying data or a NumPy array.警告:我们建议使用 Series.array 或 Series.to_numpy(),具体取决于您是否需要对基础数据或 NumPy 数组的引用。

3) 3)

newlist = []

for i in range(0, 8):
        newlist = random.uniform(Low, High)

I'm really struggling to find the words to explain this one.我真的很难找到解释这个词的词。 random.uniform is a function which takes two numbers, and produces a random float between the first two. random.uniform是一个 function ,它接受两个数字,并在前两个数字之间产生一个随机浮点数。 In this case, however, random.uniform is operating on two Series , thanks to the magic of broadcasting .然而,在这种情况下,由于广播的魔力, random.uniform在两个Series上运行。 This is why despite not looking like it makes much sense, the code doesn't actually produce an error or crash.这就是为什么尽管看起来没有多大意义,但代码实际上并没有产生错误或崩溃。

Notice also that the code above doesn't actually do anything with the result in newlist , nor does newList depend at all on the variable i .还要注意,上面的代码实际上并没有对newlist中的结果做任何事情, newList也完全不依赖于变量i

My guess is that you had something like this in mind:我的猜测是你有这样的想法:

# notice the variable name
new_nums_list = []

for i in range(low.size):
    curr_rand_num = random.uniform(low[i], high[i])
    new_nums_list.append(curr_rand_num)

Which could have also been written:也可以这样写:

new_nums_list = [random.uniform(low[i], high[i]) for i in range(low.size)]

Thanks to the power of zip() , we could do even better:由于zip()的强大功能,我们可以做得更好:

new_nums_list = [random.uniform(curr_low, curr_high) for curr_low, curr_high in zip(low, high)]

Any further questions?还有其他问题吗? :) :)

import random 
HIGH = [45.04, 44.05, 42.07, 41.95, 43.45, 46.04, 49.12, 50.94, 49.32] 
LOW = [19.35, 18.09, 17.44, 16.46, 18.38, 20.78, 24.99, 25.22, 24.67]  
result = []

for low,high in zip(LOW,HIGH): 
    result.append(random.uniform(low,high))

print(result) 

Example output:示例 output:

[20.23415150413731, 31.21737764031939, 33.39330466412157, 29.23878916462911, 33.23211876295469, 35.20524920370517, 32.21931456613152, 44.07717212042314, 48.56798614652851]

I think this should give you the solution you're after.我认为这应该为您提供所需的解决方案。 The code below puts the low and high lists into a list of its own, and iterates through each index, randomly picking one.下面的代码将低和高列表放入自己的列表中,并遍历每个索引,随机选择一个。

from random import randint

low = [19.35, 18.09, 17.44, 16.46, 18.38, 20.78, 24.99, 25.22, 24.67]
high = [45.04, 44.05, 42.07, 41.95, 43.45, 46.04, 49.12, 50.94, 49.32]

lists = [low, high]
newlist = []
for i in range(0, len(low)):
    newlist.append(lists[randint(0, 1)][i])

This is an output I just generated, for reference:这是我刚刚生成的一个output,供参考:

[19.35, 18.09, 42.07, 16.46, 18.38, 46.04, 24.99, 25.22, 49.32]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM