简体   繁体   中英

How can I convert an array inside a python dictionary to a tuple?

I have this dictionary:

{
    0: array([-0.16638531, -0.11749843]),
    1: array([-0.2318372 ,  0.00917023]),
    2: array([-0.42934129, -0.0675385 ]),
    3: array([-0.63377579, -0.02102854]),
    4: array([-0.26648222, -0.42038916]),
    5: array([-0.17250316, -0.73490218]),
    6: array([-0.42774336, -0.61259704]),
    7: array([-0.55420825, -0.77304496]),
    8: array([0.13900166, 0.07800885]),
    9: array([0.42223986, 0.16563338]),
    10: array([ 0.39895669, -0.09198566]),
    12: array([0.24324618, 0.44829616]),
    11: array([ 0.55394714, -0.17960723]),
    13: array([0.192127 , 0.5988793]),
    14: array([0.39554203, 0.7186038 ]),
    15: array([0.53721604, 1.        ])
}

I want to convert those numpy.ndarray values to tuples, and have something like this:

{
    0: (-0.16638531, -0.11749843),
    1: (-0.2318372 ,  0.00917023),
    ...
}

From this answer here it looks like for each value in the dictionary you can:

tuple(arr)

So for the whole dictionary you can probably do something like:

 new_dict = {key: tuple(arr) for key, arr in old_dict.items()}

Or easier to understand:

new_dict = {}
for key, arr in old_dict.items():
    new_dict.update({key: tuple(arr)})
mapping = { key: (item[0], item[1]) for key, item in your_dict.items() }

You can use a dictionary comprehension.

Python dictionaries have an .items() method that return a tuple of (key, value) for each key-value pair.

The comprehension recreates a new mapping with the original key and the array cast as a tuple .

from numpy import array

data = {
    0: array([-0.16638531, -0.11749843]),
    1: array([-0.2318372 ,  0.00917023]),
    2: array([-0.42934129, -0.0675385 ]),
    3: array([-0.63377579, -0.02102854]),
    4: array([-0.26648222, -0.42038916]),
    5: array([-0.17250316, -0.73490218]),
    6: array([-0.42774336, -0.61259704]),
    7: array([-0.55420825, -0.77304496]),
    8: array([0.13900166, 0.07800885]),
    9: array([0.42223986, 0.16563338]),
    10: array([ 0.39895669, -0.09198566]),
    12: array([0.24324618, 0.44829616]),
    11: array([ 0.55394714, -0.17960723]),
    13: array([0.192127 , 0.5988793]),
    14: array([0.39554203, 0.7186038 ]),
    15: array([0.53721604, 1.        ])
}

print({key: tuple(value) for key, value in data.items()})

OUTPUT: {0: (-0.16638531, -0.11749843), 1: (-0.2318372, 0.00917023), 2: (-0.42934129, -0.0675385), 3: (-0.63377579, -0.02102854), 4: (-0.26648222, -0.42038916), 5: (-0.17250316, -0.73490218), 6: (-0.42774336, -0.61259704), 7: (-0.55420825, -0.77304496), 8: (0.13900166, 0.07800885), 9: (0.42223986, 0.16563338), 10: (0.39895669, -0.09198566), 12: (0.24324618, 0.44829616), 11: (0.55394714, -0.17960723), 13: (0.192127, 0.5988793), 14: (0.39554203, 0.7186038), 15: (0.53721604, 1.0)}

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