在列表中每行打印n个元素 [英] Printing n elements per line in a list
问题描述
如果有一个从1到100的列表,那么最简单,最优雅的方式是什么?
将它们打印出来,这样每行只有n个元素。
因此,如果n = 5,打印列表将如下所示:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
等
我在之前的帖子中搜索产生的方法来打印所有
单行列表中的值,但这不是我想要的。我觉得
就像有一个简单,漂亮的方法来做到这一点。我认为有可能使用while循环和一些丑陋的切片来破解它,但希望我很好地b / b
缺少一些东西
>
" unexpected" < su *********** @ gmail.comwrites:
如果有一个从1到100的列表,那么最简单的是什么,最优雅的方式将
打印出来,这样每行只有n个元素。
因此,如果n = 5,打印列表会显示喜欢:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
等
我在以前的帖子中搜索产生的方法可以在一行上打印列表的所有
值,但是''不是我想要的。我觉得
就像有一个简单,漂亮的方法来做到这一点。我认为有可能使用while循环和一些丑陋的切片来破解它,但希望我很好地b / b
缺少一些东西
最直接的方式是(未经测试):
$ x $ b for x in xrange(0,100,5):
print''''。join([i:i + 5]中j的str(j))
其中a是数组。如果你喜欢无坐标的话。风格,你
可以使用类似的东西:
for x,y in itertools.groupby(a,lambda n:n // 5):
print''''。join(str(k)for k in y)
我希望我做对了。
Paul Rubin< http://ph****@NOSPAM.invalidwrites:
for x,y in itertools.groupby( a,lambda n:n // 5):
print''''。join(str(k)for k in y)
我希望我做对了。
当然我的意思是说
for x,y in itertools.groupby(enumerate(a ),lambda(n,x):n // 5):
print''''。join(str(k)代表n,k代表y)
这仍然是丑陋的,我从来没有找到一个非常好的解决方案。
2006年8月15日16:51:29 - 0700,
意外 < su *********** @ gmail.comwrote:
如果有一个从1到100的列表,那么最简单的是什么,最优雅的方式将
打印出来,这样每行只有n个元素。
因此,如果n = 5,打印列表将如下所示:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
等
我在之前的帖子中搜索产生的方法可以在一行上打印列表的所有
值,但这不是什么我想要。我觉得
就像有一个简单,漂亮的方法来做到这一点。我认为有可能使用while循环和一些丑陋的切片来破解它,但希望我很好地b / b
缺少一些东西
也许不是最漂亮的,但是从现在开始六个月我想不出任何更简单的东西:
:
counter = 0
$ _ $ b for the_list中的an_element:
打印an_element,
counter = counter + 1
if counter == n:
打印
counter = 0
问候,
Dan
-
Dan Sommers
< http://www.tombstonezero.net/dan/>
我希望人们按字母顺序死亡。 - 我的妻子,系谱学家
If have a list from 1 to 100, what''s the easiest, most elegant way to
print them out, so that there are only n elements per line.
So if n=5, the printed list would look like:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
etc.
My search through the previous posts yields methods to print all the
values of the list on a single line, but that''s not what I want. I feel
like there is an easy, pretty way to do this. I think it''s possible to
hack it up using while loops and some ugly slicing, but hopefully I''m
missing something
"unexpected" <su***********@gmail.comwrites:
If have a list from 1 to 100, what''s the easiest, most elegant way to
print them out, so that there are only n elements per line.
So if n=5, the printed list would look like:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
etc.
My search through the previous posts yields methods to print all the
values of the list on a single line, but that''s not what I want. I feel
like there is an easy, pretty way to do this. I think it''s possible to
hack it up using while loops and some ugly slicing, but hopefully I''m
missing somethingThe most direct way would be something like (untested):
for i in xrange(0, 100, 5):
print '' ''.join(str(j) for j in a[i:i+5])
where a is the array. If you prefer a "coordinate-free" style, you
could use something like:
for x,y in itertools.groupby(a, lambda n: n//5):
print '' ''.join(str(k) for k in y)
I hope I got that right.
Paul Rubin <http://ph****@NOSPAM.invalidwrites:for x,y in itertools.groupby(a, lambda n: n//5):
print '' ''.join(str(k) for k in y)
I hope I got that right.Of course I meant to say
for x,y in itertools.groupby(enumerate(a), lambda (n,x): n//5):
print '' ''.join(str(k) for n,k in y)This is still way ugly and I''ve never found a really good solution.
On 15 Aug 2006 16:51:29 -0700,
"unexpected" <su***********@gmail.comwrote:
If have a list from 1 to 100, what''s the easiest, most elegant way to
print them out, so that there are only n elements per line.
So if n=5, the printed list would look like:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
etc.
My search through the previous posts yields methods to print all the
values of the list on a single line, but that''s not what I want. I feel
like there is an easy, pretty way to do this. I think it''s possible to
hack it up using while loops and some ugly slicing, but hopefully I''m
missing somethingPerhaps not the prettiest, but I can''t think of anything simpler to read
six months from now:
counter = 0
for an_element in the_list:
print an_element,
counter = counter + 1
if counter == n:
counter = 0
Regards,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
这篇关于在列表中每行打印n个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!