Python 3.6.6 |Anaconda custom (64-bit)| (default, Jun 28 2018, 17:14:51)
Type "copyright", "credits" or "license" for more information.
IPython 6.5.0 -- An enhanced Interactive Python.
In [1]: dir
Out[1]: <function dir>
In [2]: ls
accettazione-gianluca.pdf Libri/
agenda.py Lightworks/
Arduino/ lmms/
artists/ Maps/
bin/ mips11.asm
birdseye/ mips1.asm
bollettino-caldaia-2018.pdf missfont.log
'Calibre Library'/ Music/
cloud-config.yml My_Cmaps/
CmapToolsLogs/ my-systemd.pp
codifica.py my-systemd.te
conf.py n
consegna-gianluca.pdf nltk_data/
CUDATest.nb npm-debug.log
Desktop/ OPT-IEETEL-symmetric.ods
docker-snippets Pictures/
Documents/ pixiedust/
Downloads/ "PlayOnLinux's virtual drives"@
Drive/ pro.py
Dropbox/ prova.py
DUPES/ Public/
dwhelper/ qrcodegen/
emu/ Recordings/
euromoot-2019-acconto.pdf rosegarden/
f1.gephi rpmbuild/
fcdtdebugger/ snap/
filippo.gephi src/
f.py ssl/
GCloud/ STEM/
git/ Templates/
GPUCache/ tmp/
gurobi.log TODO/
HACKS UNITELMA/
index.html Videos/
index.rst viva.py
iso/ wekafiles/
LDCad/ workspace/
lecture08.mp4 workspace-ARM/
lettera-gianluca.pdf workspace-fcdtdebug/
In [3]: ls
ipython-lez5.html lezione1.py lezione4.py lezione5.py __pycache__/
In [4]: pwd
Out[4]: '/home/andrea/Documents/Uni/Didattica/Prog1/2018-19/Lezioni'
In [5]: ls
ipython-lez5.html lezione1.py lezione4.py lezione5.py __pycache__/
In [6]: cd ..
/home/andrea/Documents/Uni/Didattica/Prog1/2018-19
In [7]: cd Lezioni
/home/andrea/Documents/Uni/Didattica/Prog1/2018-19/Lezioni
In [8]: t= "iwuyfiaulgsaihul"
In [9]: sorted(t)
Out[9]:
['a',
'a',
'f',
'g',
'h',
'i',
'i',
'i',
'l',
'l',
's',
'u',
'u',
'u',
'w',
'y']
In [10]: %pprint
Pretty printing has been turned OFF
In [11]: sorted(t)
Out[11]: ['a', 'a', 'f', 'g', 'h', 'i', 'i', 'i', 'l', 'l', 's', 'u', 'u', 'u', 'w', 'y']
In [12]: set(t)
Out[12]: {'h', 's', 'l', 'u', 'f', 'y', 'a', 'g', 'w', 'i'}
In [13]: sorted(set(t))
Out[13]: ['a', 'f', 'g', 'h', 'i', 'l', 's', 'u', 'w', 'y']
In [14]: def trasforma(coppia):
...: w, h = coppia
...: return w*h, -h, w
...:
...:
In [15]: rettangoli = [ (30, 20), (5, 100), (7, 40)]
In [16]: sorted(rettangoli)
Out[16]: [(5, 100), (7, 40), (30, 20)]
In [17]: sorted(rettangoli, key=trasforma)
Out[17]: [(7, 40), (5, 100), (30, 20)]
In [18]: def trasforma(coppia):
...: w, h = coppia
...: print(coppia)
...: return w*h, -h, w
In [19]: sorted(rettangoli, key=trasforma)
(30, 20)
(5, 100)
(7, 40)
Out[19]: [(7, 40), (5, 100), (30, 20)]
In [20]: def trasforma(coppia):
...: w, h = coppia
...: terna = w*h, -h, w
...: print(coppia, terna)
...: return terna
In [21]: sorted(rettangoli, key=trasforma)
(30, 20) (600, -20, 30)
(5, 100) (500, -100, 5)
(7, 40) (280, -40, 7)
Out[21]: [(7, 40), (5, 100), (30, 20)]
In [22]: def pippo(lista):
...: risultato = []
...: for v in lista:
...: risultato.append(v*v)
...: return risultato
...:
...:
In [23]: def quadrati(lista):
...: risultato = []
...: for v in lista:
...: risultato.append(v*v)
...: return risultato
In [24]: interi = [ 6, 9, 33, 2]
In [25]: quadrati(interi)
Out[25]: [36, 81, 1089, 4]
In [26]: q = [ x*x for x in interi ]
In [27]: q
Out[27]: [36, 81, 1089, 4]
In [28]: def q2(lista):
...: return [x*x for x in lista]
In [29]: q2(interi)
Out[29]: [36, 81, 1089, 4]
In [30]: def quadrati_dispari(lista):
...: risultato = []
...: for v in lista:
...: if v % 2:
...: risultato.append(v*v)
...: return risultato
In [31]: quadrati_dispari(interi)
Out[31]: [81, 1089]
In [32]: def q2(lista):
...: return [x*x for x in lista if x % 2]
In [33]: q2(interi)
Out[33]: [81, 1089]
In [34]: v = 6
In [35]: x = v*v if v % 2 else v*v*v
In [36]: x
Out[36]: 216
In [37]: def q2(lista):
...: return [x*x if x % 2 else x*x*x for x in lista ]
In [38]: q2(interi)
Out[38]: [216, 81, 1089, 8]
In [39]: def quadrati_dispari_pari(lista):
...: risultato = []
...: for v in lista:
...: if v % 2:
...: risultato.append(v*v)
...: else:
...: risultato.append(v*v*v)
...: return risultato
In [40]: def q2(lista):
...: return [x*x if x % 2 else x*x*x for x in lista ]
In [40]:
In [40]:
In [41]: { x*x for x in interi }
Out[41]: {1089, 81, 36, 4}
In [42]: { x : x*x for x in interi }
Out[42]: {6: 36, 9: 81, 33: 1089, 2: 4}
In [43]: tuple( x*x for x in interi )
Out[43]: (36, 81, 1089, 4)
In [44]: ( x*x for x in interi )
Out[44]: <generator object <genexpr> at 0x7fc12c0cb830>
In [45]: tuple( x*x for x in interi )
Out[45]: (36, 81, 1089, 4)
In [46]: [ [ x for x in range(y) ] for y in interi]
Out[46]: [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6, 7, 8], [0, 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], [0, 1]]
In [47]: %pprint
Pretty printing has been turned ON
In [48]: [ [ x for x in range(y) ] for y in interi]
Out[48]:
[[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5, 6, 7, 8],
[0,
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],
[0, 1]]
In [49]: %pprint
Pretty printing has been turned OFF
In [50]: [ [ x for x in range(y) ] for y in interi]
Out[50]: [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6, 7, 8], [0, 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], [0, 1]]
In [51]: [ [ x for x in range(y) ] for y in interi if y % 2]
Out[51]: [[0, 1, 2, 3, 4, 5, 6, 7, 8], [0, 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]]
In [52]: [ [ x for x in range(y) if x % 3 ] for y in interi if y % 2]
Out[52]: [[1, 2, 4, 5, 7, 8], [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32]]
In [53]: def lista4(lista):[ [ x for x in range(y) if x % 3 ] for y in interi if y % 2]
In [54]: def lista4(lista):
...: return [ [ x for x in range(y) # tutti gli elementi da 0 a y
...: if x % 3 # non divisibili per 3
...: ] for y in lista # per tutti i valori y in lista
...: if y % 2 # ma solo se sono dispari
...: ]
...:
In [55]: tuple(interi)
Out[55]: (6, 9, 33, 2)
In [56]: tuple(interi).sort()
Traceback (most recent call last):
File "<ipython-input-56-3c428ee39575>", line 1, in <module>
tuple(interi).sort()
AttributeError: 'tuple' object has no attribute 'sort'
In [57]:
In [57]: sorted(tuple(interi))
Out[57]: [2, 6, 9, 33]
In [58]: sorted(set(interi))
Out[58]: [2, 6, 9, 33]
In [59]: set(interi).sort()
Traceback (most recent call last):
File "<ipython-input-59-93006440540d>", line 1, in <module>
set(interi).sort()
AttributeError: 'set' object has no attribute 'sort'
In [60]:
In [60]: d = { x: x*x for x in interi }
In [61]: d.sort()
Traceback (most recent call last):
File "<ipython-input-61-a6324fc0ae3a>", line 1, in <module>
d.sort()
AttributeError: 'dict' object has no attribute 'sort'
In [62]:
In [61]:
In [62]: