Python 3.6.1 :: Anaconda custom (64-bit)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mtptlb
print (np.__version__)
1.12.1
print (mtptlb.__version__)
2.0.2
%matplotlib inline
a=np.random.uniform(1,100,1000000)
b=range(1,101)
plt.hist(a)
Run Code Online (Sandbox Code Playgroud)
为什么Y轴显示100000?np.random.uniform(1100,百万)具有值百万,因此不应该它显示在y轴上百万?
在Java中,我们可以在其标题本身内更改for循环的计数器变量.见下文:
for(int i=1; i<1024; i*=2){
System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)
我知道以下代码是错误的.但有没有一种方法可以在不改变i循环内部值的情况下编写.我喜欢让我的for循环简单而简短:-)
for i in range(1, 1024, i*=2):
print(i)
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)
它适用于该示例,但可能有更有效的方法来涵盖更多边缘情况.
我正在尝试将具有不同数据和大小的2个列表组合成1,并将较小的列表"换行".我正在寻找一种干净的方法来做到这一点,例如
输入:
list1 = ['apple', 'orange', 'strawberry', 'avocado']
list2 = ['1','2','3']
Run Code Online (Sandbox Code Playgroud)
输出:
[
{"l1": "apple", "l2": "1"},
{"l1": "orange", "l2": "2"},
{"l1": "strawberry", "l2": "3"},
{"l1": "avocado", "l2": "1"}
]
Run Code Online (Sandbox Code Playgroud)
请注意,对于"avocado",我们回到"1"并包含list2.
明显(丑陋的)解决方案是从一个空列表开始,在一个循环中有两个索引,每个迭代附加一个新的列表项,而较小的一个'包装'到达结尾时的开头.在Python 2.7中有一种干净的方法吗?
我正在做一个练习,你应该计算你的项目是多么优雅的分数.高帽给你2分,领结给你4分,单片眼镜给你5分.我已经在每个类实例上启动了一个包含这些项的字典,但是当我getattr用来检查项是否具有该属性时,它总是返回None.
class Classy(object):
def __init__(self):
self.items = []
self.classyItems = {'tophat': 2, 'bowtie': 4, 'monocle': 5}
def addItem(self, str):
self.items.append(str)
def getClassiness(self):
classiness = 0
for item in self.items:
itemIsClassy = getattr(self.classyItems, item, None) # Why does this always return none?
if itemIsClassy:
classiness += itemIsClassy
else:
pass
return classiness
# Test cases
me = Classy()
# Should be 0
print me.getClassiness()
me.addItem("tophat")
# Should be 2
print me.getClassiness() # I'm getting 0
Run Code Online (Sandbox Code Playgroud)
为什么getattr回国 …
我知道我可以.translate(None, string.punctuation)从字符串中删除标点符号。但是,我想知道是否有一种方法可以仅在它是最后一个字符时才删除标点符号。
例如:
However, only strip the final punctuation.->However, only strip the final punctuation
和This is sentence one. This is sentence two!->This is sentence one. This is sentence two
和This sentence has three exclamation marks!!!->This sentence has three exclamation marks
我知道我可以编写一个while循环来执行此操作,但我想知道是否有一种更优雅/更有效的方法。
我刚开始使用OOP进行python(如果这是一个愚蠢的问题,请原谅我).我正在使用一个模型,该模型使用一个函数来模拟大气中二氧化碳排放的衰减.对于每个时间步,应使用该函数减少c_size类的属性Emissiondecay
我已将代码简化为单次运行,但我不断收到错误:
Traceback (most recent call last):
File "<co2>", line 20, in <module>
x.decay(year=1)
TypeError: decay() got multiple values for argument 'year'
Run Code Online (Sandbox Code Playgroud)
我无法看到任何多个值可能来自哪里.我只是将一个int值传递给代码.
代码是:
import numpy as np
class Emission:
def __init__(self,e_year=0, e_size=0.0):
self.e_year=e_year #emission year
self.e_size=e_size #emission size
self.c_size=[e_size] #current size
def decay(year):
#returns a % decay based on the year since emission 0 = 100% 1 = 93%
# etc. from AR5 WG1 Chapter 8 supplementary material equation 8.SM.10
# using coefficients …Run Code Online (Sandbox Code Playgroud) 与使用纯 python 相比,使用 numba 会产生更快的程序:
现在看来,纯 python 上的 numba 甚至(大部分时间)都比 numpy-python 快,例如https://jakevdp.github.io/blog/2015/02/24/optimizing-python-with- numpy 和 numba/。
根据https://murillogroupmsu.com/julia-set-speed-comparison/,在纯 python 代码上使用的 numba 比在使用 numpy 的 python 代码上使用的速度快。这通常是真的吗?为什么?
在/sf/answers/1816668031/中解释了为什么纯 python 上的 numba 比 numpy-python 更快:numba 看到更多的代码并且有更多的方法来优化代码,而 numpy 只看到一小部分。
这是否回答了我的问题?在使用 numpy 时,我是否会妨碍 numba 完全优化我的代码,因为 numba 被迫使用 numpy 例程而不是找到更优化的方法?我曾希望 numba 会意识到这一点,如果它没有好处,就不要使用 numpy 例程。然后它会使用 numpy 例程,只是它是一个改进(毕竟 numpy 已经过很好的测试)。毕竟“对 NumPy 数组的支持是 Numba 开发的重点,目前正在进行广泛的重构和改进。”
我不明白这段代码是如何工作的:
i = 1
while False:
if i % 5 == 0:
break
i = i + 2
print(i)
Run Code Online (Sandbox Code Playgroud)
什么呢while False?什么是假的?我不明白......
python ×10
python-3.x ×5
numpy ×3
list ×2
python-2.7 ×2
class ×1
deep-copy ×1
dictionary ×1
function ×1
getattr ×1
loops ×1
matplotlib ×1
numba ×1
oop ×1
performance ×1
punctuation ×1
random ×1
string ×1
strip ×1
while-loop ×1