说我有一个国家名单
l = ['India', 'China', 'China', 'Japan', 'USA', 'India', 'USA']
Run Code Online (Sandbox Code Playgroud)
然后我有一个独特的国家清单
ul = ['India', 'China', 'Japan', 'USA']
Run Code Online (Sandbox Code Playgroud)
我想按升序对列表中的每个独特国家/地区进行计数。因此输出应如下所示:
Japan 1
China 2
India 2
USA 2
Run Code Online (Sandbox Code Playgroud) 我有一个这样的字典:
migration_dict = {'30005': ['key42750','key43119', 'key44103', ['key333'],
['key444'], ['keyxx']], '30003': ['key43220', 'key42244','key42230',
['keyzz'], ['kehh']]}
Run Code Online (Sandbox Code Playgroud)
我怎样才能压平每个键的值以便得到类似的东西:
migration_dict = {'30005': ['key42750','key43119', 'key44103', 'key333',
'key444', 'keyxx'], '30003': ['key43220', 'key42244','key42230',
'keyzz', 'kehh']}
Run Code Online (Sandbox Code Playgroud) 我遇到了一个我不太明白的错误.如果我有以下代码段:
class Test(object):
def __init__(self):
self.data = {}
def update_data(self, **update):
self.data = update
t = Test()
t.update_data(test='data') # Works
t.update_data({'test':'data'}) # TypeError: update_data() takes 1 positional argument but 2 were given
Run Code Online (Sandbox Code Playgroud)
所以根据我的理解,**update语法是字典破坏语法,当你将dict传递给函数时,它会被转换为关键字参数.
我在这里不正确地理解了什么?
我想要一个易于读取访问多维numpy数组的某些部分.对于任何访问第一个维度的数组都是easy(b[index]).另一方面,访问第六维是"硬"(特别是阅读).
b[:,:,:,:,:,index] #the next person to read the code will have to count the :
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?特别是有一种方法,在编写程序时轴是不知道的吗?
编辑:索引维度不一定是最后一个维度
注意:我不确定这是否重复-请让我知道是否重复(并结束问题)。
如果有一个一维NumPy数组vector,则如果编写形式为以下形式的for循环:
for element in vector :
print(element)
Run Code Online (Sandbox Code Playgroud)
结果将打印NumPy数组的每个元素。
如果有一个二维NumPy数组matrix,则如果编写一个for循环,其形式为:
for vector in matrix :
print(vector)
Run Code Online (Sandbox Code Playgroud)
结果将打印二维NumPy数组的每一行,即它将打印一维NumPy数组,并且不会单独打印数组的每个元素。
但是,如果将for循环写为:
import numpy
for element in numpy.nditer(matrix) :
print(element)
Run Code Online (Sandbox Code Playgroud)
结果将打印二维NumPy数组的每个元素。
问题:如果拥有3维NumPy数组会发生什么tensor?
一种。如果编写以下形式的for循环:
for unknownType in tensor :
print(unknownType)
Run Code Online (Sandbox Code Playgroud)
这会打印出组成的二维NumPy(子)数组tensor吗?
即对于n维NumPy数组nArray,是否for unknownType in nArray :迭代组成n(N-1)维的NumPy(子)数组nArray?
b。如果编写以下形式的for循环:
for unknownType in numpy.nditer(tensor) :
print(unknownType)
Run Code Online (Sandbox Code Playgroud)
这会印出的元素tensor吗?还是会打印组成的二维二维NumPy(子)数组的组成一维NumPy(子)数组tensor?
即对于n维NumPy数组nArray,是否for unknownType in …
我知道我可以通过这样做在python dict中添加一个新的键/值
some_dict['absent_key'] = somevalue
Run Code Online (Sandbox Code Playgroud)
但我真的不了解内部工作.
我曾经认为字典表现得像C++地图.如果[]操作符不存在,则为给定键创建元素,然后返回对它的引用,以便可以在运算符的同一行中为其赋值=.
但是,C++中的这种行为的结果是,如果我们从映射中查询不存在的键的值,则会为该键创建该元素,并返回值类型的默认值而不是错误.在python中,这会抛出一个KeyError.
所以我不明白的是:如何,因为[]运算符必须=在python 之前进行评估(我认为?),它的行为会有所不同,这取决于结果是读取还是分配了一个值(它不应该在那里知道)表达式评价的要点)?
python评估表达式的顺序有区别吗?或者解释器只是更聪明,因为字典是一个硬编码类型,所以它更准确地知道它的行为,而std :: map是在'库'中,所以编译器可以假设更少?还是其他一些原因?
请在投票前仔细阅读此问题。我在这里的其他问题中找不到我的问题。
假设我有一个数组,
>>> import numpy as np
>>> array = np.linspace(1,4,4, dtype=np.int)
>>> array
array([1, 2, 3, 4])
Run Code Online (Sandbox Code Playgroud)
我想要一个函数,将这个数组分割成所有可能的部分,这样,
没有分裂:
([1,2,3,4])
Run Code Online (Sandbox Code Playgroud)
分成2几部分:
([1], [2,3,4])
([1,2], [3,4])
([1,2,3] ,[4])
Run Code Online (Sandbox Code Playgroud)
分成3几部分:
([1], [2], [3,4])
([1,2]), [3], [4])
([1], [2,3], [4])
Run Code Online (Sandbox Code Playgroud)
分成len(array)几部分:
([1],[2],[3],[4])
Run Code Online (Sandbox Code Playgroud)
我知道有np.split(array, r),但它不会给出所有可能的拆分。例如np.split(array, 2)会给,
[array([0, 1]), array([2, 3])]
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,这不是我需要的。如何实现我的需求?
我试图填补我对蟒蛇的理解中的空白property().以下是我提出的代码,以了解property():
class Temperature:
def __init__(self, temp = 10):
self.set_temp(temp)
def to_farenheit(self):
return (self._temp * 1.8) + 32
def get_temp(self):
print "getting temperature value"
return self._temp
def set_temp(self, temp):
print "setting temperature value"
if temp < -237:
raise ValueError("this shud be above -273")
else:
self._temp = temp
temps = property(get_temp, set_temp)
Run Code Online (Sandbox Code Playgroud)
我执行上面的类并执行以下操作:
>>> t = Temperature()
setting temperature value
>>> t.temps
getting temperature value
10
>>> t.temps = 13
>>> t.temps
13
>>> t.get_temp()
getting temperature value …Run Code Online (Sandbox Code Playgroud) 当我尝试np.empty在用 numba 编译的函数定义中使用时遇到了这个奇怪的错误,并打开nopython=True以确保优化的输入有效。
这很奇怪,因为 numba 声称支持np.empty前两个参数,而我只使用前两个参数(我认为正确吗?),所以我不知道为什么它输入不正确。
@jit(nopython=True)
def empty():
return np.empty(5, np.float)
Run Code Online (Sandbox Code Playgroud)
在 ipython notebook 中定义上述函数后,
empty()
Run Code Online (Sandbox Code Playgroud)
给出以下错误消息:
@jit(nopython=True)
def empty():
return np.empty(5, np.float)
Run Code Online (Sandbox Code Playgroud) 我有一个名为 Person() 的类。它有一个 CURRENT_YEAR 类变量,旨在在该类的所有实例之间共享。
我希望单个模块中的每个测试都能获得一个新的(新的)对象,因为我将夹具的范围限定为“函数”。但是,当我在一个测试函数中更改 CURRENT_YEAR 时(使用更改 Person.CURRENT_YEAR 值的类方法发生这种情况),它会保留到下一个测试函数中。很明显,该对象不会在每次测试中被清除并重新创建。
该装置在 conftest.py 中创建,可供所有测试访问。
最后,我把它全部分解,并移动了一些东西,但仍然看到同样的东西。正如我所期望的那样,Person() 类没有被实例化多次。应该如何创建一个固定装置,以便每个 test_ 函数都有自己的类范围?
我尝试过将测试移动到单独的模块,但没有帮助。
我尝试制作第二个固定装置,它返回一个 Person() 对象。没有不同。
我已经在下面的代码中将其删除,因此希望清楚我正在尝试的内容以及为什么我感到困惑。
import os,sys
tests = os.path.dirname(__file__)
project = os.path.dirname(tests)
sys.path.insert(0,project)
import pytest
from app.person import *
def test_current_year_changes(person_fixture):
import pytest
p = person_fixture
print(f"CY is {p.CURRENT_YEAR} and age is {p.get_age()}")
p.add_years_to_age(20)
print(f"CY is {p.CURRENT_YEAR} and age is {p.get_age()}")
assert p.CURRENT_YEAR == 20
def test_current_year_changes2(person_fixture2):
import pytest
p = person_fixture2
print(f"CY is {p.CURRENT_YEAR} and age is {p.get_age()}")
p.add_years_to_age(20)
print(f"CY …Run Code Online (Sandbox Code Playgroud) python ×10
arrays ×4
numpy ×4
dictionary ×3
python-3.x ×2
class ×1
count ×1
fixtures ×1
flatten ×1
for-loop ×1
function ×1
indexing ×1
numba ×1
operators ×1
properties ×1
pytest ×1
python-2.7 ×1
python-2.x ×1
split ×1
typing ×1
unit-testing ×1