converting list of string to list of integer [dupl

2019-02-12 08:13发布

This question already has an answer here:

How do I convert a space separated integer input into a list of integers?

Example input:

list1 = list(input("Enter the unfriendly numbers: "))

Example conversion:

['1', '2', '3', '4', '5']  to  [1, 2, 3, 4, 5]

7条回答
你好瞎i
2楼-- · 2019-02-12 09:11

Just curious about the way you got '1', '2', '3', '4' instead of 1, 2, 3, 4. Anyway.

>>> list1 = list(input("Enter the unfriendly numbers: "))
Enter the unfriendly numbers: 1, 2, 3, 4
>>> list1 = list(input("Enter the unfriendly numbers: "))
Enter the unfriendly numbers: [1, 2, 3, 4]
>>> list1
[1, 2, 3, 4]
>>> list1 = list(input("Enter the unfriendly numbers: "))
Enter the unfriendly numbers: '1234'
>>> list1 = list(input("Enter the unfriendly numbers: ")) 
Enter the unfriendly numbers: '1', '2', '3', '4'
>>> list1
['1', '2', '3', '4']

Alright, some code

>>> list1 = input("Enter the unfriendly numbers: ")
Enter the unfriendly numbers: map(int, ['1', '2', '3', '4'])
>>> list1
[1, 2, 3, 4]
查看更多
登录 后发表回答