将数字字符串转换为数字列表 [英] Convert string of numbers to list of numbers

查看:107
本文介绍了将数字字符串转换为数字列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

l='2,3,4,5,6'

期望:

[2,3,4,5,6]

在python中如何转换成以上格式

In python how can I convert into the above format

第一个是一串由一些数字组成的字符串,我希望有一个由相同数字组成的列表.

the first one is a string of some numbers I am expecting a list of same numbers.

推荐答案

就这么简单:

在Python2中:

print [int(s) for s in l.split(',')]

,而在Python3中,只需用括号括起来即可:

and in Python3 simply wrapped with a parentheses:

print([int(s) for s in l.split(',')])

这篇关于将数字字符串转换为数字列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆