[英]Converting python value of dictionary from list to float
I usually program in another language but need python for something currently.我通常用另一种语言编程,但目前需要 python 。 I would like to convert the strings from this python dictionary to floats, but the tips I've read online aren't working exactly as I'd hoped.
我想将此 python 字典中的字符串转换为浮点数,但我在网上阅读的提示并没有像我希望的那样工作。
example= dict({'Baker': '16,765',
'Benton': '93,590',
'Clackamas': '419,425',
'Clatsop': '39,200',
'Columbia': '51,900',
'Coos': '63,275'})
The following results in only taking the first integer以下结果只取第一个 integer
for key in example.keys():
example[key] = [float(example[key][0]),float(example[key][1])]
example
Out[]:
{'Baker': [1.0, 6.0],
'Benton': [9.0, 3.0],
'Clackamas': [4.0, 1.0],
'Clatsop': [3.0, 9.0],
'Columbia': [5.0, 1.0],
'Coos': [6.0, 3.0]}
And the following is still a string以下仍然是一个字符串
{k:(map(float, example[k])) for k in example}
example
Out[]:
{'Baker': '16,765',
'Benton': '93,590',
'Clackamas': '419,425',
'Clatsop': '39,200',
'Columbia': '51,900',
'Coos': '63,275'}
I'm hoping you can help.我希望你能帮忙。 I'm using python3.
我正在使用python3。 Thanks.
谢谢。
You've made a few mistakes.你犯了几个错误。 Using curly brackes, keys and values already defines a
dict
.使用花括号、键和值已经定义了一个
dict
。 You don't need to call dict()
again.您无需再次调用
dict()
。 Then you either use dict
comprehension, or the map
function, but you don't need both.然后你要么使用
dict
理解,要么使用map
function,但你不需要两者。 Lastly, you have to replace the comma with a dot to make the conversion work.最后,您必须用点替换逗号才能进行转换。
example = {
'Baker': '16,765',
'Benton': '93,590',
'Clackamas': '419,425',
'Clatsop': '39,200',
'Columbia': '51,900',
'Coos': '63,275'
}
in_float = {k: float(i.replace(',', '.')) for k, i in example.items()}
print(in_float)
If you just want the int
values instead of a string, you just need this:如果你只想要
int
值而不是字符串,你只需要这个:
example = {'Baker': '16,765',
'Benton': '93,590',
'Clackamas': '419,425',
'Clatsop': '39,200',
'Columbia': '51,900',
'Coos': '63,275'}
for key in example:
s = example[key]
example[key] = [int(item) for item in s.split(',')]
print(example)
Output: Output:
{'Baker': [16, 765],
'Benton': [93, 590],
'Clackamas': [419, 425],
'Clatsop': [39, 200],
'Columbia': [51, 900],
'Coos': [63, 275]}
Alternatively: [float(item) for item in s.split(',')]
would make each value a float
.或者:
[float(item) for item in s.split(',')]
将使每个值成为float
。
You can do this with a nifty one-liner:你可以用一个漂亮的单线做到这一点:
example = {
'Baker': '16,765',
'Benton': '93,590',
'Clackamas': '419,425',
'Clatsop': '39,200',
'Columbia': '51,900',
'Coos': '63,275'
}
new_dict = {k: [float(i) for i in v.split(',')]
for k, v in example.items()}
# pretty print the output
from pprint import pprint
pprint(new_dict)
Thanks for the answers.感谢您的回答。
This helped me realize that my main problem was the commas in the numbers.这帮助我意识到我的主要问题是数字中的逗号。 I was able to remove the commas at the source with
example.replace(',','',regex=True)
我能够使用
example.replace(',','',regex=True)
删除源中的逗号
And then I was able to change the data type然后我能够更改数据类型
convert_dict = {'column_name': int}
dataframe = dataframe.astype(convert_dict)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.