将长整数列表转换为整数 [英] Converting list of long ints to ints

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

问题描述

[112L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L] 

如何将此列表转换为这些提及值的整数值列表?我尝试了 int()但它返回了一个错误。任何想法的家伙?

How can I convert this list into a list of integer values of those mentioned values? I tried int() but it returned an error. Any idea guys?

推荐答案

您通常有很多方法可以做到这一点。您可以使用列表推导来应用内置的 int() 功能到列表中的每个个人 long 元素:

You generally have many ways to do this. You can either use a list comprehension to apply the built-in int() function to each individual long element in your list:

l = [112L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L]

l2 = [int(v) for v in l]

使用相应的 int 值返回新列表 l2

which will return the new list l2 with the corresponding int values:

[112, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126]

或者,你可以使用 map() ,这是另一个内置函数,与 int()完成同样的事情:

Or, you could use map(), which is another built-in function, in conjunction with int() to accomplish the same exact thing:

# gives similar results
l2 = map(int, l) 

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

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