在列表中解压缩迭代器的Python方法 [英] Pythonic way to unpack an iterator inside of a list

查看:82
本文介绍了在列表中解压缩迭代器的Python方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清 pythonic 的方式在列表中解压缩迭代器.

I'm trying to figure out what is the pythonic way to unpack an iterator inside of a list.

例如:

my_iterator = zip([1, 2, 3, 4], [1, 2, 3, 4])

我提供了以下几种在列表中解压缩迭代器的方法:

I have come with the following ways to unpack my iterator inside of a list:

1)

my_list = [*my_iterator]

2)

my_list = [e for e in my_iterator]

3)

my_list = list(my_iterator)

No 1)是我最喜欢的方法,因为它减少了代码,但是我想知道这是否也是pythonic方法.或者,也许除了Python 3之外,还有另一种方法可以实现这一目标?

No 1) is my favorite way to do it since is less code, but I'm wondering if this is also the pythonic way. Or maybe there is another way to achieve this besides those 3 which is the pythonic way?

推荐答案

在研究了更多主题之后,我得出了一些结论.

After exploring more the subject I've come with some conclusions.

应该有一种-最好只有一种-显而易见的方式

There should be one-- and preferably only one --obvious way to do it

(禅宗的python )

确定哪个选项是"pythonic"选项时,应考虑一些标准:

Deciding which option is the "pythonic" one should take into consideration some criteria :

  • 多么明确,
  • 简单
  • 而且可读.

在所有条件下都能胜出的显而易见的"pythonic"选项是选项3):

And the obvious "pythonic" option winning in all criteria is option number 3):

列表=列表(my_iterator)

list = list(my_iterator)

这就是为什么很明显"没有3)是pythonic:

Here is why is "obvious" that no 3) is the pythonic one:

  • 选项3)接近自然语言,可让您立即" 想想输出是什么.
  • 选项2)(使用列表理解)(如果您是第一次看到) 该行代码将带您阅读更多内容并付钱 多一点注意.例如,当我使用列表理解时 要添加一些额外的步骤(以迭代方式调用函数 元素或使用if语句进行一些检查),所以当我看到一个 列表理解我检查内部是否有任何可能的函数调用 如果有的话.
  • 选项1)(使用*拆包)星号运算符可能会有些混乱 如果您不定期使用它,则有 4使用情况 Python中的星号:

  • Option 3) is close to natural language making you to 'instantly' think what is the output.
  • Option 2) (using list comprehension) if you see for the first time that line of code will take you to read a little bit more and to pay a bit more attention. For example, I use list comprehension when I want to add some extra steps(calling a function with the iterated elements or having some checking using if statement), so when I see a list comprehension I check for any possible function call inside or for any if statment.
  • option 1) (unpacking using *) asterisk operator can be a bit confusing if you don't use it regularly, there are 4 cases for using the asterisk in Python:

  1. 用于乘法和幂运算.
  2. 用于重复扩展列表类型的容器.
  3. 用于使用可变参数. (所谓的包装")
  4. 用于打开容器的包装.

另一个好参数是 python文档本身,我已经做了一些统计数据检查文档选择了哪些选项,为此,我选择了4个内置迭代器,并且选择了模块

Another good argument is python docs themselves, I have done some statistics to check which options are chosen by the docs, for this I've chose 4 buil-in iterators and everything from the module itertools (that are used like: itertools.) to see how they are unpacked in a list:

  • 地图
  • 范围
  • 过滤器
  • 枚举
  • itertools.

浏览文档后,我发现:使用选项1)和2)在列表中解压缩了0个迭代器,使用选项3)解开了35个迭代器.

After exploring the docs I found: 0 iterators unpacked in a list using option 1) and 2) and 35 using option 3).

结论:

在列表中解压缩迭代器的pythonic方法是:my_list = list(my_iterator)

这篇关于在列表中解压缩迭代器的Python方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆