List comprehension without [ ] in Python

后端 未结 7 2279
悲&欢浪女
悲&欢浪女 2020-11-21 05:28

Joining a list:

>>> \'\'.join([ str(_) for _ in xrange(10) ])
\'0123456789\'

join must take an iterable.

Appa

相关标签:
7条回答
  • 2020-11-21 05:59

    That's a generator, rather than a list comprehension. Generators are also iterables, but rather than creating the entire list first then passing it to join, it passes each value in the xrange one by one, which can be much more efficient.

    0 讨论(0)
提交回复
热议问题