List Ganso range

1. List list

  • Some data storage containers such as bags wardrobe
  • Action: some data storage, a greater amount of data
  • Subscripts may be sliced ​​can be exactly the same step and the string
lst = [1,2,3]
print(lst)          #[1,2,3]
lst = ["qwe","wwe",4,5,6]
print(lst)          #["qwe","wwe",4,5,6]
lst = ["qwe","wwe",4,5,6]
print(lst)          #["qwe","wwe",4,5,6]
print(lst[0])       #hello
print(lst[0][1])    #i
lst[0] = "我怎么这么可爱"
print(lst)          #["我怎么这么可爱","wwe",4,5,6]
  • Strings are immutable
s = "你好"
s[0] = "我" 
print(s)     #报错,字符串是不可变类型  值在变
s = "年后"
s = "我好"    #指向在变

1.1 List of increase

1.1.1 lst.append()

  • Appended at the end of a plus position
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
lst.append("牛爱花")
print(lst)#['杨紫', '高圆圆', '刘亦菲', '关晓彤', '牛爱花']

1.1.2 lst.insert()

  • Insert the first insertion position to the second index is to insert the contents
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
lst.insert(1,"包夜")
print(lst)#['杨紫', '包夜', '高圆圆', '刘亦菲', '关晓彤']

1.1.3 lst.extend () extension - add iteration

lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
lst.extend("今天是新的一年")
print(lst)#['杨紫', '高圆圆', '刘亦菲', '关晓彤', '今', '天', '是', '新', '的', '一', '年']
  • Plastic Boolean value can not be iterative

1.1.4 list merge

lst1 = [1,2,3]
lst2 = [4,5,6]
print(lst1+lst2) #[1, 2, 3, 4, 5, 6]
  • Opened up a new space

1.2 List of deleted

1.2.1

  • Delete List
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
del lst
print(lst) #报错 没有lst列表
  • With the subscript, slice, step deleted
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
del lst[-1]
print(lst)  #['杨紫', '高圆圆', '刘亦菲']
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
del lst[0:2]
print(lst)  #['刘亦菲', '关晓彤']
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
del lst[0:2:2]
print(lst)  #['高圆圆', '刘亦菲', '关晓彤']

1.2.2 lst.remove()

  • By deleting content
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
lst.remove("高圆圆")
print(lst)#['杨紫', '刘亦菲', '关晓彤']

1.2.3 lst.pop()

  • No default delete the last
  • You can delete the specified index
  • Returns the underlying value bit contents
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
lst.pop()
print(lst)#['杨紫', '高圆圆', '刘亦菲']
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
ret=lst.pop(0)
print(lst)#['高圆圆', '刘亦菲', '关晓彤']
print(ret) #杨紫

1.2.4 lst.clear () Clear

lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
lst.clear()
print(lst)#[]

1.3 change list

1.3.1 Change through long index, slice, step

lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]#****
lst[0] = "杨幂"
print(lst)#['杨幂', '高圆圆', '刘亦菲', '关晓彤']
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
lst[0:3] = "你好美"
print(lst)#['你', '好', '美', '关晓彤']
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
lst[0:3] = "你好"
print(lst) #['你', '好','关晓彤']
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
lst[0:3] = "委书记董位按实际都那时"
print(lst)#['委', '书', '记', '董', '位', '按', '实', '际', '都', '那', '时', '关晓彤']
  • To unify length
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
lst[0:4:2] = "你好"
print(lst)#['你', '高圆圆', '好', '关晓彤']

1.4 check list

1.4.1 for loop

lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]#杨紫
for i in lst:                       #高圆圆
    print(i)                        #刘亦菲
                                    #关晓彤

1.4.2 investigation by the subscript

lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
print(lst[0])#杨紫

1.5 List of operation

1.5.1 lst.index()

  • Find index by content
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
print(lst.index("高圆圆"))#1

1.5.2 lst.count()

  • count
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
print(lst.count("高圆圆")) #1

1.5.3 lst.copy()

  • copy
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
ret = lst.copy()
print(lst)#['杨紫', '高圆圆', '刘亦菲', '关晓彤']
print(ret)#['杨紫', '高圆圆', '刘亦菲', '关晓彤']

1.5.4 lst.reverse()

  • Overturn
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
ret = lst.reverse()
print(lst)#['关晓彤', '刘亦菲', '高圆圆', '杨紫']
print(ret)#None
         #这是一操作,没有返回的内容   变化的内容在元数据查看

1.5.5 lst.sort()

  • By comparing the code default sort ascending asicii
lst = ["杨紫","高圆圆","刘亦菲","关晓彤"]
lst.sort()
print(lst)['关晓彤', '刘亦菲', '杨紫', '高圆圆']
lst = [1,5,9,63,54,81,22]
lst.sort(reverse=True)#降序
print(lst)#[81, 63, 54, 22, 9, 5, 1]

1.5 nested list

lst = ["杨紫","高圆圆","刘亦菲","关晓彤",["迪丽热巴","古力娜扎",["杨幂","蔡依林"]]]
print(lst[4][1]) #古力娜扎
print(lst[4][3][0])#杨幂

2. Ganso tuple

  • Tuples are immutable data can not be modified to store some of the user's password when
  • Ganso is actually a list can not be amended
tu = (1,2,3,4,"你好啊",True)
print(tu)#(1, 2, 3, 4, '你好啊', True)
print(tu[2])#3
print(tu[2:5])#(3, 4, '你好啊') 切片为元祖
print(tu[2:5:2])#(3, '你好啊')

3.range

  • Python3 range in range (0,10) is a iterable

  • python2 in xrange and python in the range is the same as all iterables

    python2 the range is to obtain a list of

range(0,10) # 其实位置 10 终止位置
print(range(0,10))# range(0,10)
print(list(range(0,10,3)))#[1,4,7]
  • The first position is a position of the second fact is the end position after the third step does not take the first position and the termination sections are taken as they are separated by commas

Guess you like

Origin www.cnblogs.com/zhaohaiyu/p/11134895.html