如何使这个按字母顺序排列的列表在python的输出中看起来更好

2024-06-28 19:53:53 发布

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

它在大多数情况下都能工作,但是输出看起来非常混乱,所以我怎样才能清理它呢?如果可能的话,我也想知道如何表达伪代码。你知道吗

#open file with list
infile  = open("unsorted_fruits.txt", "r") 
#open file writing to
outfile = open("sorted_fruits.txt", "w")      
#create variable to work with list
all_lines = infile.readlines()              
for line in all_lines:                      
    print (line,)          

#this function has sorted other list
def insertion_sort(list):                   
    for index in range(1, len(list)):
        value = list[index]
        i = index - 1
        while i >= 0:
            if value < list[i]:
                list[i+1] = list[i]
                list[i] = value         
                i = i - 1               
            else:
                break  

#calling the function to sort 
insertion_sort(all_lines)                   
all_sorted = str(all_lines)                                                                  
#print list to show its sorted
print (all_sorted)
#write the sorted list to the file
outfile.write(all_sorted)                                    

infile.close()                              
outfile.close()
exit() 

你知道吗输入:木瓜你知道吗

猕猴桃

扎波特布兰科

哈克贝利

香蕉

无花果

石灰

西瓜

香草

伊萨斯

罗望子

乌姆科洛

木瓜

苹果

因布

接骨木

朱纳贝利

芒果

草莓

油桃

日期

樱桃

橙色

西瓜

葡萄

覆盆子

据输出输出输出:“\n',\n'''''''\n''''''''\n''''''''\n''''''''\n'''''\n'''''''\n''''''''\n''''''''''\n'''''''''',\n''''''\n'''''''''',\n'''''''''\n''''''''',\n'''''''''\n'''''''\n'''''''''''\n''''''''''''\n'''''''',\n'''''''''''\n''''''''''\n''''''''''''','油桃\n','橘子\n','番木瓜\n'、'木瓜\n'、'覆盆子\n'、'草莓\n'、'罗望子\n'、'乌姆科洛\n'、'香草\n'、'西瓜\n'、'西瓜\n'、'伊萨斯\n'、'扎波特白葡萄酒\n']


Tags: thetoindexvalueopenallsortinfile
3条回答

现在使用以下代码输出文件如下所示。任何人都有让它看起来更好的建议。 一项新的法律实施了一项新的法律实施了一项新的法律实施了一项新的政府实施了一项新的政府实施了一项新的政府实施了一项新的政府实施了一项新的政府实施一项新的政府实施了一项新的经济发展。一项新的政府实施了一项新的法律实施了一项新的政府实施了一项新的政府实施了一项新的政府实施了一项商业服务,这项新的政府实施实施了一项法律实施了一项新的法律实施了一项新的政府实施了一项新的政府实施了一项新的政府实施了一项新的政府实施的政府实施一项政策。这项新的政府实施的政府实施了一项新的政府实施了一项新的政府实施了一项新的政府实施的政策。这项新的政府实施的政府实施实施了一项政策,这项政策的政府实施实施了一项政策的政府实施了一项新的政府实施的政府实施了一项政策,即“,”,'banana\n'、'cherry\n'、'date\n'、'elderberry\n'、'fig\n'、'grape\n'、'huckleberry\n'、'imbu\n'、'juneberry\n'、'kiwifruit\n'、'lime\n'、'mango\n'、'油桃\n'、'orange\n'、'papapaya\n'、'quince\n'、'raspberry\n'、'草莓\n'、'tamarin\n'、'umkolo\n'、'香草\n'、'西瓜\n'、'xigua\n'、'yiessas\

#open file with list
infile  = open("unsorted_fruits.txt", "r") 
#open file writing to
outfile = open("sorted_fruits.txt", "w")      
#create variable to work with list
all_lines = infile.readlines()              
for line in all_lines:                      
    print (line,)          

#this function has sorted other list
def insertion_sort(list):                   
    for index in range(1, len(list)):
        value = list[index]
        i = index - 1
        while i >= 0:
            if value < list[i]:
                list[i+1] = list[i]
                list[i] = value         
                i = i - 1               
            else:
                break  

#calling the function to sort and make it look nicer
insertion_sort(all_lines)
for word in all_lines:
    if word.rstrip('\n'):
        print(word.rstrip('\n'))
for word in all_lines:
    if word.rstrip('\n'):
       outfile.write(word.rstrip('\n'))

all_sorted = str(all_lines)

#write the sorted list to the file
outfile.write(all_sorted)                                    

infile.close()                              
outfile.close()
exit() 

问题不在于输出。这是你阅读水果清单的方式。你真的想排序水果,而不是行。你知道吗

输入文件中的许多行都是空白的,您不想包含它们。而且您可能也不想将后面的'\n'视为每个水果的一部分。你知道吗

因此,与其迭代内嵌读线(),我建议遍历填充阅读()。拆分线()。这会自动拖动尾部\n,而当你在它的时候,你可以放下空白行。你知道吗

with open('unsorted_fruits.txt', 'r') as infile:
    fruits = [f for f in infile.read().splitlines() if f != '']

然后你可以把水果分类而不是排成一行。如果不想将最终输出写成Python列表的字符串表示形式,可以使用print()。你知道吗

for fruit in fruits:
    print(fruit, file=outfile)

或者文件.writelines()

outfile.writelines(f + '\n' for f in fruits)

这可能是由于文件末尾有额外的换行符,这些换行符是在readlines()函数中提取的。你知道吗

当您要打印最终列表中的每个项目时,只需过滤掉那些仅为新行的项目:

for word in all_sorted:
    if word.rstrip('\n'):
        print(word.rstrip('\n'))

rstrip()函数(right strip)去掉单词末尾的所有换行符。如果单词只是一个新行,那么rstrip将返回一个空字符串,该字符串将被If语句过滤掉。你知道吗

相关问题 更多 >