Reputation: 21
I have a tuple that looks like this:
List=[
('actinium-225', '10', '314'),
('actinium-226', '1.2238', '110'),
('americium-240', '2.117', '395'),
('berkelium-245', '4.94', '182'),
]
The list goes on, anyways so I've been searching how to unpack these to create a dictionary so that I can calculate the radioactive decay rates of these elements given the first value is the element, the second being the half life and third the remaining stock quantity (in grams).
I need something like this
Dict={
1: ('actinium 225', 10, 314),
2: ('actinium-226', 1.2238, 110),
3: ('americium-240', 2.117, 395),
4: ('berkelium-245', 4.94, 182),
}
Upvotes: 1
Views: 143
Reputation: 1
Simple for-loop
with enumerate
and data destruction
:
l = [
('actinium-225', '10', '314'),
('actinium-226', '1.2238', '110'),
('americium-240', '2.117', '395'),
('berkelium-245', '4.94', '182'),
]
d = {}
for i, (a, b, c) in enumerate(l, 1):
d[i] = (a, float(b), int(c))
print(d)
'''
{1: ('actinium-225', 10.0, 314),
2: ('actinium-226', 1.2238, 110),
3: ('americium-240', 2.117, 395),
4: ('berkelium-245', 4.94, 182)}
'''
Upvotes: 0
Reputation: 8556
The answer to you question is trivial, you can transform your iterable as much as you want with tools like comprehension, generated expressions, map / filter, whatever you prefer.
This one is kind of a code-golf for that:
Dict = {i: (el, float(hl), int(st)) for i, (el, hl, st) in enumerate(List, 1)}
HOWEVER,
I honestly don't see the point. If you think you need a dictionary whose keys are just ordered int
numbers, you don't need a dict
, you need a list
!
And you already have them in a list
. You can retrieve the values by index like List[2]
, much like (and even better) than retrieving them from a dict
, like Dict[2]
.
Upvotes: 1
Reputation: 18416
How do you turn a tuple with three elements into a dictionary containing a key and two values?
Your question title doesn't match your expected output, so I'll just go with the expected output rather than question title.
You can use map
and enumerate
with Dictionary Comprehension:
{idx: tuple([item[0]] + list(map(float, item[1:]))) for idx, item in enumerate(List, 1)}
OUTPUT:
{1: ('actinium-225', 10.0, 314.0),
2: ('actinium-226', 1.2238, 110.0),
3: ('americium-240', 2.117, 395.0),
4: ('berkelium-245', 4.94, 182.0)}
If no type cast required, you can just do dict(enumerate(List,1))
Upvotes: 1
Reputation: 3860
dic = {}
for index, ele in enumerate(List):
key = index+1
val_2 = float(ele[1])
val_3 = int(ele[2])
dic.update({key: (ele[0], val_2, val_3)})
print(dic)
{1: ('actinium-225', 10.0, 314), 2: ('actinium-226', 1.2238, 110), 3: ('americium-240', 2.117, 395), 4: ('berkelium-245', 4.94, 182)}
Upvotes: 1
Reputation: 298
You can just loop through the tuple and create a dictionary, like this
elements = [('actinium-225', '10', '314'), ('actinium-226', '1.2238', '110'), ('americium-240', '2.117', '395'), ('berkelium-245', '4.94', '182')]
elemDict = {}
for ind, tup in enumerate(elements):
elemDict[ind+1] = tup
Upvotes: 0