Home  >  Article  >  Backend Development  >  How to store input values ​​into a list in Python

How to store input values ​​into a list in Python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-24 17:40:4213430browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn