Simple exploration of Python input stream

This year's Blue Bridge Cup adds a new python game, CCF-CSP certification can now also use python language, my partner and around some small pyhton chosen to use the algorithm to do the title. I just returned from C / C ++ to python has a lot of discomfort, the first problem encountered is the python's input problem


Let me give a simple example, enter two integers, separated by a space,
in a very simple implementation in C ++

int a,b;
cin>>a>>b;

C ++ input stream of >>support not only separated by a carriage return input, also supports separated by a space enter
just beginning to learn python I have this preconceived notion, just write such python code

a=input()
b=input()
print(a)
print(b)

Written grammar is no problem, run the input 12, but when I enter the 2 Press the Enter key, the cursor will continue to prompt me to enter, I subconsciously press the Enter the next output, this should appear in the two lines 1 and 2 actually appeared in a row

1 2

Modify the program, remove the print (b)

print(a)

In running the program, enter 12, click Run

1 2

The results unchanged, apparently 1, spaces, both assigned to the a 2
Why is there such a result of it, I am here to help with inquiries about it

>>> help(input)
Help on built-in function input in module builtins:

input(prompt=None, /)
    Read a string from standard input.  The trailing newline is stripped.
    
    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.
    
    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
    On *nix systems, readline is used if available.

A the Read stringfrom Standard INPUT. Phrase it explains very clearly, input () is a string read. In other words, just when I press the space bar, not the end of the current a = input () of this action, but I continue to read the next 2 input from the keyboard, so the 2 b have received.
I continue to test on the Python Shell

>>> a=input()
1 2
>>>print("a=",a,"\na's type is",type(a))
a= 1 2
a's type is <class 'str'>

python's input () is a bit like C ++ cin.getline(), you can accept a string with spaces, press Enter before the end of the current input.
Even if your input is integer, floating point, will be automatically converted to a string

I PAT (Basic Level) Practice looking for a OJ in the title to practice my encounter such problems (input with spaces) in OJ how to do it

1010 univariate polynomial derivation

Monadic design function of the derivative of the polynomial. (Note: x ^ n (n is an integer) of the first derivative of nx ^ n-1.)
Input format:
In a manner descending exponential input nonzero polynomial coefficient and the exponent (integer not exceeding the absolute value of both the 1000 ). Between numbers to 空格separate.
Output format:
with the same input format output derivatives of the polynomial coefficients and the index of non-zero entries. Between numbers separated by spaces, but the end can not have extra spaces . Note that the "zero polynomial" of the index and the coefficient is 0, but is represented as 00.
Sample input:
34-5261-20
Output Sample:
123-10160

ok, read a title feel very OJ simple question, but for the one week was just learning python head full of C ++ is not easy for me, just let me enter a headache for a long time. I carefully back to the thought for a moment, the problem is that I have to input () is assigned to a variable 字符串with no clear concept

Well, now we believe that input () input is a string, the next is the operation of the string, but the characters are a direct operation is very blunt, I first used split()to convert it to 列表, which then transformed its character integer, and the rest of the list of actions to fill the gap

st=input()
s=[int(x) for x in st.split()]
result=[]
for i in range(0,len(s),2):
    if s[i+1]!=0:
        result.append(str(int(s[i]*s[i+1])))
        result.append(str(s[i+1]-1))
if len(result)==0:
    print('0 0')
else:
    print(' '.join(result))

In fact, when you skilled python, in front of two can be simplified to the following code

s=[int(x) for x in input().split()]

input () is a string, bold use of .split () just fine

In order to strengthen the input () is a string that the concept of it, I prepared a question

1009 ironic

Given a word of English, asking you to write a program, all the words of the sentence order reversed output.
Input format:
test input comprising a test case, given the string length does not exceed a total of 80 in a row. String composed of several words and a number of spaces, where the word is English letters (case is case) consisting of string, separated by a space between words, the input end of the sentence to ensure that no extra space.
Output format:
each test case output per line, output sentence after the reverse.
Sample input:
the Hello World Here Come the I
sample output:
Come the I Here the Hello World

After my repeated changes, the final answer only one line of code

print(' '.join(input().split()[::-1]))

First with split()the input () is converted to 字符串列表, and then use the 切片[::-1]reverse order, the last character in the list into a '' .join () 字符串output, the fusion line conversion between the input of the output stream, and a list of strings, Python use it really smooth


This is the second in Vito original blog, then I will continue to introduce a little experience of learning python. In addition to publishing blog on CSDN, I will be in great heritage of Computer Science and mineral resources plan (Resource Inheritance Plan of CUMTCS) This GitHub library resources and learning materials to share my blog

Released three original articles · won praise 4 · Views 388

Guess you like

Origin blog.csdn.net/weixin_43608722/article/details/104306943