Python refresh question-use stdin for input and output-random array deduplication, pythonstdin
Original question address:
Explicit Random Number
The rules for input of a given variable may vary in different websites and different given questions. Generally, the most common input method is the sys. stdin method.
For example, this simple question:
1.Input description:Input Multiple rows, enter the number of random integers, and then enter the corresponding number of Integers
2.Output description:Returns multiple rows. The processed results
3. processing requirements: the random integer group must be de-duplicated and sorted from low to high to output the sorted results.
First, let's look at the input. For the input, the total length of the input number depends on the number of the first input. Therefore, we should first obtain the first input, determine the length of the next input, and then continue to obtain the input.
import sysvar1=int(sys.stdin.readline())
Records the first input results to var1
Next, we are going to store the following input in var2. for example, var1 = 11, that is, we need to input 11 variables. If we want to implement this through a for loop, that is, the input can be completed only after 11 cycles:
The method here is to initialize var2 to be empty first, and then not accept an input. append the input result to var2. After the loop ends, the var2 input is obtained:
Var2 = [] for I in range (var1): line = int (sys. stdin. readline (). strip () # strip () removes the line break after each line Input. For example, if the input is 10, the actual print line result is 10 \ n var2.append (line)
For each loop, line is updated to a new input and appended to var2. The input is completed after the loop is completed. Strip () is used to remove line breaks.
To store the variables, the solution is as follows:
V = list (set (var2) # Use the set () method to deduplicate var2 v. sort () # sort the de-duplicated result v.
V is the result we need to output, but note the output requirements here, we can still use the for loop for output, that is:
for i in v: print i
Combine all the above processes and nest them in while True: try:... break t: break:
import syswhile True: try: var1=int(sys.stdin.readline().strip()) var2=[] for i in range(var1): line=int(sys.stdin.readline().strip()) var2.append(line) v=list(set(var2)) v.sort() for i in v: print i except: break