使用format()打印给定字符串列表的三行输出

2024-06-01 06:41:25 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,伙计们,我还有一个问题。我只是想学习做事情的最佳方法。我相信这很容易。你知道吗

基本上,我需要在标准输出中输出一些国家的名称,如下所示:

Afghanistan                   Albania                       Armenia
Bangladesh                    Benin                         Bhutan
Bolivia                       Burkina Faso                  Burundi
Cabo Verde                    Cambodia                      Cameroon
Central African Republic      Chad                          Comoros
Congo                         Cote D'Ivoire                 D.P.R. Of Korea
D.R. Of The Congo             Djibouti                      Egypt
El Salvador                   Eritrea                       Ethiopia
Gambia                        Georgia                       Ghana
Guatemala                     Guinea                        Guinea-Bissau
Guyana                        Haiti                         Honduras
India                         Indonesia                     Kenya
Kiribati                      Kosovo                        Kyrgyzstan
Lao People'S Dr               Lesotho                       Liberia
Madagascar                    Malawi                        Mali
Marshall Islands              Mauritania                    Micronesia (Fs Of)
Mongolia                      Morocco                       Mozambique
Myanmar                       Nepal                         Nicaragua
Niger                         Nigeria                       Pakistan
Papua New Guinea              Paraguay                      Philippines
Republic Of Moldova           Rwanda                        Samoa
Sao Tome And Principe         Senegal                       Sierra Leone
Solomon Islands               Somalia                       South Sudan
Sri Lanka                     State Of Palestine            Sudan
Swaziland                     Syrian Arab Republic          Tajikistan
Togo                          U.R. Of Tanzania: Mainland    Uganda
Ukraine                       Uzbekistan                    Vanuatu
Viet Nam                      Yemen                         Zambia
Zanzibar                      Zimbabwe

我已经编写了一个函数来完成这个任务,叫做table(countries)。虽然我所写的工作,这似乎不是最有效的方式来做到这一点。虽然我不必为赋值使用format()函数,但这是我最熟悉的。如果有更好的方法,请随意告诉我另一种方法,但请记住这是我的第一门编程语言/课程。你知道吗

以下是我编写的代码:

def table(countries):
    counter = 0 #Four counting when I've printed 3 columns
    for outer in range(len(countries)):
        print(format(countries[outer], '30'), end ='')
        counter  +=1 
        if counter  == 3:
            counter  = 0
            print() #Starts a new column

提前谢谢!你知道吗


Tags: of方法函数formatcountertablecountriesprint
2条回答

您可以尝试列表理解:

countries = ['aaa','bbb','ccc','dd','eeeee','fff','ggggggg']
print('\n'.join([" ".join([country.ljust(30) for country in countries[i:i+3]]) for i in range(0,len(countries),3)]))

这将导致:

aaa                            bbb                            ccc                           
dd                             eeeee                          fff                           
ggggggg       

首先,我们把每个国家分成3个国家

然后,我们将子列表中的每个国家变成固定长度的字符串,用空格填充到长度为30-[country.ljust(30) for country in countries[i:i+3]]

之后,我们将每个子列表连接成一个字符串-" ".join(...)

最后,我们将子列表的每个字符串连接成一个带有行尾符号的字符串-'\n'.join(...)

值得注意的是,在每个字符串的末尾都会有尾随空格-如果不需要,可以调用rstrip()来除去它们。你知道吗

你可以修改,不需要另一个变量。你知道吗

def table(countries):
    for outer in range(len(countries)):
      if outer%3  == 0:
        print() #Starts a new column
      print(format(countries[outer], '30'), end ='')

相关问题 更多 >