简体   繁体   中英

Sorting list output: UTF-8

I am taking an input file that contains lists of numbers with different statements separated by spaces and taking those numbers, adding two next to each other and printing out a word out if UTF-8. so if a list of numbers were 67 5 100 1 11 97 98 10 1 110 , the numbers would become 72, 101, 108, 108 and 111 which reads Hello in UTF-8.

I currently have the following code:

file_name = input('Enter filename: ')
input_file = open(file_name, 'r')
word_list = []
count3 = 0

for line in input_file:
    count1 = 0
    count2 = 1
    count3 += 1
    word_str = ''
    line = line.split()
    length = len(line)

    while count1 < length:
        num_char = int(line[count1]) + int(line[count2])
        count1 += 2
        count2 += 2
        word_str += chr(num_char)
    word_list.append(word_str)

print (word_list)

example input file:

67 5 100 1 11 97 98 10 1 110 
15 72 10 101 47 67 88 20 94 6 22 11

61 11 93 4 73 39 78 34 17 104
23 43 11 93 65 52 20 96 66 31 86 24 40 61 102 13 50 51
73 43 28 73 8 89 31 68 77 27 24 77 42 72 15 24 64 51
25 75 7 90 10 111 17 16

From this code I get the output:

['Hello', 'World!', '', 'Happy', 'Bhutanese', "teacher's", 'day!']

My problem is that I need to sort the output in my list with the last statement first and the first statement last.

So for the provided numbers the expected output would read:

['Happy Bhutanese teacher's day!',
'Hello World!']

Any help is greatly appreciated

It seems that you don't want a list of individual words - you want a list of sentences , where the sentences are separated blank lines.

For clarity, you should rename word_list to sentence_list . I'll refer to it as sentence_list from now on.

To build sentences, put a space between each word. Since sentences span multiple lines, you don't want to re-initialize word_str in every loop - you'll need to keep it around. To do this, initialize it once , before the loop.

word_str = ''
for line in input_file:
    ...

Instead of always appending the line's word to the list, word_list.append(word_str) , you'll want to handle it differently for a word than for a blank line.

If the line just processed was a word, add a space to the end.

If it was a blank line, add the current word_str to the sentence_list , and reset word_str to an empty string.

You can decide which to do by checking the line's length: blank lines have length 0.

if length == 0:
    sentence_list.append(word_str)
    word_str = ''
else:
    word_str += ' '

Since you want the last sentence to appear first in the list, you'll need to reverse the list before printing it.

sentence_list.reverse()
print(sentence_list)

Then instead of using append , add new word to the front by hand or use insert :

word_list = []
...
for ...:
...
    while ...:
        ...
    word_list = [word_str] + word_list
    #word_list.insert(0, word_str)

print(word_list)

There is also reverse function in list . You could just say:

word_list.reverse()
print(word_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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