简体   繁体   中英

issue transforming Python dictionary of one-itemed list to dictionary of floats

I have a dictionary with one-itemed lists as values. How can I transform them simply into a Dictionary of floats?

I've tried this way, but can't get it to work:

dict = {a:[1.25], b:[3.35], c:[3.24]}
for lst in dict.values():
    lst = lst[0]

The dictionary just stays the same, but I'd like it to be: dict = {a: 1.25, b: 3.35, c: 3.24}

You're really close!

for k in dict:
    dict[k] = dict[k][0]

Also, since dict is a built-in class name, you should avoid using it as a variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM