我想从数组中获取两个最小值x。但是当我使用时np.where:
A,B = np.where(x == x.min())[0:1]
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
ValueError:需要多个值才能解压
如何解决此错误?我是否需要在数组中按升序排列数字?
在使用scipy/numpy时,我确实获得了存储到的信息 numpy.ndarray
>>> a
array([[ 0.15555605, 0.51031528, 0.84580176, 0.06722675],
[ 0.60556045, 0.62721023, -0.48979983, -0.04152777],
[-0.78044785, 0.58837543, -0.21146041, -0.13568023],
[ 0. , 0. , 0. , 1. ]])
>>> print(a)
[[ 0.15555605 0.51031528 0.84580176 0.06722675]
[ 0.60556045 0.62721023 -0.48979983 -0.04152777]
[-0.78044785 0.58837543 -0.21146041 -0.13568023]
[ 0. 0. 0. 1. ]]
Run Code Online (Sandbox Code Playgroud)
如何在一行上打印结果?
我已经检查过:
>>> numpy.get_printoptions()
{'precision': 8, 'threshold': 1000, 'edgeitems': 3, 'linewidth': 75, 'suppress': False, 'nanstr': 'nan', 'infstr': 'inf', 'formatter': None}
Run Code Online (Sandbox Code Playgroud)
但即使设置linewidth为1000也不会改变这一点.有没有办法更改该类型的显示格式?
是否也可以在每个数字之间添加逗号(如数组显示但没有周围array(...))?
当我试图通过代码绘制交互式绘图时:
import matplotlib.pyplot as plt
import PyQt5
%matplotlib qt
...
plt.plot(a_list,b_list)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
--------------------------------------------------------------------------- TypeError Traceback (most recent call
last) <ipython-input-49-187dd4fead7a> in <module>()
----> 1 plt.plot(parameters['eng_list'],np.real(conductivity))
2 plt.plot(parameters['eng_list'],np.imag(conductivity))
3 plt.show()
C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(*args,
**kwargs) 3305 @_autogen_docstring(Axes.plot) 3306 def plot(*args, **kwargs):
-> 3307 ax = gca() 3308 # Deprecated: allow callers to override the hold state 3309 # by passing hold=True|False
C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py in gca(**kwargs)
948 matplotlib.figure.Figure.gca : The figure's gca method.
949 """
--> 950 return gcf().gca(**kwargs)
951
952 # More …Run Code Online (Sandbox Code Playgroud) 所以我有一个包含 5,000,000 个整数的列表。我想将列表覆盖到 numpy 数组中。我尝试了以下代码:
numpy.array( list )
Run Code Online (Sandbox Code Playgroud)
但速度非常慢。
我对该操作进行了 100 次基准测试,并对列表进行了 100 次循环。没有太大区别。
有什么好主意如何让它更快吗?
我正在尝试运行我几年前编写的使用mplfrom 的代码matplotlib。它曾经运行良好,但现在突然出现错误:
from matplotlib import mpl
ImportError: cannot import name mpl
Run Code Online (Sandbox Code Playgroud)
我正在使用 Python 2.7 和 matplotlib 1.5.2。
所以我的 python 代码中遇到了一个问题,我将其归结为:
假设我们有一个函数u:
def u(y,t):
h = float(10)
U0 = float(1)
return U0/h*(y)
Run Code Online (Sandbox Code Playgroud)
和一个数组:
a=np.array([[0]*2]*2)
Run Code Online (Sandbox Code Playgroud)
然后执行以下操作:
a[1][1] = u(1,0)
Run Code Online (Sandbox Code Playgroud)
但尽管等于 仍会a[1][1]返回。0u(1,0)0.1
为什么会发生这种情况以及如何避免这种情况?
我有以下数组:
import numpy as np
a = np.array([[2, 3, 5],
[4, 6, 7],
[1, 5, 7]])
Run Code Online (Sandbox Code Playgroud)
我想将它扩展到这个数组:
b = [[2 2 2 3 3 3 5 5 5]
[2 2 2 3 3 3 5 5 5]
[2 2 2 3 3 3 5 5 5]
[4 4 4 6 6 6 7 7 7]
[4 4 4 6 6 6 7 7 7]
[4 4 4 6 6 6 7 7 7]
[1 1 1 5 5 5 7 …Run Code Online (Sandbox Code Playgroud) 演示我所要求的内容的几行代码是:
>>> x = ()
>>> for i in range(1000000):
... x = (x,)
>>> x.__hash__()
=============================== RESTART: Shell ===============================
Run Code Online (Sandbox Code Playgroud)
1000000 可能过多,但它表明在散列嵌套元组(并且我假设其他对象)时存在某种形式的限制。只是为了澄清,我没有重新启动 shell,当我尝试哈希时它会自动执行此操作。
我想知道这个限制是什么,为什么会发生(以及为什么它没有引发错误),以及是否有解决方法(以便我可以将这样的元组放入集合或字典中)。
我刚刚收到错误:
Traceback (most recent call last):
File "./download_documents.py", line 153, in <module>
paragraphs, used_pages = find_pages(lang, to_extract)
File "./download_documents.py", line 67, in find_pages
random.shuffle(page_titles_queue)
File "/usr/lib/python2.7/random.py", line 291, in shuffle
x[i], x[j] = x[j], x[i]
KeyError: 1
Run Code Online (Sandbox Code Playgroud)
这让我很困惑。
random.shuffle 似乎适用于零元素列表和单元素列表。page_titles_queue 是元组列表。random.shuffle(page_titles_queue),有page_titles_queue.pop(),但这不应该影响洗牌。对?那么 KeyError 的可能原因是什么?
我Python 2.7.12在 Ubuntu 16.04 上使用。
我注意到当我在列表理解中使用递归时会发生一些奇怪的事情.如果递归过深,解释器似乎空闲(我等了5分钟,什么都没发生).
为了这个问题,假设我想要展平嵌套列表(我没有 - 但它是一个简短的代码示例,说明了我遇到的问题):
def flatten(x):
if isinstance(x, list):
return [a for i in x for a in flatten(i)]
else:
return [x]
Run Code Online (Sandbox Code Playgroud)
使用辅助函数创建嵌套列表:
def wrap_in_lists(value, depth):
a = value
for _ in range(depth):
a = [a]
return a
Run Code Online (Sandbox Code Playgroud)
使用时效果很好:
>>> flatten(wrap_in_lists(1, 2**10))
[1]
Run Code Online (Sandbox Code Playgroud)
但是当我使用时它完全停止:
>>> flatten(wrap_in_lists(1, 2**11))
# Nothing happens, no exception, no result, no segfault, ...
Run Code Online (Sandbox Code Playgroud)
奇怪的是,使用生成器的类似方法不会显示此行为:
def flatten(l):
def inner(x):
for item in x:
if isinstance(item, list):
yield from inner(item)
else:
yield item
return list(inner(l))
>>> flatten(wrap_in_lists(1, …Run Code Online (Sandbox Code Playgroud) python ×9
numpy ×5
arrays ×4
matplotlib ×2
python-2.7 ×2
formatting ×1
hash ×1
importerror ×1
jupyter ×1
keyerror ×1
list ×1
performance ×1
plt ×1
pointers ×1
pyqt ×1
python-3.x ×1
qt ×1
random ×1
recursion ×1
scipy ×1
shuffle ×1
zoom ×1