简体   繁体   English

在python中,如何使用double for循环打印表?

[英]In python, how do I print a table using double for loops?

Here is my python code, 这是我的python代码,

from fractions import gcd 
print "| 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
print "-----------------------------------"
xlist = range(2,16)
ylist = range(2,51)
for b in ylist:
     print b, " | "
     for a in xlist:
        print gcd(a,b)

I'm having trouble printing a table that will display on the top row 2-15 and on the left column the values 2-50. 我在打印表格时遇到麻烦,该表格将在第一行2-15和左列显示值2-50。 With a gcd table for each value from each row and each column. 对于每行每一列的每个值都有一个gcd表。

Here is a sample of what I'm getting 这是我得到的样本

| | 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2 3 4 5 6 7 8 9 10 11 12 13 14 15


2 | 2 |

2 2

1 1个

2 2

You can have it much more concise with list comprehension: 您可以通过列表理解更加简洁:

from fractions import gcd
print("   |   2  3  4  5  6  7  8  9 10 11 12 13 14 15")
print("-----------------------------------------------")
xlist = range(2,16)
ylist = range(2,51)

print("\n".join(" ".join(["%2d | " % b] + [("%2d" % gcd(a, b)) for a in xlist]) for b in ylist))

Output: 输出:

   |   2  3  4  5  6  7  8  9 10 11 12 13 14 15
-----------------------------------------------
 2 |   2  1  2  1  2  1  2  1  2  1  2  1  2  1
 3 |   1  3  1  1  3  1  1  3  1  1  3  1  1  3
 4 |   2  1  4  1  2  1  4  1  2  1  4  1  2  1
 5 |   1  1  1  5  1  1  1  1  5  1  1  1  1  5
 6 |   2  3  2  1  6  1  2  3  2  1  6  1  2  3
 7 |   1  1  1  1  1  7  1  1  1  1  1  1  7  1
 8 |   2  1  4  1  2  1  8  1  2  1  4  1  2  1
 9 |   1  3  1  1  3  1  1  9  1  1  3  1  1  3
10 |   2  1  2  5  2  1  2  1 10  1  2  1  2  5
11 |   1  1  1  1  1  1  1  1  1 11  1  1  1  1
12 |   2  3  4  1  6  1  4  3  2  1 12  1  2  3
13 |   1  1  1  1  1  1  1  1  1  1  1 13  1  1
14 |   2  1  2  1  2  7  2  1  2  1  2  1 14  1
15 |   1  3  1  5  3  1  1  3  5  1  3  1  1 15
16 |   2  1  4  1  2  1  8  1  2  1  4  1  2  1
17 |   1  1  1  1  1  1  1  1  1  1  1  1  1  1
18 |   2  3  2  1  6  1  2  9  2  1  6  1  2  3
19 |   1  1  1  1  1  1  1  1  1  1  1  1  1  1
20 |   2  1  4  5  2  1  4  1 10  1  4  1  2  5
21 |   1  3  1  1  3  7  1  3  1  1  3  1  7  3
22 |   2  1  2  1  2  1  2  1  2 11  2  1  2  1
23 |   1  1  1  1  1  1  1  1  1  1  1  1  1  1
24 |   2  3  4  1  6  1  8  3  2  1 12  1  2  3
25 |   1  1  1  5  1  1  1  1  5  1  1  1  1  5
26 |   2  1  2  1  2  1  2  1  2  1  2 13  2  1
27 |   1  3  1  1  3  1  1  9  1  1  3  1  1  3
28 |   2  1  4  1  2  7  4  1  2  1  4  1 14  1
29 |   1  1  1  1  1  1  1  1  1  1  1  1  1  1
30 |   2  3  2  5  6  1  2  3 10  1  6  1  2 15
31 |   1  1  1  1  1  1  1  1  1  1  1  1  1  1
32 |   2  1  4  1  2  1  8  1  2  1  4  1  2  1
33 |   1  3  1  1  3  1  1  3  1 11  3  1  1  3
34 |   2  1  2  1  2  1  2  1  2  1  2  1  2  1
35 |   1  1  1  5  1  7  1  1  5  1  1  1  7  5
36 |   2  3  4  1  6  1  4  9  2  1 12  1  2  3
37 |   1  1  1  1  1  1  1  1  1  1  1  1  1  1
38 |   2  1  2  1  2  1  2  1  2  1  2  1  2  1
39 |   1  3  1  1  3  1  1  3  1  1  3 13  1  3
40 |   2  1  4  5  2  1  8  1 10  1  4  1  2  5
41 |   1  1  1  1  1  1  1  1  1  1  1  1  1  1
42 |   2  3  2  1  6  7  2  3  2  1  6  1 14  3
43 |   1  1  1  1  1  1  1  1  1  1  1  1  1  1
44 |   2  1  4  1  2  1  4  1  2 11  4  1  2  1
45 |   1  3  1  5  3  1  1  9  5  1  3  1  1 15
46 |   2  1  2  1  2  1  2  1  2  1  2  1  2  1
47 |   1  1  1  1  1  1  1  1  1  1  1  1  1  1
48 |   2  3  4  1  6  1  8  3  2  1 12  1  2  3
49 |   1  1  1  1  1  7  1  1  1  1  1  1  7  1
50 |   2  1  2  5  2  1  2  1 10  1  2  1  2  5

This works in Python2 and Python3. 这适用于Python2 Python3。 If you want zeros at the beginning of each one-digit number, replace each occurence of %2d with %02d . 如果您希望在每个一位数字的开头都为零,请用%02d替换每次出现的%2d You probably shouldn't print the header like that, but do it more like this: 您可能不应该那样打印标题,而应这样做:

from fractions import gcd
xlist = range(2, 16)
ylist = range(2, 51)
string = "   | " + " ".join(("%2d" % x) for x in xlist)
print(string)
print("-" * len(string))

print("\n".join(" ".join(["%2d | " % b] + [("%2d" % gcd(a, b)) for a in xlist]) for b in ylist))

This way, even if you change xlist or ylist , the table will still look good. 这样,即使您更改xlistylist ,该表仍将看起来不错。

Your problem is that the python print statement adds a newline by itself. 您的问题是python打印语句本身添加了换行符。

One solution to this is to build up your own string to output piece by piece and use only one print statement per line of the table, like such: 一种解决方案是构建自己的字符串以逐段输出,并且在表的每一行仅使用一个print语句,例如:

from fractions import gcd 
print "| 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
print "-----------------------------------"
xlist = range(2,16)
ylist = range(2,51)
for b in ylist:
     output=str(b)+" | " #For each number in ylist, make a new string with this number
     for a in xlist:
        output=output+str(gcd(a,b))+" " #Append to this for each number in xlist
     print output #Print the string you've built up

Example output, by the way: 示例输出,顺便说一句:

| 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-----------------------------------
2 | 2 1 2 1 2 1 2 1 2 1 2 1 2 1
3 | 1 3 1 1 3 1 1 3 1 1 3 1 1 3
4 | 2 1 4 1 2 1 4 1 2 1 4 1 2 1
5 | 1 1 1 5 1 1 1 1 5 1 1 1 1 5
6 | 2 3 2 1 6 1 2 3 2 1 6 1 2 3
7 | 1 1 1 1 1 7 1 1 1 1 1 1 7 1
8 | 2 1 4 1 2 1 8 1 2 1 4 1 2 1
9 | 1 3 1 1 3 1 1 9 1 1 3 1 1 3

You can specify what kind of character end the line using the end parameter in print. 您可以使用print中的end参数指定哪种字符结束行。

from fractions import gcd 
print("| 2 3 4 5 6 7 8 9 10 11 12 13 14 15")
print("-----------------------------------")
xlist = range(2,16)
ylist = range(2,51)
for b in ylist:
     print(b + " | ",end="")
     for a in xlist:
        print(gcd(a,b),end="")
     print("")#Newline

If you are using python 2.x, you need to add from __future__ import print_function to the top for this to work. 如果您使用的是python 2.x,则需要from __future__ import print_function添加到顶部,以使其正常工作。

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

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