Home >Backend Development >Python Tutorial >How to store input values into a list in Python
#How does python store input values into a list? The following is a specific method for you:
Usually the input string needs to be split, and python uses the split() function for splitting.
Related recommendations: "Python Video Tutorial"
Example:
>>> x=input() 1,2,3,4 >>> xlist=x.split(",") >>> print(xlist) ['1', '2', '3', '4'] >>> xlist = [int(xlist[i]) for i in range(len(xlist))] #for循环,把每个字符转成int值 >>> print(xlist) [1, 2, 3, 4]
The parameters of the split("") function can be any delimiter, including (a,b,c….;1,2,3…;%,!,*,space)
>>> x=input() 1 2 3 4 >>> xlist=x.split(" ") >>> print(xlist) ['1', '2', '3', '4'] >>> xlist = [int(xlist[i]) for i in range(len(xlist))] >>> print(xlist) [1, 2, 3, 4]
The above is the detailed content of How to store input values into a list in Python. For more information, please follow other related articles on the PHP Chinese website!