简体   繁体   中英

Turning a tuple of tuples into a dictionary

So I've got tuples inside of tuples, and I would like to turn them into a key: value pair.

((1L, 'I.T.'), (2L, 'Project Management'), (3L, 'Creative'), (4L, 'Programming'), (5L, 'Sales'), (6L, 'Administration'), (7L, 'AV'), (8L, 'Human Resources'), (9L, 'Conference Rooms'), (10L, 'Testing'), (11L, 'none'))

How would I go about doing this?

Just pass it to the dict constructor/function! It can take any iterable of (key, value) tuples and create a dictionary from it.

>>> x = ((1L, 'I.T.'), (2L, 'Project Management'), (3L, 'Creative'), (4L, 'Programming'), (5L, 'Sales'), (6L, 'Administration'), (7L, 'AV'), (8L, 'Human Resources'), (9L, 'Conference Rooms'), (10L, 'Testing'), (11L, 'none')
>>> dict(x)
{1L: 'I.T.', 2L: 'Project Management', 3L: 'Creative', 4L: 'Programming', 5L: 'Sales', 6L: 'Administration', 7L: 'AV', 8L: 'Human Resources', 9L: 'Conference Rooms', 10L: 'Testing', 11L: 'none'}

通过以元组为参数调用dict

d = dict(t)

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