unpack1.py 1.37 KB
Newer Older
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
# unpack sequences

a, = 1,     ; print(a)
a, b = 2, 3 ; print(a, b)

a, b = range(2); print(a, b)

# with star

*a, = ()    ; print(a)
*a, = 4,    ; print(a)
*a, = 5, 6  ; print(a)

*a, b = 7,          ; print(a, b)
*a, b = 8, 9        ; print(a, b)
*a, b = 10, 11, 12  ; print(a, b)

a, *b = 13,         ; print(a, b)
a, *b = 14, 15      ; print(a, b)
a, *b = 16, 17, 18  ; print(a, b)

a, *b, c = 19, 20           ; print(a, b)
a, *b, c = 21, 22, 23       ; print(a, b)
a, *b, c = 24, 25, 26, 27   ; print(a, b)

a = [28, 29]
*b, = a
print(a, b, a == b)

try:
    a, *b, c = (30,)
except ValueError:
    print("ValueError")

# with star and generic iterator

*a, = range(5)          ; print(a)
*a, b = range(5)        ; print(a, b)
*a, b, c = range(5)     ; print(a, b, c)
a, *b = range(5)        ; print(a, b)
a, *b, c = range(5)     ; print(a, b, c)
a, *b, c, d = range(5)  ; print(a, b, c, d)
a, b, *c = range(5)     ; print(a, b, c)
a, b, *c, d = range(5)  ; print(a, b, c, d)
a, b, *c, d, e = range(5)   ; print(a, b, c, d, e)

*a, = [x * 2 for x in [1, 2, 3, 4]]     ; print(a)
*a, b = [x * 2 for x in [1, 2, 3, 4]]   ; print(a, b)
a, *b = [x * 2 for x in [1, 2, 3, 4]]   ; print(a, b)
a, *b, c = [x * 2 for x in [1, 2, 3, 4]]; print(a, b, c)

try:
    a, *b, c = range(0)
except ValueError:
    print("ValueError")

try:
    a, *b, c = range(1)
except ValueError:
    print("ValueError")