Learning Python list type ----

A role: the value corresponding to the value stored plurality of values, and the index, according to the position of storing a plurality of values

Second, the definition

l = [1, 1.2, 'yan', [21, 99]]

Third, the type of transformation

Whenever for loop can be traversed types can be used as a parameter to the list () turn as a list

res=list('hello')
print(res)

['h', 'e', 'l', 'l', 'o']
res=list({'k1':111,'k2':222,'k3':3333})
print(res)

['k1', 'k2', 'k3']

Fourth, built-in method

1, by accessing the index value (Forward + Reverse Access Access): which can be changed to take

Forward to take:
l = [111, 'egon', 'hello']
print(l[0])

111
Reverse take:
l = [111, 'egon', 'hello']
print(l[-1])

hello
May be taken may be changed: the index value corresponding to the presence of the modified
l = [111, 'egon', 'hello']
l[0]=222 # 将列表的第一个值改为222
print(l)

[222, 'egon', 'hello']
Whether it is the value of the operation or assignment: the index does not exist error

2, a slice (care regardless of the end, step)

l = [111, 'egon', 'hello', 'a', 'b', 'c', 'd', [1, 2, 3]]
print(l[0:3])
print(l[0:5:2]) 
print(l[0:len(l)])
print(l[:])

[111, 'egon', 'hello']
[111, 'hello', 'b']
[111, 'egon', 'hello', 'a', 'b', 'c', 'd', [1, 2, 3]]
[111, 'egon', 'hello', 'a', 'b', 'c', 'd', [1, 2, 3]]

new_l=l[:] # 切片等同于拷贝行为,而且相当于浅copy

msg1='hello:egon:<>:18[]==123'
msg2=msg1[:]
print(msg1,id(msg1))
print(msg2,id(msg2))

hello:egon:<>:18[]==123 2473211243232  # id 地址相同
hello:egon:<>:18[]==123 2473211243232

3, the length (len ())

print(len([1, 2, 3])) # 判断字符长度

3    # 长度为3

4, members and not in operation in

in: to determine whether a character string in large
print('aaa' in ['aaa', 1, 2])  # True     
print(1 in ['aaa', 1, 2])  # True
not in: to determine whether the character string is not big
print(3 not in ['aaa', 1, 2]) # True
print(not 3 in ['aaa', 1, 2]) # True   # 能够使用 但不推荐 使用 not.....in....

5, add value to the list

5.1 Additional (.append ())

From back to front plus

l = [111, 'egon', 'hello']
l.append(3333)  # 将 3333 添加到列表l中 从后往前加
l.append(4444)  # 将 4444 添加到列表l中 从后往前加
print(l)

[111, 'egon', 'hello', 3333, 4444]
new_l=[1,2,3]
l=[111,'egon','hello']
l.append(new_l) # 将 new_l的值添加到 l 中
print(l)

[111, 'egon', 'hello', [1, 2, 3]]
5.2 interpolated values ​​(.insert ())

The position of the insert

l = [111, 'egon', 'hello']
l.insert(0, 'alex') # 将 alex 插入列表 0 号位置
print(l)

['alex', 111, 'egon', 'hello']
5.3 extend added value
l = [111, 'egon', 'hello']
new_l=[1,2,3]
l.extend(new_l)  将 列表 new_l 添加到列表l中 从后往前加
print(l)

[111, 'egon', 'hello', 1, 2, 3]

6, delete

Method 1: Delete common method, but simply deleted, no return value

l = [111, 'egon', 'hello']
del l[1]
            # x = del l[1] # 抛出异常,不支持赋值语法
print(l)

[111, 'hello']

Second way: .pop () to delete the index, the return value will be deleted

l = [111, 'egon', 'hello']
l.pop() # 不指定索引默认删除最后一个
l.pop(0) # 删除 第一个
print(l)

[111, 'egon']
['egon', 'hello']

Three ways: .remove () according to delete elements, return None

l = [111, 'egon', [1, 2, 3], 'hello']
l.remove([1, 2, 3])
print(l)

[111, 'egon', 'hello']
l = [111, 'egon', [1, 2, 3], 'hello']
res = l.remove('egon')
print(res)  # None
print(l)

None
[111, [1, 2, 3], 'hello']
7, circulation
l = [1, 'aaa', 'bbb']
for x in l:
    l.pop(1)
    print(x)

1
bbb

Fifth, the need to have an operation

  1. The number of times an element statistics appear in the list .count ()
    l = [1, 'aaa', 'bbb','aaa','aaa']
    print(l.count('aaa'))
    
    3    # aaa  在列表 l 中出现3次
  2. .index () to print an index of an element (if the error will not be found)
    l = [1, 'aaa', 'bbb','aaa','aaa']
    print(l.index('aaa'))
    print(l.index('aaaaaaaaa')) # 找不到报错
    
    1
    ValueError: 'aaaaaaaaa' is not in list  # 报错信息 
  3. .clear () Clear List
    l = [1, 'aaa', 'bbb','aaa','aaa']
    l.clear()
    print(l)
    
    []   # 列表 l 被clear 清空
  4. .reverse () will list upside down (not sorted)
    l = [1, 'egon','alex','lxx']
    l.reverse()
    print(l)
    
    ['lxx', 'alex', 'egon', 1]   # reverse 将列表 l 倒过来
  5. .sort () in the list of elements must be the same type can sort
    l = [11, -3, 9, 2, 3.1]
    l.sort()  # 默认从小到大排,称之为升序
    l.sort(reverse=True)  # 从大到小排,设置为降序
    print(l)
    
    [-3, 2, 3.1, 9, 11]
    [11, 9, 3.1, 2, -3] #.sort(reverse=True)   

Learn: the size of the string can be compared, according to the character position corresponding sequence pk
is distinguished ASCI code table in the order of size of the string, in front of the rear row of the table is greater than the character

print('a'>'b')   # False  b>a   z>a 
print('abz'>'abcdefg') # 比较首个字符 如果相同比较一下个字符  大小与字符串长度无关

False
True

Learn: The list can also be the same size ratio, with the principle of string, but the elements corresponding to the position must be the same type

l1 = [1, 'abc', 'zaa'] # l1 < l2  比较第一个值  abc 大小相同比较第二个值 
l2 = [1, 'abc', 'zb'] # l1 'zaa' l2 'zb' 首个字符相同  比较第二个字符 a < b 所以 l2 >l1
print(l1 < l2)

True        

Sixth, supplement

1, the queue: FIFO, FIFO

Enqueue operation

l = []
l.append('first')
l.append('second')
l.append('third')
print(l)

['first', 'second', 'third']

Dequeue

l = ['first', 'second', 'third']
print(l.pop(0))
print(l.pop(0))
print(l.pop(0))

first
second
third
2, the stack: LIFO, last in first out

Stack operation

l = []
l.append('first')
l.append('second')
l.append('third')
print(l)

['first', 'second', 'third']

Dequeue

l = ['first', 'second', 'third']
print(l.pop())
print(l.pop())
print(l.pop())

third
second
first

Guess you like

Origin www.cnblogs.com/x945669/p/12464864.html