热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python中linspace函数_Pythonlinspace()显示奇怪的行为(Pythonlinspace()showsstrangebehavior)...

Python-linspace()显示奇怪的行为(Python-linspace()showsstrangebehavior)我遇到了np.linspace()的问题,

Python - linspace()显示奇怪的行为(Python - linspace() shows strange behavior)

我遇到了np.linspace()的问题,我不知道为什么它会像这样。 我的代码如下所示:

x = linspace(0, 6, 3)

print 'x = ', x # prints the correct values x = [ 0. 3. 6.]

y = x; y *= 3; y += 3

print 'y = ', y # prints the correct values y = [ 3. 12. 21.]

z = x; z *= 3; z -= 3

print 'z = ', z # prints the correct values z = [ 6. 33. 60.]

# and then this ???

print x, y, z # prints [ 6. 33. 60.] [ 6. 33. 60.] [ 6. 33. 60.]

有人知道为什么打印x,y,z不像以前那样返回正确的值(打印x,打印y等等?我是否必须将x,y,z转换为单独的新数组才能打印出来正确吗?在启蒙前提前感谢!

I'm running into an issue with np.linspace(), and i have no idea why it behaves like this. My code looks like this:

x = linspace(0, 6, 3)

print 'x = ', x # prints the correct values x = [ 0. 3. 6.]

y = x; y *= 3; y += 3

print 'y = ', y # prints the correct values y = [ 3. 12. 21.]

z = x; z *= 3; z -= 3

print 'z = ', z # prints the correct values z = [ 6. 33. 60.]

# and then this ???

print x, y, z # prints [ 6. 33. 60.] [ 6. 33. 60.] [ 6. 33. 60.]

does somebody know why print x,y,z doesn't return the correct values as before (print x, print y, etc.? Do i have to convert x,y,z into separate, new arrays in order for them to print correctly? Thanks a ton in advance for the enlightenment!

原文:https://stackoverflow.com/questions/21923435

更新时间:2019-10-27 14:10

相关问答

arange()类似于内置函数range(),通过指定开始值、终值和步长创建表示等差数列的一维数组,注意得到的结果数组不包含终值。 linspace()通过指定开始值、终值和元素个数创建表示等差数列的一维数组,可以通过endpoint参数指定是否包含终值,默认值为True,即包含终值。

正如Jeff Meatball Yang所说,索引的作用使得它首先出现在列表中。 我会做那样的事情 lst = [["X", [], []],

[[], "X", []],

[[], "X", []]]

temp_lst = []

for i in range(len(lst)):

lst_1 = []

for j in range(len(lst[i])):

if lst[i][j] == "X":

lst_1.append(

...

线性空间。 换句话说,从一段时间的直线上我们取n个样本。 A linear space. So in other words, from a straight line over an interval we take n samples.

编辑 :我想我误解了这个问题意味着你不理解转义序列。 如果你不明白为什么这些颜色是错误的,这就是为什么: if words[i+1]>86400:

您正在比较字符串与整数。 if float(words[i+1][:-4])>86400:

这将切断正常运行时间的“分钟”,并将剩余时间转换为浮点数。 同样, if float(words[i+1][:-1])>80:允许百分比与数字进行比较。 此外,由于0.0%是多行生成的,所以replace函数可能不是最适合您的需求,因为ist会一次格式化“0

...

正如评论中所指出的,您在Python 2.x中看到行缓冲标准输出的效果:在print的尾随将阻止它发出新行。 在发出新行之前,不会打印带尾随的文本。 您可以使用一些简单的代码删除缓冲: #!/usr/bin/env python

import sys

class Logger(object):

def __init__(self, stream):

self.stream = stream

def write(self, msg):

self.st

...

你可以这样做: c = np.array([np.linspace(i,j,5) for i,j in zip(a,b)])

#array([[ 1. , 1.25, 1.5 , 1.75, 2. ],

# [ 2. , 2.25, 2.5 , 2.75, 3. ],

# [ 3. , 3.25, 3.5 , 3.75, 4. ],

# [ 4. , 4.25, 4.5 , 4.75, 5. ],

#

...

如果你阅读了locals函数的文档 ,你会看到 更新并返回表示当前本地符号表的字典。 locals()在函数块中调用时返回自由变量,但在类块中不调用。 locals()不只返回局部变量的字典; 它还会更新 dict以反映当前的局部变量值。 If you read the docs for the locals function, you'll see Update and return a dictionary representing the current local symbol table

...

是的,请将您的numpy文档与列表文档进行比较: https : //docs.python.org/3.6/tutorial/datastructures.html 你也可以玩下面的代码: from numpy import linspace

a = linspace(2, 3, num=5)

b = range(5)

print(type(a), a)

print(type(b), b)

print()

print("array + array:", a + a)

print("list

...

您不应该使用linspace的输出来索引数组! linspace生成浮点数,数组索引需要一个整数。 从你的代码看起来你真正想要的是这样的: z_bin = numpy.linspace(0.0, 10.0, 21)

for i in range(len(z_bin)-1):

zmin = z_bin[i]

zmax = z_bin[i+1]

# do some things with zmin/zmax

zmin和zmin仍然是浮点(十进制)数。 You should

...

执行赋值时,numpy数组指向相同的内存位置。 要将值复制到新的内存位置,请执行y = x.copy() a numpy array points to the same memory location when you do assignment. For copying values to a new memory location do a y = x.copy()



推荐阅读
author-avatar
赵小锅2502889451
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有