Python 3 list operations

Source: Internet
Author: User
Tags shallow copy

Create List subject = ["Liunx", "Python", "web", "Java"]

#读取列表print (subject) #打印显示 [' Liunx ', ' python ', ' web ', ' Java '] #列表的切片, the slice index is the 0-based print (subject[0]) # NO. 0 index, That is, the first element of the list print (subject[1]) print (subject[2]) print (subject[3]) print (Subject[1:3]) # reads the second element of the list to the fourth element, the fourth element is not taken, Halves print (subject[-1]) #读取列表的最后一个元素print (Subject[-2]) # reads the penultimate element of the list print (subject[-3:]) #读取列表的倒数第三个元素至最后一个元素 # Print show liunxpythonwebjava[' python ', ' web ']javaweb[' python ', ' web ', ' Java '

Append to List

Create List subject = ["Liunx", "Python", "web", "Java"]

#追加列表subject. Append ("PHP") #打印列表print (subject) [' Liunx ', ' python ', ' web ', ' Java ', ' php ']# #追加内容到指定的位置subject. Insert (1 , "Windows") # inserts Windows into the # 1th index # Print List (subject) [' Liunx ', ' windows ', ' Python ', ' web ', ' Java ', ' php '] # 1th index has become windows Subject.insert (2, "UI") #打印 list print (subject) [' Liunx ', ' windows ', ' UI ', ' Python ', ' web ', ' Java ', ' PHP ']

# Deletion of the list

Create List subject = [' Liunx ', ' windows ', ' UI ', ' python ', ' web ', ' Java ', ' PHP ']

#删除指定元素subject. Remove ("PHP") #打印列表print (subject) [' Liunx ', ' Unix ', ' UI ', ' python ', ' web ', ' Java '] #根据索引删除del subject[0] #删除Liunx # printed list print (subject) [' Unix ', ' UI ', ' python ', ' web ', ' Java ']# pop default Delete last element, here Delete javasubject.pop () #打印列表 [' Unix ' , ' UI ', ' python ', ' web '] #查找某个名称的索引print (subject.index ("Python")) 2

#统计元素的个数

Create List subject = [' Liunx ', ' windows ', ' UI ', ' python ', ' web ', ' Java ', ' php ', ' python ']

Print (Subject.count ("Python")) print (subject) The inverse of the list subject.reverse () print (subject) [' Python ', ' php ', ' Java ', ' web ' , ' Python ', ' UI ', ' windows ', ' Liunx '] #列表的排序 collation according to Assci Code rules subject.sort () print (subject) [' Liunx ', ' UI ', ' Java ', ' php ', ' p Ython ', ' python ', ' web ', ' windows ']# List of Clear subject.clear () print (subject) []

Extension of the list extend

Create a list

Subject = [' Liunx ', ' windows ', ' UI ', ' python ', ' web ', ' Java ', ' php ', ' python ']

Subject2 = ["A", "B", "C"]

Subject.extend (SUBJECT2) print (subject) print (SUBJECT2) #打印列表 [' Liunx ', ' windows ', ' UI ', ' python ', ' web ', ' Java ', ' php ', ' Python ', ' A ', ' B ', ' C ' [' A ', ' B ', ' C ']

#列表的copy

  Create List   subject = ["Liunx", "Python", "web", ["C + +", "Ruby"], "Java"] 

subject = ["Liunx", "Python", "web", ["C + +", "Ruby"], "Java"] subject3 = subject.copy () # Copy subject  content # Print List print (subject) print (SUBJECT3) [' Liunx ',  ' python ',  ' web ',  [' C + + ',  ' Ruby '],  ' java ' [' Liunx ',  ' python ',  ' web ',  [' C + + ',  ' Ruby '],  ' java '] #修改   The contents of the subject  element subject[0] = "gnu liunx subject[3][0]=" C + + #打印列表print (subject) print (SUBJECT3) [ ' gnu liunx  ',  ' python ',  ' web ',  [' C + + ',  ' Ruby '],  ' java '] # subject  [' liunx ',  ' python ',  ' web ',  [' C + + ',  ' Ruby '],  ' java '] # subject  3 can see   modify subject[0] = "gnu liunx   content does not take effect in subject3 , subject[3][0]=" C + + "  This element takes effect. Shallow copy  only copy  a layer, nested part of will not be copy, just copy  the memory address inside, when subject[0] = "gnu liunx  Equivalent to re-opened a memory address, so subject3[0]  read the element content or liunx shallow copy   corresponding is a deep copy import copysubject = ["Liunx", " Python "," web ", [" C + + "," Ruby "],"Java", "PHP"] #  inside re-assigned value subject[0] = "small liunx " subject[3][0]= "C + +" subject3 =  copy.deepcopy (subject) print (subject) print (SUBJECT3) [' small liunx  ',  ' python ',  ' web ',  [' C + + ',  ' Ruby '],  ' java ',  ' PHP ' [' small liunx  ',  ' python ',  ' web ',  [' C + + ',  ' Ruby '],  ' java ',  ' PHP ']

#列表的循环取值

#创建列表 subject = ["Liunx", "Python", "web", ["C + +", "Ruby"], "Java", "PHP"]

Subject = ["Liunx", "Python", "web", ["C + +", "Ruby"], "Java", "PHP"]for i in Subject:print (i) #打印列表Liunxpythonweb [' C + + ', ' Ruby ']javaphp

The list operation for Python is now complete.

Python 3 list operations

Related Article

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.