Python 并排打印 2 个均匀填充的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27663924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:05:04  来源:igfitidea点击:

Printing 2 evenly populated lists side by side evenly

pythonstringlistformattingrows

提问by Hymanson Blankenship

I'm using the following code to produce 2 lists, nameList and gradeList.

我正在使用以下代码生成 2 个列表,nameList 和 GradeList。

nameList[]        
gradeList[]
for row in soup.find_all('tr'):
        name = row.select('th strong')
        grade = row.select('td label')
        if grade and name:
            if "/" in grade[0].text:
                gradeList.append(grade[0].text)
                nameShort = re.sub(r'^(.{20}).*$', '\g<1>...', str(name[0].text))
                nameList.append(nameShort)

Producing something like:

生产类似的东西:

nameList = [“grade 1”,”grade 2222222222”,”grade 3”]
gradeList = [“1/1”,”2/2”,”100000/100000”]

I want the program to print the lists in 2 clean columns, side by side. Within each column, I want the data to align to the left. The lists (without fail) will always be evenly populated. The first column (nameList) will never be longer than 25 characters. What I am looking for would be similar to the following:

我希望程序在 2 个干净的列中并排打印列表。在每一列中,我希望数据向左对齐。列表(不会失败)将始终均匀填充。第一列 (nameList) 永远不会超过 25 个字符。我正在寻找的内容类似于以下内容:

        Assignment          Grade
0       grade 1             1/1
1       grade 2222222222    2/2
2       grade 3             100000/100000

I've tried to use pandas and it worked, but the formatting was weird and out of place. It wouldn't align to the left like I want. I believe this happened because the data each has a different character length in both lists (shown above).

我试过使用熊猫,它奏效了,但格式很奇怪而且不合适。它不会像我想要的那样向左对齐。我相信这是因为数据在两个列表中都有不同的字符长度(如上所示)。

采纳答案by falsetru

Using str.format:

使用str.format

nameList = ["grade 1", "grade 2222222222", "grade 3"]
gradeList = ["1/1", "2/2", "100000/100000"]

fmt = '{:<8}{:<20}{}'

print(fmt.format('', 'Assignment', 'Grade'))
for i, (name, grade) in enumerate(zip(nameList, gradeList)):
    print(fmt.format(i, name, grade))

output:

输出:

        Assignment          Grade
0       grade 1             1/1
1       grade 2222222222    2/2
2       grade 3             100000/100000

Alternatively, you can also use printfstyle formatting using % operator:

或者,您也可以printf使用 % 运算符使用样式格式

fmt = '%-8s%-20s%s'

print(fmt % ('', 'Assignment', 'Grade'))
for i, (name, grade) in enumerate(zip(nameList, gradeList)):
    print(fmt % (i, name, grade))

回答by Bob Haffner

Given two lists

给定两个列表

nameList = ['grade 1','grade 2222222222','grade 3']
gradeList = ['1/1','2/2','100000/100000']

tab separated format. using zip() two iterate through both lists at the same time

制表符分隔格式。使用 zip() 两个同时遍历两个列表

print 'Assignment \t\tGrade' 
for n,g in zip(nameList,gradeList):
    print n + '\t\t\t' + g


Assignment         Grade
grade 1            1/1
grade 2222222222   2/2
grade 3            100000/100000