+ 10
What happens here?(python)
print hash(tuple(map(int,raw_input().strip().split(" "))))
2 Antworten
+ 5
Note: this is only valid for Python 2
raw_input()
takes input from the user minus the new line (enter/return) as a string value. You can also pass in the prompt you want as a string. example raw_input("Enter 5 numbers separated by a space: ")
So we see:
Enter 5 numbers separated by a space: 1 2 3 4 5
strip()
then removes any leading and trailing spaces from that string and returns it.
" 1 2 3 4 5 " becomes "1 2 3 4 5"
split(" ")
takes the string and returns it as a list of words, using the space " " as the separator.
example: "what have you learned on sololearn"
becomes: ["what", "have", "you", "learned", "on", "sololearn"]
"1 2 3 4 5" becomes ["1", "2", "3", "4", "5"]
map()
usually takes a function or lambda as the first argument that operates on the second argument. In this case it is using int to convert the strings from the list into an integer.
["1", "2", "3", "4", "5"] becomes [1, 2, 3, 4, 5]
tuple()
converts the list of integers to a tuple in the same order with the same values.
example: this [1, 2, 3, 4, 5] becomes (1, 2, 3, 4, 5)
hash()
Will return the hash value of the object (tuple in this case) if it has one.
If we were to enter 1 2 3 4 5 at the prompt you would see something like 8315274433719620810 as the output.
+ 10
Thank you for your response👍