Advanced Python 2

1. List

  • Data structures: in some manner (e.g., by number) of the combination element (shown with characters as well as other data structures) set.
  • In python, the basic data structure sequence. Each element has a sequence number, i.e., the position or index, wherein the index of the first element is 0, the second element of the index 1, and so on.
  • Offset: offset relative to the beginning of the sequence

1.1 Sequence

  • Container (container): objects may contain other objects
  • The main container: the sequence (lists and tuples (indexes)), mapping (dictionary (key)) and collections.

1.2 generic sequence of operations

  • index
a = [1, 3, 5, 7]
print(a[1])  # 打印3
  • slice
a = [1, 3, 5, 7, 9, 11, 13]
print(a[1:3])  # [3,5]
print(a[-3:-1])  # [9, 11]
print(a[:3])  # [1, 3 , 5]
print(a[4:])  # [9, 11, 13]
print(a[:])  # [1, 3, 5, 7, 9, 11, 13]
print(a[1:6:2])  # [3, 7, 11],指定步长
print(a[5:1:-1])  # [11, 9, 7],注意开始索引大于结束索引
  • Sequences are added
print([1, 2, 3] + [4, 5, 6])  # [1, 2, 3, 4, 5, 6]
  • multiplication
print([1] * 5)  # [1, 1, 1, 1, 1]
print([none] * 5)  # 创建指定长度空列表
  • Membership

To check the specific value is contained within the sequence, the operator may be used in, returns a Boolean value.

name = 'zhaojun'
print('zh' in name)  # True
hobby_list = ['sing', 'dancing', 'rap', 'basketball', 'music']
print('run' in hobby_list)  # False
  • Length, maximum, minimum
nums = [1, 3, 5, 2, 4]
print(len(nums))  # 5
print(max(nums))  # 5
print(min(nums))  # 1

1.3 List of basic operations

  • Modify the list: to assign elements
nums = [1, 2, 3]
x[1] = 5
print(x)  # [1, 5, 3]
  • Removing elements
nums = [1, 2, 3]
del nums[2]
print(nums)  # [1, 2]
  • To slice assignments
nums = [1, 2, 3, 4, 5, 6, 7]
nums[:3] = [0, 0, 0]
print(nums)  # [0, 0, 0, 4, 5, 6, 7]

# 赋值是将整段插入切片位置,不考虑数量
nums = [1, 2, 3, 4, 5, 6, 7]
nums[1:3] = [0, 0, 0, 0]
print(nums)  # [1, 0, 0, 0, 0, 4, 5, 6, 7]

1.4 List method

  • append (in situ modification): an object is added to the end of the list.
nums = [1, 2, 3, 4]
nums.append(5)
print(nums)  # [1, 2, 3, 4, 5]
  • clear: clear the list contents in place.
nums = [1, 2, 3, 4]
nums.clear()
print(nums)  # [] 返回空列表

# 用切片赋值实现
nums1 = [1, 2, 3, 4]
nums1[:] = []
print[nums1]  # []
  • copy: Copy List
nums = [1, 2, 3, 4]
nums1 = nums.copy()
print(nums1)
print(id(nums))
print(id(nums1))
nums.append(5)
print(nums)
print(nums1)

"""
执行结果为:
[1, 2, 3, 4]
2193656132360
2193656116168
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
"""
  • count: Evaluates the specified element appears many times in the list.
nums = [1, 2, 2, 3, 4, 4]
print(nums.count(2))  # 2
  • entend: a plurality of value to the end of the list (List Extended)
a = [1, 2, 3]
b = [4, 5, 6]
a.entend(b)
print(a)  # [1, 2, 3, 4, 5, 6]
  • index: find the index of the first occurrence of a specified value in the list. If not found, an exception is thrown, ValueError.
a = ['a', 'b', 'c', 'd']
print(a.index('b'))  # 1
  • insert: insert an object list.
nums = [1, 2, 3, 4, 5]
nums.insert(3, '6')
print(nums)  # [1, 2, 3, '6', 4, 5]
  • pop: remove an element from the list (as at the end of last element) and returns this element.

    It can be achieved using pop a common data structure --- stack (stack), last in first out (LIFO).

    push and pop are two stacks generally accepted name of the operation (addition and removal) of. python is not provided, but can append instead.

    To create a first in first out (FIFO) queue, use the insert (0, ...) instead of append, append but can continue to use the pop-up with pop (0).

x = [1, 2, 3]
a = x.pop()
print(a)  # 3
print(x)  # [1, 2]
b = x.pop(0)
print(b)  # 1
print(x)  # [2]
  • remove: Remove the first (in-place modification does not return a value) for the element specified value.
x = ['to', 'be', 'or', 'not', 'to', 'be']
x.remove('be')
print(x)  # ['to', 'or', 'not', 'to', 'be']
  • reverse: the reverse order of elements in the list.

    To carry out iterative sequence in reverse order, using the reversed function, this function does not return the list, but returns an iterator.

x = [1, 2, 3]
x.reverse()
print(x)  # [3, 2, 1]
  • sort: sort the list in place.

    sorted functions: sort the list, sort the list after returning the original list unchanged.

a = [1, 3, 5, 2, 4]
a.sort()
print(a)  # [1, 2, 3, 4, 5]

b = [1, 3, 5, 2, 4]
c = sorted(b)
print(b)  # [1, 3, 5, 2, 4]
print(c)  # [1, 2, 3, 4, 5]

Guess you like

Origin www.cnblogs.com/zj420255586/p/11306008.html