如何将元素拉到列中而不会丢失类型信息
我想将一组列列表的字段拉到列中。但是,此操作失去了类型。
from typing import NamedTuple
class MyTuple(NamedTuple):
one: int
two: str
one, two = zip(*[MyTuple(i, str(i)) for i in range(100)])
Pycharm的类型检查器不再知道一个
和两个
是整数和字符串。有没有办法在不丢失类型的情况下这样做?
I'd like to zip fields of a list of tuples into columns. However, this operation loses the types.
from typing import NamedTuple
class MyTuple(NamedTuple):
one: int
two: str
one, two = zip(*[MyTuple(i, str(i)) for i in range(100)])
PyCharm's type checker does no longer know that one
and two
are integers and strings. Is there a way to do this without losing types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个
是(1、2、3、4,...,100)和
两个
是('1','2','3','3','4',...,'100')因此,
and
and 和<代码>两个是元组。如果您编写
type(一个[0])
,那么可能还可以。one
is (1, 2, 3, 4, ..., 100)and
two
is ('1', '2', '3', '4', ..., '100')therefore, type of both
one
andtwo
is tuple.if you write
type(one[0])
then it might be okay.