Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

python - 将列表中的所有字符串转换为int(Convert all strings in a list to int)

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

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

1 Reply

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...