python - 如何在python中获取替换度数符号
问题描述
我有一个字符串列表,其中一个元素看起来像“69.3°F”我想去掉整个列表的“°F”,以便将其转换为浮点数。
我试过了:
myTemps = myTemps.replace("°F", "")
myTemps = myTemps.replace(u"°F", "")
和 myTemps = [w.replace('°F','') for w in myTemps]
myTemps = [w.replace(u'°F','') for w in myTemps]
我不断回来
'list' object has no attribute 'replace'
关于我做错了什么的任何建议?
当我打印 MyTemps 我得到
['69.9 °F'], ['70.3 °F'], ['70.0 °F'], ['69.5 °F'],
myTemps 是由列表组成的列表
解决方案
对,如果你myTemps
是一个字符串列表并且你想要一个浮点列表,
>>> temps = [['69.9 °F'], ['70.3 °F'], ['70.0 °F'], ['69.5 °F']]
>>> [float(l[0].replace('°F', '').strip()) for l in temps]
[69.9, 70.3, 70.0, 69.5]
推荐阅读
- angular - 如果分配了单击侦听器,则在组件中检测 Angular 2+
- android - 如何在自定义 Recyclerview 中实现搜索?
- reactjs - 反应中的切换状态工作正常,但我的功能检查不正确
- oracle - 从动态 SQL 访问记录类型的表
- docker - GKE集群创建时如何为节点使用自定义Ubuntu镜像?
- javascript - ExpressJS RESTapi 动态设置路由
- ruby - 如何将文件夹引用添加到 PBXGroup?
- bash - 尝试 grep 时出现错误“期望:生成 id exp4 未打开”
- excel - Excel VBA 应用程序总和
- c++ - 是什么导致我的“命名空间'Foam'中的'FatalError'没有命名类型”错误?