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

爱喝马黛茶的安东尼
Release: 2019-06-24 17:40:42
Original
13266 people have browsed it

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]
Copy after login

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]
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!