如何将十亿或百万缩写的字符串转换为列表中的整数 [英] How to convert strings with billion or million abbreviation into integers in a list

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

问题描述

我很少有字符串项目,这些字符串项目是列表中具有十亿或百万缩写的数字:

i have few string items which are the numbers with billion or million abbreviation in a list:

list = ["150M", "360M", "2.6B", "3.7B"]

我想使用一种语法,可以将这些字符串项转换为以千为单位的整数(例如150M> 150,000,3.7B> 3,700,000),谢谢

I would like to use a syntax that could convert those string items into integers counted in thousands (e.g 150M > 150,000, 3.7B> 3,700,000 ), thanks

推荐答案

您可以将列表理解与dict映射一起使用:

You can use list comprehension with a dict mapping:

l = ["150M", "360M", "2.6B", "3.7B"]
m = {'K': 3, 'M': 6, 'B': 9, 'T': 12}
print([int(float(i[:-1]) * 10 ** m[i[-1]] / 1000) for i in l])

这将输出:

[150000, 360000, 2600000, 3700000]

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

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