python tips

mydict = {'carl':40,
          'alan':2,
          'bob':1,
          'danny':3}

for key in sorted(mydict.keys()):

 有顺数得遍历字典

keylist = mydict.keys()
keylist.sort()
for key in keylist:
    print "%s: %s" % (key, mydict[key])

 

*   **  的用法

def foo(*a):
    print a[0:3]

def fii(**a):
    print a.values, a.keys()

def aa(a, b):
    print a, b

l = [2,3]
foo(*l)

d = {'a': 1, 'b': 2}
fii(**d)

aa(*d)
aa(**d)

 

你可能感兴趣的:(python)