-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path3.py
More file actions
78 lines (57 loc) · 2.17 KB
/
3.py
File metadata and controls
78 lines (57 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Primary reference - http://nedbatchelder.com/text/iter.html
Best way to iterate over a list is to use a simple for loop. Don't use while loops or range()/xrange() if all you
want to do is do something with the members of a list.
Anytime you want to do something repetitively with something which has data in Python, try and convert it
using any of the techniques in this file to a list or something you can iterate over.
"""
import operator
#Iterate over a list
mylist = ['a', 'b', 'c', 'd']
for letter in mylist:
print letter
#Iterate over a list and get the index too. The customized index is auto generated as the first argument.
for i,v in enumerate(mylist, start=1):
print str(i)+':'+str(v)
#Iterate over a string
for c in "Hello":
print c
#Iterate over a dictionary getting only keys, only values, both or both in sorted form (sorted by key which is default, or by value using itemgetter())
dict1 = {'key1':4, 'key2':2, 'key3':3}
dict2 = {'key1':'40', 'key2':'2', 'key3':'30000'}
print "Keys only"
for key in dict1:
print key
print "Values only"
for value in dict1.itervalues():
print value
print "Keys and values"
for key, value in dict1.iteritems():
print key+':'+str(value)
print "Keys and values sorted by key"
for key, value in sorted(dict1.iteritems()):
print key+':'+str(value)
print "Keys and values sorted by value"
for key, value in sorted(dict1.items(), key=operator.itemgetter(1)):
print key+':'+str(value)
print sorted(dict2.items(), key=operator.itemgetter(1), reverse=True)
#Iterate over the lines of a file
filename = 'test'
with open(filename) as f:
for line in f:
print repr(line)
#Common operations on a list and dictionary. Only showing that its possible to do a lot of things on an iterable.
mylist = [1,2,3,4]
total = sum(mylist)
smallest = min(dict1.itervalues())
greatest = max(mylist)
joined = ''.join(dict1)
print str(total)+':'+str(smallest)+':'+str(greatest)+':'+str(joined)
#Iterate over multiple lists simultaenously.
list1 = ['a', 'b', 'c', 'd']
list2 = [1, 2, 3, 4]
for i, j in zip(list1, list2):
print i
print j
#Create a dict using 2 lists which have keys and values separately
print dict(zip(list1, list2))