Changing Data Type of Values in Dictionaries [duplicate]
This question already has an answer here:
convert the value of dictionary from list to float
3 answers
I have a program which reads a file and converts the contents into a dictionary (d). The values all come in as a string. The value of each key is a list containing two elements. Both of these elements are numbers and I wish to store them as floats. I'm having a hard time finding how I can access this specific part of each value-list in the dictionary to change it.
My code is:
d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], ... 'mango':['2.0','431.0']}
Whereas I would like:
d = {'apples':[100.0, 23.5], 'bananas':[41.5, 321.0], ... 'mango':[2.0, 431.0]}
These values are meant to be stored as floats as some division follows later
python list dictionary nested
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 16:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
convert the value of dictionary from list to float
3 answers
I have a program which reads a file and converts the contents into a dictionary (d). The values all come in as a string. The value of each key is a list containing two elements. Both of these elements are numbers and I wish to store them as floats. I'm having a hard time finding how I can access this specific part of each value-list in the dictionary to change it.
My code is:
d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], ... 'mango':['2.0','431.0']}
Whereas I would like:
d = {'apples':[100.0, 23.5], 'bananas':[41.5, 321.0], ... 'mango':[2.0, 431.0]}
These values are meant to be stored as floats as some division follows later
python list dictionary nested
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 16:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
convert the value of dictionary from list to float
3 answers
I have a program which reads a file and converts the contents into a dictionary (d). The values all come in as a string. The value of each key is a list containing two elements. Both of these elements are numbers and I wish to store them as floats. I'm having a hard time finding how I can access this specific part of each value-list in the dictionary to change it.
My code is:
d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], ... 'mango':['2.0','431.0']}
Whereas I would like:
d = {'apples':[100.0, 23.5], 'bananas':[41.5, 321.0], ... 'mango':[2.0, 431.0]}
These values are meant to be stored as floats as some division follows later
python list dictionary nested
This question already has an answer here:
convert the value of dictionary from list to float
3 answers
I have a program which reads a file and converts the contents into a dictionary (d). The values all come in as a string. The value of each key is a list containing two elements. Both of these elements are numbers and I wish to store them as floats. I'm having a hard time finding how I can access this specific part of each value-list in the dictionary to change it.
My code is:
d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], ... 'mango':['2.0','431.0']}
Whereas I would like:
d = {'apples':[100.0, 23.5], 'bananas':[41.5, 321.0], ... 'mango':[2.0, 431.0]}
These values are meant to be stored as floats as some division follows later
This question already has an answer here:
convert the value of dictionary from list to float
3 answers
python list dictionary nested
python list dictionary nested
asked Jan 1 at 16:46
MattMatt
308
308
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 16:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 16:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Use this code
d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], 'mango':['2.0','431.0']}
dict={}
for i in d:
dict[i]=
for j in d[i]:
dict[i].append(float(j))
print(dict)
It will generate new dictionary which values are float type
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use this code
d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], 'mango':['2.0','431.0']}
dict={}
for i in d:
dict[i]=
for j in d[i]:
dict[i].append(float(j))
print(dict)
It will generate new dictionary which values are float type
add a comment |
Use this code
d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], 'mango':['2.0','431.0']}
dict={}
for i in d:
dict[i]=
for j in d[i]:
dict[i].append(float(j))
print(dict)
It will generate new dictionary which values are float type
add a comment |
Use this code
d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], 'mango':['2.0','431.0']}
dict={}
for i in d:
dict[i]=
for j in d[i]:
dict[i].append(float(j))
print(dict)
It will generate new dictionary which values are float type
Use this code
d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], 'mango':['2.0','431.0']}
dict={}
for i in d:
dict[i]=
for j in d[i]:
dict[i].append(float(j))
print(dict)
It will generate new dictionary which values are float type
answered Jan 1 at 16:54


Mohammad AnsariMohammad Ansari
1131315
1131315
add a comment |
add a comment |