python: A way to read in sort and output by row

  • 2021-07-24 11:19:41
  • OfStack

Title description

Given n strings, arrange n strings in lexicographic order.

Enter description:

Enter the first line as a positive integer n (1 ≤ n ≤ 1000), and the following n line is n string (string length ≤ 100), which contains only upper and lower case letters.

Output description:

The data is output in line n, and the output result is a string arranged in lexicon order.

Example 1

Input


9
cap
to
cat
card
two
too
up
boat
boot

Output


boat
boot
cap
card
cat
to
too
two
up

The python3 code is implemented as:


n=int(input())
word=[]
for i in range(n):
  word.append(input())
for i in sorted(word):
  print(i)

Analysis:

(1) input () in python3 is a string, so

n=int(input())

Convert to int.

(2) Sort usage in python:

For reference

Advanced sorting skills of python, sort and sorted

Pay attention to distinguish sort from sorted

(3) When output, output by line, then for loop can be used

For direct output, parentheses and quotation marks are displayed, which can be used in join method:


print("\n".join(sorted(word)))

That is, each element of list is connected with a carriage return


Related articles: