Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In Python, I want to convert all strings in a list to integers.

(在Python中,我想将列表中的所有字符串转换为整数。)

So if I have:

(所以,如果我有:)

results = ['1', '2', '3']

How do I make it:

(我该如何做:)

results = [1, 2, 3]
  ask by Michael translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
324 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use the map function (in Python 2.x):

(使用map函数(在Python 2.x中):)

results = map(int, results)

In Python 3, you will need to convert the result from map to a list:

(在Python 3中,您需要将结果从map转换为列表:)

results = list(map(int, results))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...