简体   繁体   English

是否有任何单个函数来打印可迭代的值?

[英]Is there any single function to print an iterable's values?

Suppose I have an iterable:假设我有一个可迭代的:

var = "ABCDEF"

I get the iterable like this:我得到这样的迭代:

it = itertools.combinations(var,2)

Is there any single function to print all values of iterables like是否有任何单个函数可以打印可迭代的所有值,例如

printall(it)

rather than using the for loop?而不是使用for循环?

This rather depends what you want, if you want to print out all the values, you need to compute them - an iterable doesn't guarantee the values are computed until after they are all requested, so the easiest way to achieve this is to make a list :这更取决于你想要什么,如果你想打印出所有的值,你需要计算它们 - 一个迭代不能保证在它们全部被请求之后计算这些值,所以实现这一点的最简单方法是使一个列表

print(list(iterable))

This will print out the items in the normal list format, which may be suitable.这将以正常列表格式打印出项目,这可能是合适的。 If you want each item on a new line, the best option is, as you mentioned, a simple for loop:如果您希望每个项目都在一个新行上,最好的选择是,正如您所提到的,一个简单的 for 循环:

for item in iterable:
    print(item)

If you don't need the data in a specific format, but just need it to be readable (not all on one line, for example), you may want to check out the pprint module .如果您不需要特定格式的数据,而只需要它是可读的(例如,不是全部在一行上),您可能需要查看pprint模块

A final option, which I don't really feel is optimal, but mention for completeness, is possible in 3.x, where the print() function is very flexible:最后一个选项,我并不觉得是最佳选择,但为了完整性,在 3.x 中是可能的,其中print()函数非常灵活:

print(*iterable, sep="\n")

Here we unpack the iterable as the arguments to print() and then make the separator a newline (as opposed to the usual space).在这里,我们将可迭代对象解包print()的参数,然后将分隔符设为换行符(与通常的空格相反)。

您可以使用str.join方法并将迭代的每个元素加入新行。

print('\n'.join(it))

You can use format which will allow each element to be formated as you please:您可以使用 format ,这将允许您随意格式化每个元素:

>>> print '\n'.join('{:>10}'.format(e) for e in iter([1,2,'1','2',{1:'1'}]))
         1
         2
         1
         2
  {1: '1'}

Each element does not need to be a string necessarily, but must have a __repr__ method if it is not a string.每个元素不一定是字符串,但如果不是字符串,则必须具有__repr__方法。

You can then easily write the function you desire:然后,您可以轻松编写所需的函数:

>>> def printall(it,w): print '\n'.join('{:>{w}}'.format(e,w=w) for e in it)
>>> printall([1,2,'3','4',{5:'6'}],10)
         1
         2
         3
         4
  {5: '6'}

I am using a list, but any iterable would do.我正在使用一个列表,但任何可迭代的都可以。

You can use chain() function from itertools to create iterator for var data and then just unpack using * operator of iterator您可以使用 itertools 中的chain()函数为 var 数据创建迭代器,然后使用迭代器的 * 运算符解包

>>> from itertools import chain
>>> var = 'ABCDEF'
>>> print(*chain(var))
A B C D E F
>>> print(*chain(var), sep='\n')
A
B
C
D
E
F

If you just need to iterate over existing data and print it out again you can use star operator * for this如果您只需要迭代现有数据并再次打印出来,您可以为此使用星号运算符 *

>>> print(*var)
A B C D E F
>>> print(*var, sep='\n')
A
B
C
D
E
F

If you insist on a solution which iterates without a for loop and works for infinite iterators:如果您坚持使用无需for循环进行迭代适用于无限迭代器的解决方案:

from more_itertools import consume

def printall(it):
    consume(map(print, it))

Instead of the for loop we have consume here.我们在这里consume不是for循环。
Note that in this case the iterator is consumed while printing.请注意,在这种情况下,迭代器在打印时被消耗。

This, of course, works for infinite iterators as well:当然,这也适用于无限迭代器:

from itertools import count

printall(count())

Just for fun.只是为了好玩。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM