Python中将一个数字字符串转化为数组

Python中将一个数字字符串转化为数组


做一道leetcode题目时遇到一个情况,将一个数字字符串转化为数组,也就是

Input : str_a = "976"
Output : array_a = [9, 7, 6]

借助map()函数,因为字符串可视为一个字符数组进行操作,所以

map_a = map(int, str_a)

也就是对str_a中每个字符进行int()函数转化。
注意一下在python3.x中map()函数返回的是一个迭代器,所以还需要进行list转化

list_a = list(map(int, str_a))
原创文章 25 获赞 34 访问量 7万+

猜你喜欢

转载自blog.csdn.net/HOMEGREAT/article/details/81227237