我不完全确定这种行为是否符合预期,但这绝对是很奇怪的。当你有这样的代码时:
def a_function():
if a_list != some_other_list:
print(a_list)
Run Code Online (Sandbox Code Playgroud)
它工作得很好,我没有遇到任何问题。但是,如果将其更改为:
def a_function():
if a_list != some_other_list:
a_list = some_other_list
Run Code Online (Sandbox Code Playgroud)
突然,出现了一个问题,提示a_list第 2 行是未解析的引用。为什么if语句中的内容会影响能否a_list解决呢?这种事情正常吗?这可能是 Python 3.6.1 或 PyCharm(社区版 2017.1.5)中的错误吗?任何澄清这一点的帮助将不胜感激。
我有两个numpy数组.
x = [[1,2], [3,4], [5,6]]
y = [True, False, True]
Run Code Online (Sandbox Code Playgroud)
我想获得的元素X,其对应的元素的y是True:
filtered_x = filter(x,y)
print(filtered_x) # [[1,2], [5,6]] should be shown.
Run Code Online (Sandbox Code Playgroud)
我试过了np.extract,但它似乎只在x1d数组时工作.如何提取x相应值的元素y是True?
我有一个关于列表浅复制的问题。
在这两个示例中,我都修改了列表的一个元素,但在示例 1 中,列表b发生了更改,而在示例 2 中,列表d没有更改。我很困惑,因为在这两个示例中,我修改了列表的元素。
有什么不同?
示例1:
a=[1,2,[3,5],4]
b=list(a)
a[1]=0
print(a) # [1, 0, [3, 5], 4]
print(b) # [1, 2, [3, 5], 4]
Run Code Online (Sandbox Code Playgroud)
示例2:
c=[1,2,[3,5],4]
d=list(c)
c[2][0]=0
print(c) # [1, 2, [0, 5], 4]
print(d) # [1, 2, [0, 5], 4]
Run Code Online (Sandbox Code Playgroud) 自引入以来bool,它一直是子类int,并且bool可以隐式地"转换"为整数:
>>> issubclass(bool, int)
True
>>> ['one', 'two'][False]
'one'
>>> ['one', 'two'][True]
'two'
>>> True/20
0.05
Run Code Online (Sandbox Code Playgroud)
这是出于历史原因:与2.3之前的API的兼容性; 我明白它保持在2.3到2.7之间.(这在2011年的这个问题中得到了解决)
但是,为什么它仍然适用于Python 3?我认为没有任何优势.并且没有理由为了向后兼容而保持这一点:Python 3.0是一个突破性版本; 并且我认为任何2.3之前的API都不会存在.
我正在使用浮点数.如果我做:
import numpy as np
np.round(100.045, 2)
Run Code Online (Sandbox Code Playgroud)
我明白了:
Out[15]: 100.04
Run Code Online (Sandbox Code Playgroud)
显然,这应该是100.05.我知道IEEE 754的存在,并且浮点数的存储方式是导致这种舍入误差的原因.
我的问题是:我怎样才能避免这个错误?
我有一个数组,通过绘制直方图并用高斯拟合它我想在噪声图上创建一个信号(用我的高斯的西格玛比例).我在几个地方有值0但我想只保留值<0和> 0.
我可以这样写我的剧本吗?
new_SN_map = temp_SN_map[(temp2_SN_map < 0) & (temp_SN_map > 0)]
Run Code Online (Sandbox Code Playgroud)
有没有办法自动删除值0?
在numpy中使用单精度(float32)编写代码时,太难写了。
首先,单精度浮子的寿命太长。我们必须按如下方式输入所有变量。
a = np.float32(5)
Run Code Online (Sandbox Code Playgroud)
但其他一些语言使用更简单的表示。
a = 5.f
Run Code Online (Sandbox Code Playgroud)
其次,艺术手术也不方便。
b = np.int32(5)+np.float32(5)
Run Code Online (Sandbox Code Playgroud)
我期望的类型b是 isnumpy.float32但它是numpy.float64。
当然,
b = np.add(np.int32(5), np.float32(5), dtype=np.float32)
Run Code Online (Sandbox Code Playgroud)
返回我想要的。但要替换所有操作就太长了。
有没有更简单的方法在 numpy 中使用单精度?
我想比较我创建新列的两列的值bin_crnn.如果他们是等于我想要1,否则想要0.
# coding: utf-8
import pandas as pd
df = pd.read_csv('file.csv',sep=',')
if df['crnn_pred']==df['manual_raw_value']:
df['bin_crnn']=1
else:
df['bin_crnn']=0
Run Code Online (Sandbox Code Playgroud)
我得到以下错误
if df['crnn_pred']==df['manual_raw_value']:
File "/home/ahmed/anaconda3/envs/cv/lib/python2.7/site-packages/pandas/core/generic.py", line 917, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Run Code Online (Sandbox Code Playgroud) 我正在尝试初始化表示 3x3 数组的列表列表:
import copy
m = copy.deepcopy(3*[3*[0]])
print(m)
m[1][2] = 100
print(m)
Run Code Online (Sandbox Code Playgroud)
输出是:
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[0, 0, 100], [0, 0, 100], [0, 0, 100]]
Run Code Online (Sandbox Code Playgroud)
这不是我所期望的,因为每行的最后一个元素是共享的!我确实通过使用得到了我需要的结果:
m = [ copy.deepcopy(3*[0]) for i in range(3) ]
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么第一个(和更简单的)形式不起作用。不是deepcopy应该很深吗?
我有一个由1和0组成的列表,例如
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0]
Run Code Online (Sandbox Code Playgroud)
我想输出另一个相同长度的列表,其中每个条目代表刚刚消失的连续0的数量,即上面示例的输出将是:
[0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 1, 0]
Run Code Online (Sandbox Code Playgroud)
请注意,输出列表的第一个条目将始终是,0并且输入列表的最后一个条目是什么并不重要.
到目前为止我尝试过的:
def zero_consecutive(input_list):
output = [0]
cons = 0
for i in input_list[:-1]:
if i == 0:
cons += 1
output.append(cons)
else:
cons = 0
output.append(cons)
return output
Run Code Online (Sandbox Code Playgroud)
它适用于该示例,但可能有更有效的方法来涵盖更多边缘情况.