开发者

Lists in Python [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

What is the easiest way to convert list with str into list with int? =)

Is it possible to transform:

a = ['1', '2', '3开发者_JS百科', '4']

to

a = [1, 2, 3, 4]

Thank You!


Another way:

result = [int(x) for x in a]

This is called a list comprehension.


You could use map to apply a function to each element of a list, and a get the resulting list (Python 2.x) / iterable (Python 3.x) back.

map(int, a)

It could be done with list comprehension too.

[int(x) for x in a]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜