python list, add list, delete the list, a list of commonly used methods, etc.

Python List

A. List

1. list definitions:

Python sequence is the most basic data structure. Each element in the sequence is assigned a number - its position, or index, the first index is 0, the second index is 1, and so on.

Python has a built-in type 6 sequences, but the most common are lists and tuples.

The sequence of operations can be carried out, including indexing, slicing, add, multiply, check members.

Further, Python already built determined sequence length and determining the maximum and minimum element method.

Python is a list of the most common types of data, it can appear as a comma-separated values ​​in square brackets.

Data items list need not have the same type

Example:

print("示例1:")
member = ['zhangsan','lisi','wangwu']
print(member)
print(member[0])
"""
	运行结果:
    示例1:
    ['zhangsan', 'lisi', 'wangwu']
    zhangsan
"""

print("示例2:")
_list1 = [1111,2222,'学生']
print(_list1)
for each in _list1:
    print(each)
"""
    示例2:
    [1111, 2222, '学生']
    1111
    2222
    学生
"""

print("示例3:")
_list2 = [111,222,'333',444,555,666,]
print('_list2[1:3]:',_list2[1:5])#注意这里
"""
    示例3:
    _list2[1:3]: [222, '333', 444, 555]
"""

II. Elements are added to the list

1.append () method:

Description:

append () method is used at the end of the list to add new objects

Syntax:

list.append(obj)

Example:

list4 = ['张三','李四','王五']
print(list4,"长度为:",len(list4))

list4.append("小明")
print(list4,"更新后的长度为:",len(list4))
"""
	运行结果:
    ['张三', '李四', '王五'] 长度为: 3
    ['张三', '李四', '王五', '小明'] 更新后的长度为: 4
"""

2.extent () method:

Description:

extend () function for adding a plurality of disposable end of the list in the other sequence values ​​(original extended list with a new list).

Syntax:

list.extend(seq)
#seq -- 元素列表,可以是列表、元组、集合、字典,若为字典,则仅会将键(key)作为元素依次添加至原列表的末尾。

Example:

list5 = ['张三','李四','王五']
print(list5,"长度为:",len(list5))

list5.extend(['小明','李华']) 
print(list5,"更新后的长度为:",len(list5))

'''
    运行结果:
    ['张三', '李四', '王五'] 长度为: 3
    ['张三', '李四', '王五', '小明', '李华'] 更新后的长度为: 5
'''

3.insert () method:

Description:

insert () function is used to specify the object into the specified position in the list.

Syntax:

list.insert(index, obj)
'''
	index -- 对象obj需要插入的索引位置。
	obj -- 要插入列表中的对象。
'''

Example:

list6 = ['太阳','月亮','地球','火星','木星']
print(list6,"长度为:",len(list6))
#使用insert()插入
list6.insert(2,"天王星")
print(list6,"插入后长度为:",len(list6))
'''
    运行结果:
    ['太阳', '月亮', '地球', '火星', '木星'] 长度为: 5
    ['太阳', '月亮', '天王星', '地球', '火星', '木星'] 插入后长度为: 6
'''

III. Delete elements from a list

1.remove () method

Description:

remove () function is used to remove the first occurrence of a value in the list.

Syntax:

list.remove(obj)
'''
	obj -- 要移除的列表中的对象。
'''

Example:

list7 = ['太阳','月亮','地球','火星','木星']
list7.remove('太阳')
print(list7,"长度为:",len(list7))
'''
    运行结果:
    ['月亮', '地球', '火星', '木星'] 长度为: 4
'''

2.del statement

Description:

del is a statement that he is the direct destruction of an object that can be used to destroy not only the list of elements can destroy the entire list

Syntax:

del listname[index]
'''
	index -- 需要销毁对象的索引位置。
'''

Example:

list8 = ['太阳','月亮','地球','火星','木星']
del list8[1]
print(list8,"长度为:",len(list8))
'''
    运行结果:
    ['太阳', '地球', '火星', '木星'] 长度为: 4
'''

list9 = ['太阳','月亮','地球','火星','木星']
del list9[0:3]
print(list9,"长度为:",len(list9))
'''
    运行结果:
    ['火星', '木星'] 长度为: 2
'''
del  list9 #直接销毁整个列表

3.pop () method

Description:

pop () function is used to remove the value of an element in the list (default to the last element), and returns the element.

Syntax:

list.pop([index=-1])
'''
	index -- 可选参数,要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值。
'''

Example:

list10 = ['张三','李四','王五','赵六']
list10.pop()
print(list10,"长度为:",len(list10))
'''
    运行结果:
    ['张三', '李四', '王五'] 长度为: 3
'''
list11 = ['张三','李四','王五','赵六']
list11.pop(2)
print(list11,"长度为:",len(list11))
'''
    运行结果:
    ['张三', '李四', '赵六'] 长度为: 3
'''

IV. Other common methods

1.count()

Description:

count () method used to count the number of an element that appears in the list.

Returns:

Returns the number of elements appear in the list.

Syntax:

list.count(obj)
'''
	obj --  统计对象。
'''

Example:

list12 = ['11','11','22','22','33','44','55','66',]
print('字符串11',"在list12中出现的次数为",list12.count('11'))
'''
    运行结果:
    字符串11 在list12中出现的次数为2
'''

2.index()

Description:

index () function is used to find the location of an index value of the first match from the list.

Syntax:

list.index(obj)
'''
    obj-- 需要查找的对象。
'''

Returns:

This method returns the index to find the location of the object, if the object is not found exception is thrown.

Example:

list13 = ['11','11','22','22','33','44','55','66',]
print('字符串11',"在list13中出现的位置为",list13.index('11'))
'''
    运行结果:
    字符串11 在list13中出现的位置为 0
'''

3.reverse()

Description:

reverse () function is used to reverse the list of elements.

Returns:

This method does not return a value, but the elements of the list will reverse the sort order.

Syntax:

list.reverse()

Example:

list14 = ['张三','李四','王五','赵六']
list14.reverse()
print(list14)
'''
    运行结果:
    ['赵六', '王五', '李四', '张三']
'''

4.sort()

Description:

sort () function is used to sort the list of the original, if the parameter is specified using the comparison function specified comparison function.

Syntax:

list.sort( key=None, reverse=False)
'''
	key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
	reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。
'''

Example:

list15 = [2,3,1,5,4,8,6,7,9]
list15.sort()
print(list15)
'''
    运行结果:
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
'''
list15.sort(reverse=True)
print(list15)
'''
    运行结果:
    [9, 8, 7, 6, 5, 4, 3, 2, 1]
'''

V. copy on the list

Copy of the list to avoid this form = list1 list2, should list1 = list2 [:] copying this form, details, see the following example demonstrates:

list16 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list_1=list16
list_2=list16[:]
print("list16:",list16)
print("list_1:",list_1)
print("list_2:",list_2)
'''
    运行结果:
    list16: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    list_1: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    list_2: [1, 2, 3, 4, 5, 6, 7, 8, 9]
'''
print("------分割线-----")
list16.reverse()
print("list16:",list16)
print("list_1:",list_1)
print("list_2:",list_2)
'''
    运行结果:
    list16: [9, 8, 7, 6, 5, 4, 3, 2, 1]
    list_1: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    list_2: [1, 2, 3, 4, 5, 6, 7, 8, 9]
'''

'' '
Run results:
list16: [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9]
list_1: [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9]
list_2: [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9]
'' '
Print ( "parting line ------ -----")
list16.reverse ()
Print ( "list16: ", list16)
Print (" list_1: ", list_1)
Print (" list_2: ", list_2)
'' '
run results:
list16: [. 9,. 8,. 7,. 6,. 5,. 4,. 3, 2,. 1]
list_1 : [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9]
list_2: [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9]
'' '


> 初学python有问题请私信博主,大家共同学习,持续更新中ヾ(◍°∇°◍)ノ゙。
Released eight original articles · won praise 7 · views 1244

Guess you like

Origin blog.csdn.net/CN_Orange_/article/details/104830606