python - 在 Python 中解压单个可迭代元素的首选方法
问题描述
鉴于我有单个元素可迭代,例如元组
t = (1,)
有没有一种将元组解包到变量中的首选方法?有很多选择,各有优劣。
unpacked, = t # Elegant but easy to overlook.
(unpacked,) = t # Maybe a bit clunky?
unpacked, *_ = t # Creates the impression, that there might be more elements.
unpacked = t[0] # Same as above.
*_, unpacked = t # Same as above, but also gives the impression
# that we are accessing the last element.
[unpacked] = t # Elegant and obvious.
PEP8 对此有什么建议吗?
解决方案
推荐阅读
- p2p - BitTorrent / P2P NAT 穿越?
- regex - AWS Athena 中的 Regex Serde 可以处理换行符吗?
- html - 如何使用 BeautifulSoup 获取特定元素之后的所有文本?
- asp.net-core - ASP.NET Core 3.1,InvalidOperationException 在使用注入到作用域服务中的用户管理器时,然后注入到 SignalR 集线器
- python - 更新给定索引 pandas 的列值
- reactjs - useEffect 和繁忙的线程
- python - 气流 DAG 调度程序循环在 Azure 数据工厂上产生高成本
- python - 具有 2 个可变轴的 Python 散景图
- java - 如何使用 Java 在 Android 中突出显示视图中的选择
- go - 如何让 openapi-generator 在多个包中生成 golang 代码?