Skip to content

Commit dc533af

Browse files
committed
update
1 parent f947f01 commit dc533af

1 file changed

Lines changed: 144 additions & 2 deletions

File tree

Work/02_chap.py

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,149 @@ def prices_ex():
322322
# using defaultdict
323323

324324

325-
'''## ##'''
325+
'''##
326326
## 2.6 List Comprehensions ##
327-
'''## ##'''
327+
[ <expression> for <variable_name> in <sequence> if <condition>]
328+
#A common task is processing items in a list. This section introduces list comprehensions, a powerful tool for doing just that.
329+
'''##
330+
331+
# A list comprehension creates a new list by applying an operation to each element of a sequence
332+
#Creating new lists
333+
a = [1,2,3,4,5 , -10,22,12,6]
334+
b = [2*x for x in a]
335+
b
336+
337+
nameX = [nm.upper() for nm in names]
338+
nameX
339+
340+
#Filtering
341+
xt = [print(x) for x in a if x>0]
342+
xt
343+
344+
## first run report.py program in bash/powershell mode and then try
345+
346+
347+
## set comprehennsion
348+
## using { } bracket for this
349+
xt = { print(s) for s in zholding}
350+
xt = { print(s) for s in holding}
351+
xt
352+
353+
354+
'''##
355+
## 2.6 Objects ##
356+
357+
'''##
358+
# Assignment
359+
# assignment operations never make a copy of the value being assigned.
360+
# All assignments are merely ****reference copies**** (shallow copy or copy by reference or * assignment) (or pointer copies if you prefer).
361+
# to check the two assigned values; use "is" or "==" to compare eg: a is b or a == b
362+
## shallow copy
363+
a = [2,3,4]
364+
b =a
365+
b
366+
a.append(5)
367+
a
368+
b
369+
b.append(0)
370+
a
371+
b
372+
c = b
373+
b[1] == a[1]
374+
# Reassigning values
375+
# Reassigning a value never overwrites the memory used by the previous value.
376+
a = [9,9,9,9]
377+
a
378+
b
379+
a is b # compares not the value by the pointer/reference address
380+
381+
id(a) == id(b)
382+
id(a)
383+
id(b)
384+
id(c)
385+
386+
## deep copy
387+
# use copy module
388+
import copy
389+
a = [2,4,[55,66]]
390+
b = a
391+
a
392+
b
393+
a == b
394+
a[2].append(99)
395+
a
396+
b
397+
398+
b = copy.deepcopy(a)
399+
a
400+
b
401+
a == b
402+
a[2].append(100)
403+
a
404+
b
405+
a[2] == b[2]
406+
407+
## Type Checking for an object -- isinstance()
408+
a = [2,3,4]
409+
type(a)
410+
isinstance(a, list)
411+
'''
412+
Everything is an object
413+
Numbers, strings, lists, functions, exceptions, classes, instances, etc. are all objects.
414+
It means that all objects that can be named can be passed around as data, placed in containers, etc.,
415+
without any restrictions. There are no special kinds of objects. Sometimes it is said that all objects are “first-class”.
416+
'''
417+
import math
418+
import os
419+
items = [abs, math, copy, os, ValueError, 1, "strd", {"age":23}, (32,4)]
420+
items
328421

422+
'''
423+
Exercise 2.24: First-class Data
424+
'''
425+
import os
426+
import csv
427+
workdir = os.getcwd()
428+
workdir += r'\work'
429+
types =[str,int,float]
430+
with open(workdir+r'\data\portfolio.csv') as f:
431+
rows = csv.reader(f)
432+
hdr = next(rows)
433+
shareValue =0
434+
for row in rows:
435+
r = zip(types,row)
436+
print(list(r))
437+
shareValue += types[1](row[1])*types[2](row[2])
438+
print(f'total share value = {shareValue}')
439+
440+
##2 -- more useful to pase data; use types and then use zip. new thing is using "fun" function type
441+
types =[str,int,float]
442+
with open(workdir+r'\data\portfolio.csv','rt') as f:
443+
rows = csv.reader(f)
444+
hdr = next(rows)
445+
shareValue =0
446+
shareValue2 =0
447+
allData = []
448+
dataDict = []
449+
for row in rows:
450+
converted = []
451+
for fun, val in zip(types,row): #In the loop, the func variable is one of the type conversion functions
452+
converted.append(fun(val)) # this can also be done wtih - { name: func(val) for name, func, val in zip(headers, types, row) }
453+
shareValue2 += converted[1]*converted[2]
454+
d= dict(zip(hdr,converted))
455+
dataDict.append(d)
456+
allData.append(converted)
457+
shareValue += types[1](row[1])*types[2](row[2])
458+
print(f'total share value = {shareValue} {shareValue2}')
459+
460+
for x in allData:
461+
print(x)
462+
463+
##2 - using list comprehension to do similar
464+
f = open(workdir+r'\data\dowstocks.csv','rt')
465+
rows =csv.reader(f)
466+
hdr = next(rows)
467+
types = [str, float, str, str, float, float, float, float, int]
468+
for row in rows:
469+
convertedX= [fun(val) for fun,val in zip(types,row)]
470+
f.close()

0 commit comments

Comments
 (0)