Python:列表到整数 [英] Python: List to integers

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

问题描述

我已阅读文件并将每行转换为列表。该列表的示例如下:

I have read a file in and converted each line into a list. A sample of the list looks like:

['15','2','0'],['63','3','445', '456''0'],['23','4','0']

['15', '2', '0'], ['63', '3', '445', '456' '0'], ['23', '4', '0']

我想从每个列表中检索第一个数字并将其转换为和整数所以当我执行类型函数时,即

i want to retrieve the first number from each list and convert it to and integer so when i carry out the type function i.e.

type(x) 
<type 'int'> is returned

此外,当我打印x时,整数是单独打印而不是连接。即如果我从上面的列表中取得前3个数字,则数字不会打印为:

Also when i print x the integers are printed individually rather than joined. i.e. if i took the first 3 numbers from the list above the numbers are not printed as:


156323

156323


推荐答案

投下你的整数:

my_ints = [int(l[0]) for l in your_list]

打印他们出去了:

print "".join(map(str, my_ints))

这篇关于Python:列表到整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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