Open cl4ver opened 8 years ago
【遍历字典】
aDict = {'key1':'value1', 'key2':'value2', 'key3':'value3'}
print '-----------dict-------------'
for d in aDict:
print "%s:%s" %(d, aDict[d])
print '-----------item-------------'
for (k,v) in aDict.items():
print '%s:%s' %(k, v)
print '------------iteritems---------'
for k,v in aDict.iteritems():
print '%s:%s' % (k, v)
print '---------iterkeys---------------'
for k in aDict.iterkeys():
print '%s:%s' % (k, aDict[k])
print '------------iterkeys, itervalues----------'
for k,v in zip(aDict.iterkeys(), aDict.itervalues()):
print '%s:%s' % (k, v)
【计数】 count={} for x in ‘sdasdad’: if x in count: count[x]+=1 else: count[x]=1 return count