Advertisement
here2share

# list_str_in_dict.py -- basic types

Jul 27th, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. # list_sort_from_dict.py -- dicts can not be inherently sorted as like lists and tuples, so...
  2.  
  3. data = {'py123':'one','py456':'two-abc','xxx':'abc','xyz.py':'123py','Python!':'Easy as ABC'}
  4.  
  5. print [x for x in list(data.keys()) if 'py' in x]
  6.  
  7. print [data[x] for x in list(data.keys()) if 'py' in x]
  8.  
  9. print [x for x in list(data.values()) if 'abc' in x]
  10.  
  11. print [x for x in data if 'abc' in data[x]]
  12.  
  13. print  [(k,v) for k,v in data.items() if k.lower().startswith('py') and v.lower().endswith('abc')]
  14.  
  15.  
  16. d = {1: 2, 3: 4, 4: 1, 2: 3, 0: 0}
  17.  
  18. print sorted(d.items(), key=lambda x: x[1])
  19.  
  20. print d.items()
  21.  
  22. # add_item_to_dict.py ...
  23.  
  24. data = {
  25.         'item a': 1,
  26.         'item b': 2,}
  27.  
  28. data['item c'] = 3
  29.  
  30. data.update({'item d': 4,'item e': 5,'item f': 6})
  31.  
  32. for i in data.items(): print i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement