python --list

Lists are the most commonly used Python data type

It can appear as a comma-separated value within square brackets .

The data items of a list do not need to be of the same type
to create a list, just enclose the different data items separated by commas in square brackets. As follows:
list1 = ['Google', 'Runoob', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

Like indices for strings, list indices start at 0. The list can be intercepted, combined, etc.

access the values ​​in the list

Use subscript indexing to access values ​​in the list, and you can also use square brackets to truncate characters (you can use the slicing method), as follows:

list1 = ['Google', 'Runoob', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print ("list1[0]: ", list1[0]) #Print out: list1[0]: Google
print ("list2[1:5]: ", list2[1:5]) #Print out as list2[1:5]: [2, 3, 4, 5]

nested list

Using nested lists creates other lists within lists, for example:

li = [1,2,3]
lis=['a','b','c']
list = [li, lis]
print(list) # print as [[1, 2, 3], ['a', 'b', 'c']]
print(list[0]) # print as [1, 2, 3]
print(list[0][2]) #Printing as 3 means looking for the character with subscript 2 in the first list

update list

You can modify or update the data items of the list

1. Directly use the method of finding subscripts to add:

list = ['Google', 'Runoob', 1997, 2000]
print ("The third element is: ", list[2]) #Print as the third element: 1997
list[2] = 2001
print ("The third element after the update is: ", list[2]) # Print as the third element after the update: 2001

2. Use append() to enter the value you need to append in parentheses and    append it to the end by default

3. Use the insert()    brackets to enter the value you need to append   to where to add it. According to the subscript, if it exceeds the length of the list, it will be automatically added to the end

list=[33,'abc','123',80,'Google',1997,2000]
list.append('2018')
print(list) # print as [33, 'abc', '123', 80, 'Google', 1997, 2000, '2018']
list.insert(3,'haha')
print(list) # print as [33, 'abc', '123', 'haha', 80, 'Google', 1997, 2000, '2018']
list.insert(10,'slightly')
print(list) # print as [33, 'abc', '123', 'haha', 80, 'Google', 1997, 2000, '2018', 'slightly']

 4.extend (seq)          appends multiple values ​​from another sequence at one time at the end of the list (extends the original list with the new list)

Note: List 1.extend(List 2) is to append the traversal loop to list 1 after iterating list 2

li1=['a','b','c']
li2=[1,2,3,4]
li3=[li1,li2]
print(li3) # print as [['a', 'b', 'c'], [1, 2, 3, 4]]
li1.append(li2)
print(li1) # print as ['a', 'b', 'c', [1, 2, 3, 4]]
li1.extend(li2)
print(li1) # prints as: ['a', 'b', 'c', 1, 2, 3, 4]    so use extend()

remove list element

1.del method to delete available slices  

li1 = [1,2,3,4,5]
del li1[2]
print(li1) # print as [1, 2, 4, 5]
del li1[2:]
print(li1) # print as [1, 2]

2.pop() deletes an element in the list (the last element by default), and returns the value of the element. You can also delete it with a subscript

li2=['a','b','c','a','d','e']
li2.pop()   #删除默认的最后一位
print(li2)    #打印为['a', 'b', 'c', 'a', 'd']
li2.pop(1)    #使用下标删除 删除下标为1的字符
print(li2)   # 打印为['a', 'c', 'a', 'd']

3.remove()   删除列表中某个值的第一个匹配项  括号中输入所要删除的对象   如果存在多个 则删除一个

li2=['a','b','c','a','d','e']
li2.remove('a')  #删除   有同列的 只删一个
print(li2)   #打印为 ['b', 'c', 'a', 'd', 'e']

返回寻找下标

1 . index()  从列表中找出某个值第一个匹配项的索引位置     不存在抛异常

  括号中有四个值(自己,  对象 ,  开始  , 结束)

li1=[1,2,3,4,5,6,3,7,8]
print(li1.index(3))  #打印为2 
print(li1.index(3,3,7))  #打印为6   在索引为3到7之间寻找

Python列表函数

1.dir(object)   将一个数据类型的所有函数和属性输出

print(dir(list))  
#打印为  ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__',
    #  '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', 
     # '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', 
    # '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__',
    #  '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', 
    # '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 
   # 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop',
    #  'remove', 'reverse', 'sort']

2. len()   列表元素个数

li1=[1,2,3,4,5,6,3,7,8]
print(len(li1))   #打印长度  9
print(li1.__len__())   #打印长度9和上效果一样   __len__()   下划线 下划线 类型的函数代表私有 一般不调用

3.max(list)  返回列表元素最大值  以ascii表为依据

4.min(list)   返回列表元素最小值  以ascii表为依据

li1=[1,2,3,4,5,6,3,7,8]
print(max(li1))     #按ASCII表查询
print(min(li1))     #按ASCII表查询

.

5. list(seq)  将元组转换为列表

tu=(1,2,3)   #元组
new_li=list(tu)
print(tu)    #  打印为(1, 2, 3)
print(new_li)  #打印为  [1, 2, 3]

6. reverse()  函数用于反向列表中元素。 该方法没有返回值,但是会对列表的元素进行反向排序。

li1=[1,2,3,4,5,6,3,7,8]
li1.reverse()
print(li1)  #打印为[8, 7, 3, 6, 5, 4, 3, 2, 1]

7.sort([func])  对原列表进行排序    func -- 可选参数, 如果指定了该参数会使用该参数的方法进行排序。

li1=[1,2,5,6,3,4,7,8]
li1.sort()
print(li1)   #打印为[1, 2, 3, 4, 5, 6, 7, 8]   按照ascii码排序

8.clear()  清空列表

li1=[1,2,5,6,3,4,7,8]
li1.clear()
print(li1)   #打印为 [ ]

9.copy()   复制列表   则顾名思义,复制一个副本,原值和新复制的变量互不影响

li1=[1,2,5]
li2=li1.copy()
print(li1)  # 打印为[1, 2, 5]
print(li2)  # 打印为[1, 2, 5]
li2[2]=3
print(li1)  # 打印为[1, 2, 5]
print(li2)   # 打印为[1, 2, 3]

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389122&siteId=291194637