-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ch10Notes.py
187 lines (120 loc) · 3.04 KB
/
Ch10Notes.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
####TUPLELS ARE IMMPUTABLE####
# t ='a', 'b', 'c'
# print(t)
# print(type(t))
## the () are recomended but not needed
## similar to lists and dictionaries, you can make an empyty one
# t = tuple()
# print(t)
# t = tuple('kitty cat')
# print(t)
## please don't make tuple a variable name
## indexing also works
# t = tuple('kitty cat')
# print(t[:5])
#
#
#
# ##but here is the kicker, you cannot modifiy
# t = tuple('kitty cat')
# t[1]='g'
# print(t)
# ## this gets a traceback
####COMPARING TUPELS####
# x = 0,1,2000000
# y = 0,3,4
# print(x<y)
## to compare size, it goes by the first value where there is a
## regardless of the others
## other function/methods are similar such as sort()
## sorting a word##
# txt = 'but soft what light in yonder window breaks'
# words = txt.split()
# t = list()
# for word in words:
# t.append((len(word), word))
# ## print(t)
# ## you are creating a tuple within each list where
# ## the fist value in the length and the second is
# ## the word
# t.sort(reverse=True)
## print(t)
# ## this says to go in revese order (from large to small)
# res = list()
# for length, word in t:
# res.append(word)
# print(res)
#### TUPLE ASSIGNMENT ####
# m = ['have', 'fun']
# x,y = m
# print(x)
# print(y)
# a,b = b,a
# addr ='monty@python.org'
# uname, domain = addr.split('@')
# print(uname)
# print(domain)
#
####DICTIONARIES AND TUPELS#####
# d={'a':10,'b':1, 'c':45}
# t =list(d.items())
# print(t)
## makes a list of tuples from the dict()
## not sure why you need the list()
# d={'a':10,'b':1, 'c':45}
# t =list(d.items())
# t.sort()
# print(t)
## sort by the first variable (in this case a letter)
# d={'a':10,'b':1, 'c':45}
# for key, val in list(d.items()):
# print(val, key)
##swap the keys and values
#
# d={'a':10,'b':1, 'c':45}
# l= list()
# for key,val in d.items():
# l.append((val,key))
# ##note the double brackets
# print(l)
# l.sort(reverse=True)
# print(l)
### sorting a tuble by value instead of key
## import romeo and Juliet text and sort by largest value
# import string
fhand = open('romeo.txt')
counts = dict()
for line in fhand:
line = line.lower()
words = line.split()
for word in words:
counts[word] = counts.get(word, 0)+1
# Sort the dictionary by value
# lst = list()
# for key, val in list(counts.items()):
# lst.append((val, key))
#
# lst.sort(reverse=True)
# for val, key in lst[:10]:
# print(val, key)
## the second chunk of that can be replaced
# print(sorted([(v,k) for k,v in counts.items()]))
#### TUBLES CAN BE THE KEY IN A DICT() ####
# first ='Colin'
# last = 'OBrien'
# number = 'number'
# d =dict()
# d[last, first]= '2034...'
# for last, first in d:
# print(first,last, d[last,first])
#### Some review on what sequence does what ####
###list()###[]
##is mutable
###Tupel###()
##is immutable, like strings
## so things like sort, do not work (is changing it)
##reverse=True (high to low)
##reverse=False (low to high)
###Video###
###https://www.coursera.org/learn/python-data/lecture/0ou0N/10-tuples
#