Python-赋值

>>> spam, ham = 'yum', 'YUM'

>>> spam

'yum'

>>> ham

'YUM'

>>> [spam, ham] = ['yum', 'YUM']

>>> [spam, ham]

['yum', 'YUM']

>>> a,b,c,d = 'spam'

>>> a

's'

>>> b

'p'

>>> c

'a'

>>> d

'm'

>>> a, *b = 'spam'

>>> a

's'

>>> b

['p', 'a', 'm']

>>> spam = ham = 'lunch'

>>> spam

'lunch'

>>> ham

'lunch'

============================================

序列赋值

>>> nudge = 1

>>> wink = 2

>>> A, B = nudge, wink                #Tuple assignment

>>> A, B                        # Like A = nudge; B = wink

(1,2)

>>> [C, D] = [nudge, wink]              # List assignment

>>> C, D

(1,2)

==========================================

赋值交换

>>> nudge = 1

>>> wink = 2

>>> nudge, wink = wink, nudge        # Tuple: swaps values

>>> nudge, wink                # Like T = nudge, nudge = wink; wink = T

(2, 1)

==========================================

序列赋值

>>> (a,b,c) = "ABC"            # The left number of listing names must match the characters of right

>>> a,b

('A','B')

==========================================

高级序列赋值

>>> string = "SPAM"

>>> a, b, c = string[0], stirng[1], string[2:]      # Index and slice

>>> a, b, c

('S', 'P', 'AM')

>>> a, b, c = list(string[:2]) + [string[2:]]       # Slice and concatenate

>>> a, b, c

('S', 'P', 'AM')

>>> a,b = string[:2]                  # Same, but simpler

>>> c = string[2:]

>>> a, b, c

('S', 'P', 'AM')

>>> (a, b), c = string[:2], string[2:]           # Nested sequences

>>> a, b, c

('S', 'P', 'AM')

>>> (a,b), c = ('SP', 'AM')

>>> a, b, c

('S', 'P', 'AM')

===================================================

>>> seq = [1,2,3,4,5]

>>> a, *b = seq

>>> a

1

>>> b

[2,3,4,5]

>>> *a, b = seq

>>> a

[1,2,3,4]

>>> b

5

>>> a, *b, c = seq

>>> a

1

>>> b

[2,3,4]

>>> c

5

>>> a, b, *c = seq

>>> a 

1

>>> b

2

>>> c

[3,4,5]

=======================================

>>> L = [1,2,3,4]

>>> while L:

...  front, *L = L

...  print(front, L)

...

1 [2,3,4]

2 [3,4]

3 [4]

4 []

=========================================

边界情况

>>> seq 

[1,2,3,4,5]

>>> a,b,c,d,*e = seq

>>> print(a,b,c,d,e)

1 2 3 4 [5]

========================================

>>> a,b,c,d,e,*f = seq

>>> print(a,b,c,d,e,f)

1 2 3 4 5 []

>>> a, b, *f, c, d, e = seq

>>> print(a, b, c, d, e, f)

1 2 3 4 5 []

>>> a, *b, c, *d, *e = seq

SyntaxError

>>> a, b, = seq

ValueError

>>> *a = seq

SyntaxError: starred assignment target must be in a list or tuple

>>> *a, = seq

>>> a

[1,2,3,4,5]

============================================

>>> a = b = []

>>> b.append(42)

>>> a, b

([42], [42])

============================================

>>> a = []

>>> b = []

>>> b.append(42)

>>> a, b

([], [42])

============================================

>>> L = [1,2]

>>> L = L + [3]       # Concatenate: slower

>>> L

[1,2,3]

>>> L.append(4)      # Faster , but in-place

>>> L

[1,2,3,4]

>>> L = L + [5,6]      # Concatenate: slower

>>> L

[1,2,3,4,5,6]

>>> L.extend([7,8])     # Faster, but in-place

>>> L

[1,2,3,4,5,6,7,8]

>>> L += [9, 10]      # Mapped to L.extend([9, 10])

>>> L

[1,2,3,4,5,6,7,8,9,10]

============================================

>>> L = [1, 2]

>>> M = L            # L and M reference the same object

>>> L = L + [3, 4]        # Concatenation makes a new object

>>> L, M             # Changes L but not M

([1,2,3,4], [1,2])

>>> L = [1,2]

>>> M = L

>>> L += [3,4]         # But += really means extend

>>> L, M            # M sees the in-place change too!

([1,2,3,4], [1,2,3,4])

============================================

在 Python 中创建 Unicode 字符串和创建普通的字符串一样简单

1 >>> u'Hello World !'
2 u'Hello World !'
3 >>> str(u"abc")
4 'abc'
原文地址:https://www.cnblogs.com/yy-is-ing/p/3945695.html