如何将元素拉到列中而不会丢失类型信息

发布于 2025-01-18 07:04:22 字数 324 浏览 2 评论 0原文

我想将一组列列表的字段拉到列中。但是,此操作失去了类型。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅紫色的梦幻 2025-01-25 07:04:22

一个是(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 and two is tuple.

if you write type(one[0]) then it might be okay.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文