last day (15 days later) » 

3:40 PM
4
Q: Dicts in Python

imane dergalI have a multidimensionnal dict, I need to return a specific value. ConsomRatio={"DAP_Local":[],"MAP11_52":[]} ConsomRatio["DAP_Local"].append({"Ammonia":"0.229", "Amine":"0.0007"}) ConsomRatio["MAP11_52"].append({"Ammonia":"0.138", "Fuel":"0.003"}) print(ConsomRatio["DAP_Local"]) The result ...

 
Can you explain more exactly what you want? Why are you putting lists inside the dict anyhow?
 
Actually, I wanted to use multi-dimensional dicts, but got lost into the syntax, so went back and used lists inside the dict. Multi-dimensional dicts are more efficient in my case.
 
dicts are really rather efficient -- at least in your case. With simpler needs as yours, lists can be used and than they are more efficient.
 
I only used some of the data to make sure my problem is clear. I actually have a big amount of data, that is why I thought dicts would be way more efficient.
 
Don't worry: in spite of my comments, dicts are really efficient in Python and you can handle big amounts of data with them, at least when you don't try to handle whole databases (multiple GBs of data with them). I guess, it is for a students project?
 
3:40 PM
@Juergen It is for my final year internship
 
Hi Imane, I did not use chat before in stackoverflow. Are you online?
 
4:07 PM
Hello, it is my first time too
 
i changed my latest example a little to make the code more solid
 
Yes I just saw that, I'm cheking it out
 
do you know the get method?
it can be very helpful!
 
Nop. I don't. I am very new to Python.
I'll check it out rightnow
 
ok, I thought so. just look it up in docu. i have to be off for a while now!
 
4:11 PM
Okay! Thank you so much for your help. Very useful!
 
but you can ask, when you have further questions.
Bye!
 
Will do, thanks! Bye.
 
4:42 PM
An other example, what you can do with your dicts:

print('To produce {to_produce} we need {amount} of {ingredience}'.format(
to_produce=to_produce, ingredience=ingredience,
amount=ConsomRatio[to_produce].get(ingredience, '0.0')))
Sorry, was to fast! Again ...
for product, ingred_list in ConsomRatio:
for iname, ivalue in ingred:
print('To produce {to_produce} we need {amount} of {ingredience}'.format(to_produce=product, ingredience=iname, amount=ivalue))
OK, That did not work out correctly. I added the example to my answer! (here, we don't have good formatting either)
 

  last day (15 days later) »