我有一个(1000L, 3L)形状可变的数据,我执行以下操作来获取坐标:
x = data[:,0]
y = data[:,1]
z = data[:,2]
Run Code Online (Sandbox Code Playgroud)
有没有办法解开它们?我试过了,但不起作用:
[x,y,z] = data1[:,0:3]
Run Code Online (Sandbox Code Playgroud) 我正在尝试将字典传递给 sklearn 分类器来设置其参数,但我也想设置base_estimator功能,例如:
>>> from sklearn.ensemble import AdaBoostClassifier
>>> x = {'n_estimators': 200}
>>> clf = AdaBoostClassifier(**x)
>>> clf
AdaBoostClassifier(algorithm='SAMME.R', base_estimator=None,
learning_rate=1.0, n_estimators=200, random_state=None)
Run Code Online (Sandbox Code Playgroud)
工作正常,但如果我尝试:
>>> x = {'base_estimator__max_depth':5}
>>> clf = AdaBoostClassifier(**x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument
'base_estimator__max_depth'
Run Code Online (Sandbox Code Playgroud)
我也尝试预先设置基本估计器,AdaBoostClassifier(base_estimator=DecisionTreeClassifier(),**x)但这也不适用于与上面相同的错误。
我意识到它可以设置clf.base_estimator__max_depth = 5,但我理想地想解压一个设置分类器多个参数的字典。所以我的问题是,这可能吗?如果可能的话,如何实现?
注意:我知道如何设置这些参数,我只是想知道是否可以通过解压字典来完成此操作,因为这对我来说看起来更好
任何人都可以解释使用单星号或双星号打开字典时的区别吗?在函数参数中使用时,您可以提及它们的区别,前提是它们在这里相关,我不这么认为。
但是,可能存在一些相关性,因为它们共享相同的星号语法。
def foo(a,b)
return a+b
tmp = {1:2,3:4}
foo(*tmp) #you get 4
foo(**tmp) #typeError: keyword should be string. Why it bothers to check the type of keyword?
Run Code Online (Sandbox Code Playgroud)
此外,为什么在这种情况下作为函数参数传递时,字典的键不允许是非字符串?有任何例外吗?他们为什么这样设计Python,是因为编译器无法推断出这里的类型还是什么?
谢谢!
我有一个函数可以读取二进制文件,然后使用 struct.unpack() 解压缩文件的内容。我的功能工作得很好。如果/当我使用长“格式”字符串解压缩整个文件时,速度会更快。问题是有时字节对齐会发生变化,所以我的格式字符串(无效)看起来像 '<10sHHb>llh' (这只是一个例子(它们通常更长))。有没有处理这种情况的超灵巧/pythonic 方法?
python performance binaryfiles python-3.x iterable-unpacking
这是我的代码:
a = [1, 2, 3, 4, 5]
a[0], a[a[0]] = a[a[0]], a[0]
print(a)
Run Code Online (Sandbox Code Playgroud)
我正在尝试交换a[0](a[a[0]]即a[1]在本例中),所以我期望的结果是:
[2, 1, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
我得到的结果是[2, 2, 1, 4, 5],这不是我想要的。
如果我简化a[0], a[a[0]] = a[a[0]], a[0]为a[0], a[1] = a[1], a[0],它就有效。
我怎样才能使列表内的交换像a, b = b, a以前那样工作?
每当我运行此代码时,python 都会给我:
ValueError:没有足够的值来解包(预期为 3,得到 2)
我正在尝试制作一种地址簿,您可以在其中添加、删除和更改信息。我试图更改第 20 行中存在for-in循环的代码(该行实际上是问题的根源),但没有给出任何结果。
members = {}
class Member:
def __init__(self, name, email, number):
self.name = name
self.email = email
self.number = number
def addmember(name, email, number):
members[name] = email, number
print('Member {0} has been added to addressbook'.format(name))
def Check():
print("You've got {0} members in your addressbook".format(len(members)))
for name, email, number in members.items(): #the problem is here
print('Contact {0} has email:{1} and has number:{2}'.format(name, email, number))
print('')
Member.addmember('Tim', 'email@mail.com', '43454')
Member.Check()
Run Code Online (Sandbox Code Playgroud) 想象一下我有一个对象,它是一个类的实例,如下所示:
@dataclass
class Foo:
bar: int
baz: str
Run Code Online (Sandbox Code Playgroud)
我使用它是dataclasses为了方便,但在这个问题的上下文中,不要求该类是dataclass.
通常,如果我想解压此类对象的属性,我必须实现__iter__,例如如下:
class Foo:
...
def __iter__(self) -> Iterator[Any]:
return iter(dataclasses.astuple(self))
bar, baz = Foo(1, "qux")
Run Code Online (Sandbox Code Playgroud)
然而,从像Pyright这样的静态类型检查器的角度来看,我现在丢失了bar和的任何类型信息baz,它只能推断出它们的类型Any。我可以通过iter手动创建元组参数来稍微改进:
def __iter__(self) -> Iterator[Union[str, int]]:
return iter((self.bar, self.baz))
Run Code Online (Sandbox Code Playgroud)
但我仍然没有bar和的特定类型baz。我可以注释bar然后直接baz使用,dataclasses.astuple如下:
bar: str
baz: int
bar, baz = dataclasses.astuple(Foo(1, "qux"))
Run Code Online (Sandbox Code Playgroud)
但这需要可读性较差的多级列表理解,例如
bars: list[int] = [
bar for bar, _ in [dataclasses.astuple(foo) for …Run Code Online (Sandbox Code Playgroud) 我想知道在Python 2.7中是否有更简洁的方法来执行以下操作?
# Current working code!
(is_enabled,) = struct.unpack_from("<?", data)
cmd_speed = struct.unpack_from("<3h", data, 1)
tach_speed = struct.unpack_from("<3h", data, 1+2*3)
Run Code Online (Sandbox Code Playgroud)
具体来说,我不喜欢手动跟踪到下一个元组的偏移量.理想情况下,我希望能够使用单个格式语句指定数据结构; 像这样的东西:
# Hypothetical example, does not work!
(is_enabled,), cmd_speed, tach_speed = struct.unpack("<(?),(3h),(3h)", data)
Run Code Online (Sandbox Code Playgroud) 如何在ipython中解压缩shell转义的输出?
示例(作品):
In [1]: !locate .hgrc
/home/wim/.hgrc
/usr/share/doc/mercurial-common/examples/sample.hgrc
In [2]: hgrcs = !locate .hgrc
In [3]: hgrcs[0]
Out[3]: '/home/wim/.hgrc'
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
In [4]: hgrc0, *rest = !locate .hgrc
File "<ipython-input-4-e8900264b4a8>", line 1
hgrc0, *rest = !locate .hgrc
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
也不工作:
In [13]: x = !locate .hgrc | head -1
In [14]: x
Out[14]: ['/home/wim/.hgrc']
In [15]: x, = !locate .hgrc | head -1
File "<ipython-input-15-524d2c9ab16f>", line 1
x, = !locate .hgrc | head -1
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
Python …
在Python 3中,以下内容返回一个map对象:
map(lambda x: x**2, range(10))
Run Code Online (Sandbox Code Playgroud)
如果我们想将此对象转换为列表,我们可以使用将其转换为列表list(mapobject).但是,我通过代码高尔夫发现了这一点
*x, = mapobject
Run Code Online (Sandbox Code Playgroud)
使x成为一个列表.为什么在Python 3中允许这样做?
python ×10
python-3.x ×4
dictionary ×2
tuples ×2
arrays ×1
bash ×1
binaryfiles ×1
ipython ×1
numpy ×1
performance ×1
pyright ×1
scikit-learn ×1
swap ×1