Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用随机数设置新记录(在数组中)时如何打印_Python_Arrays_Random_Graph_Matplotlib - Fatal编程技术网

Python 使用随机数设置新记录(在数组中)时如何打印

Python 使用随机数设置新记录(在数组中)时如何打印,python,arrays,random,graph,matplotlib,Python,Arrays,Random,Graph,Matplotlib,我已经生成了一个随机数图(标准偏差1,平均值0)。如果是目前看到的最大数字,我需要浏览一下列表并打印出每个数字。例如,给定列表[10,5,15,18,5,7,9,100],代码将打印出来: 10 (first number is always going to be the biggest currently seen) 15 18 100 这是我的密码: import pylab a = pylab.arange(1.0, 129.05, 1) xrandn = pylab.zeros(12

我已经生成了一个随机数图(标准偏差1,平均值0)。如果是目前看到的最大数字,我需要浏览一下列表并打印出每个数字。例如,给定列表
[10,5,15,18,5,7,9,100]
,代码将打印出来:

10 (first number is always going to be the biggest currently seen)
15
18
100
这是我的密码:

import pylab
a = pylab.arange(1.0, 129.05, 1)
xrandn = pylab.zeros(129, float)

for j in range(0, 129):
    xrandn[j] = pylab.randn()

pylab.plot(a, xrandn, "r-")
pylab.xlabel("Year")
pylab.ylabel("Units")
pylab.title("Possible Tempertaure Fluctuations")
pylab.show()
另外,作为一个额外的例子,我如何在图形本身上标记这些点

编辑1: 谢谢你的回复,我现在陷入了以下困境

我现在需要生成大量随机数“集合”,从中我需要能够检索每个集合中第一个值的最大值,然后能够访问每个集合中的第二个值,然后是每个集合中的第三个值,等等,计算超过前一个最大值的值的数量。我曾尝试创建一个矩阵来为我做到这一点,但我不断收到错误消息,告诉我不能在矩阵中使用浮点数,我现在不确定这是否是正确的方法,或者对以前代码的某种修改是否会产生答案

import pylab

def make_list(size):
    """create a list of size number of zeros"""
    mylist = []
    for i in range(size):
        y = pylab.randn()
        mylist.append(y)
    return mylist

def make_matrix(rows, cols):
    """
    create a 2D matrix as a list of rows number of lists
    where the lists are cols in size
    resulting matrix contains zeros
    """
    matrix = []
    for i in range(rows):
        matrix.append(make_list(cols))
    return matrix

mx = make_matrix(10, 6)

print(mx)

最简单的方法可能是为运行最大值绘制单独的阶梯曲线:

>>> def running_maximum(data):
        result = data[:1]
        for x in data[1:]:
            result.append(max(x, result[-1]))
        return result

>>> data = [10, 5, 15, 18, 5, 7, 9, 100]
>>> running_maximum(data)
[10, 10, 15, 18, 18, 18, 18, 100]

最简单的方法可能是为运行最大值绘制单独的阶梯曲线:

>>> def running_maximum(data):
        result = data[:1]
        for x in data[1:]:
            result.append(max(x, result[-1]))
        return result

>>> data = [10, 5, 15, 18, 5, 7, 9, 100]
>>> running_maximum(data)
[10, 10, 15, 18, 18, 18, 18, 100]
尽可能简单

def foo(data):
    lastMax=[data[0]]
    for x in data[1:]:
        if x>lastMax[-1]:
            lastMax.append(x)
    return lastMax
尽可能简单

def foo(data):
    lastMax=[data[0]]
    for x in data[1:]:
        if x>lastMax[-1]:
            lastMax.append(x)
    return lastMax

此函数将打印出所有记录高点,并返回它们的值以及它们在数组中出现的索引

def print_current_max(data):
    m = data[0]
    print m # First point will always be the biggest one yet seen.
    highs = [(0, m)]
    for (i, x) in enumerate(data):
        if m < x:
            m = x
            print m
            highs.append((i, m))

    return highs

此函数将打印出所有记录高点,并返回它们的值以及它们在数组中出现的索引

def print_current_max(data):
    m = data[0]
    print m # First point will always be the biggest one yet seen.
    highs = [(0, m)]
    for (i, x) in enumerate(data):
        if m < x:
            m = x
            print m
            highs.append((i, m))

    return highs

你能就我的下一个节目提供更多建议吗(编辑在同一个问题内),谢谢你能就我的下一个节目提供更多建议吗(编辑在同一个问题内),谢谢