Abstract: conversion dictionary and list
The script:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 1 17:17:11 2015
@author: yuan
"""
d={'g':3, 'a':12,'b':2,'c':32,'d':120,'e':102,'f':112}
####dictionary to list
print 'get all keys of a dictionary:'
l=list(d)
print l
#another method
l=d.keys()
print l
print 'get all values of a dictionary:'
l=d.values()
print l
#another method
l=map(lambda x: d[x], d)
print l
print 'get keys and values of a dictionary:'
l=d.items()
print l
#####list to dictionary
values=range(10)
keys=map(lambda x: 'a-'+str(x), values)
d=dict(zip(keys,values))
print d
No comments:
Post a Comment