python-Base List Collection Dictionary

Source: Internet
Author: User

Directory
    1. list, tuple operations
    2. String manipulation
    3. Dictionary operations
    4. Collection operations
    5. File operations
    6. Character encoding and transcoding
1. List, tuple operations

Lists are one of the most commonly used data types and can be easily stored, modified, and manipulated by the list.

Definition List

names = ["Zhang San", "John Doe", "pock"]

The data in the list is taken by subscript. Subscript starts at 0

names = ["Zhang San", "John Doe", "Pock"]print (Names[0]) Zhang San Print (names[1]) John Doe Print (names[2]) Pock Note: You can also rewind print (names[-3]) Zhang San print ( NAMES[-2]) John Doe Print (names[-1]) pock

Slices: Take multiple elements (Gu Tou regardless of tail.)

names = ["Zhang San", "John Doe", "Pock", "Madman", "Fool"]print (Names[0:2]) [' Zhang San ', ' John Doe ']print (Names[:3]) [' Zhang San ', ' John Doe ', ' pock ']print (names[1:]) [' John Doe ', ' pock ', ' lunatic ', ' fool ']print (Names[0::2]) [' Zhang San ', ' pock ', ' Fool ']print (Names[::2]) [' Zhang San ', ' pock ', ' fool ']

Append (added in last one)

names = ["Zhang San", "John Doe", "Pock", "Madman", "Fool"]names.append ("SB") print (names)
[' Zhang San ', ' John Doe ', ' pock ', ' lunatic ', ' idiot ', ' SB ']

Insert (insert anywhere)

names = ["Zhang San", "John Doe", "Pock", "Madman", "Fool"]names.insert (2, "old Cold Legs") Names.insert (5, "Bigfoot") print (names) [' Zhang San ', ' John Doe ', ' old cold legs ', ' pock ', ' Madman ', ' Bigfoot ', ' fool ', ' SB '

Modify

names = ["Zhang San", "John Doe", "Pock", "Madman", "fool"]names[2] = ("Leper") print (names) [' Zhang San ', ' John Doe ', ' leper ', ' lunatic ', ' fool ']

Delete

#指定删除下标对应的names = ["Zhang San", "John Doe", "Pock", "Madman", "fool"]del  names[4]print (names) [' Zhang San ', ' John Doe ', ' pock ', ' Madman ']# Use Remove to delete only the first occurrence of names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "Fool"]names.remove ("John Doe") print (names) [' Zhang San ', ' John Doe ', ' pock ', ' Madman ' , ' fool '] #使用pop删除时删除列表中的最后一个值names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "Fool"]names.pop () print (names) [' Zhang San ', ' John Doe ', ' John Doe ', ' pock ', ' Madman ']

Copy

names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "fool"]names_copy = Names.copy () print (names_copy) [' Zhang San ', ' John Doe ', ' John Doe ', ' pock ', ' lunatic ', ' fool ']

Statistics

names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "Fool"]print ("John Doe Occurrences:", Names.count ("John Doe")) John Doe Number of occurrences: 2

Sort

names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "Fool", "3", "2"]names.sort () print (names) [' 2 ', ' 3 ', ' fool ', ' Zhang San ', ' John Doe ', ' John Doe ', ' pock ', ' lunatic ']
#3.0 The different data types cannot be sorted together

Get subscript

names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "Fool", "1", "3", "2"]print ("John Doe Subscript is:", Names.index ("John Doe")) John Doe subscript is: 1
#只返回找到的第一个下标

  

  

 

python-Base List Collection Dictionary

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.