Python path -05-list or tuple

Source: Internet
Author: User

5.1 List

5.1.1 Definition List

names = ["Zhangsan","Lisi","Wangwu","Liuxiao"]

Note: The list is ordered and can be added and censored. Lists can be nested in lists, dictionaries, and anything else

5.2 Slices

to access the elements in the list by subscript, the index is counted starting at 0

5.2.1 fetching Elements

#! Author:lanhan
#切片
names = [ "Zhangsan" , "Lisi" , "Wangwu" , "Liuxiao" ]
Print (names)
Print (names[0],names[2])    #分别在names列表取值 (number from left to right, first digit 0, and so on)
print (Names[1:3])            #在names列表一起取值 (Gu Tou regardless of tail)
################################## ##########################################
print (names[3])              #取最后一位
print (names[-1])            &NBSP #取最后一位 (starting from left, 1, and so on)
print (names[-2:-1])         &NBSP #取倒数第二位 (starting from right, reading from left, Gu Tou ignoring tail)
print (names[-2:])          &NBSP #取倒数2位, omitted after colon

5.2.2 Append

List.append (" add element ")

5.2.2 Insertion

List.insert (subscript value ," insert element ")

5.2.3 Modification

list [subscript value ] = " modified element "

5.2.4 Delete

List.remove ("deleted elements")

Del list [subscript value ]

List.pop (subscript value ) # subscript is not written, the last one is deleted by default

#! Author:lanhan
#切片
names = ["Zhangsan","Lisi","Wangwu","Liuxiao"]
Print (names)
# #新增
Names.append ("Lanhan1")# # #将lanhan1插到最后面
Print"New", names)
Names.insert (2,"Lanhan2")#将lanhan2插入到wangwu前面
Print"New", names)
# #修改
names [1] ="Lanhan3"#将lisi替换成lanhan3
Print"Modify", names)
# #删除
Names.remove ("Wangwu")#直接列表元素删除
Print"Delete", names)
delnames [0]#下标删除
Print"Delete", names)
Names.pop (2)#输入下标删除 (does not enter the subscript, the last element is deleted by default)
Print"Delete", names)

# # Print # #

[' Zhangsan ', ' Lisi ', ' Wangwu ', ' Liuxiao ']

New [' Zhangsan ', ' Lisi ', ' Wangwu ', ' Liuxiao ', ' lanhan1 ']

New [' Zhangsan ', ' Lisi ', ' lanhan2 ', ' Wangwu ', ' Liuxiao ', ' lanhan1 ']

Modify [' Zhangsan ', ' lanhan3 ', ' lanhan2 ', ' Wangwu ', ' Liuxiao ', ' lanhan1 ']

Delete [' Zhangsan ', ' lanhan3 ', ' lanhan2 ', ' Liuxiao ', ' lanhan1 ']

Delete [' Lanhan3 ', ' lanhan2 ', ' Liuxiao ', ' lanhan1 ']

Delete [' Lanhan3 ', ' lanhan2 ', ' lanhan1 ']

5.2.5 The index of the unknown element search

List.index ("search string")

5.2.6 Statistics

List.count ("string of Statistics")

5.2.7 Reversal

List.reverse ()

5.2.8 Sort

List.sort ()

5.2.9 Merging

List.extend (list2) # merges list2 into the list of lists

5.3.0 deleting variable names

Del variable name

5.3.1 Copy List

List.copy ()

Note: You can copy only the first-level list, not the second-level list ( light copy)

Import copy module (deep cpy)

5.3 meta-group

Tuples are actually similar to the list, but also to save a group of numbers, it is not once created, it can not be modified, so it is called a read-only list (tuples are ordered)

syntax:names = ("Alex", "Jack", "Eric")

it has only 2 methods, one is count, the other is index

Python path -05-list or tuple

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.