Learning notes for Python/002-3 (2018-5-20)

Source: Internet
Author: User

Cluster Type

Cluster Type List Meta-group Dictionary Collection
List Tuple Dict Set

1. List
The type of list, which is ordered, has an index, and the content and length can be changed. To create a list, you can use the [] direct notation, with each element in the list separated by commas.
Common actions for lists
(1) Sectioning of elements

names=["Zhangfei","Guanyu","Liubie","Zhaoyun","Machoa","Jiangwei","Mashu","Zhaoyun"]print(names)print(names[0])    # 切位置0的元素print(names[0:4])  #  切出位置0~3的元素print(names[-1])     # 切出最后一位的元素print(names[-3:])     # 切片,左取右不取``print(names[0:-1:2])    # 有间隔的切片for i in names:    print(i)           # 循环的切片方法,这个i就代表了列表中的元素


(2) Increase, change and deletion of the list
Names.append (); Names.insert (); Names.remove (); Names.pop ()

names=["Zhangfei","Guanyu","Liubie","Zhaoyun","Machoa","Jiangwei","Masu","Zhaoyun"]print(names)names.append("Zhugelaing")   # [增]-----在列表末尾  追加  诸葛亮print(names)names.insert(3,"daqiao")       # [增]-----在列表中的位置3  插入  大乔print(names)names[2]="zhouyu"            #[改]----- 将刘备  替换  为周瑜print(names)   # names.remove("Masu")      # [删]-----删除 马谡 (有三种常用的方法)# del names[-3]print(names.pop(-3))print(names)


(3) List of check, statistics
Names.index (); Names.count ()

names=["Zhangfei","Guanyu","Liubie","Zhaoyun","Machoa","Jiangwei","Masu","Zhaoyun"]print(names)print(names.index("Zhaoyun"))         # 获取下标,赵云有俩个但是却只查到了列表里的第一个????print(names[names.index("Zhaoyun")])print(names.count("Zhaoyun") )     #   统计  列表中赵云的个数


(4) List reversal, sorting, merging
Names.reverse (); Names,sort (); Names.extend ()

names=["Zhangfei","Guanyu","Liubie","Zhaoyun","Machoa","Jiangwei","Masu","Zhaoyun"]print(names)names.reverse()    #  将列表  反转print(names)names.sort()    # 将列表按照默认的utf-8格式  排序print(names)names2=["Chaochao","Simayi","Diewei","Xiahuodong","Xuchu"]names.extend(names2 )       #  合并俩个列表,但是合并的列表仍然存在print(names)


(5) Copy of list
Names.copy (); Copy.copy (); Copy.deepcopy ()

import copy  # 需要为深copy引入模块names=["Zhangfei","Guanyu","Liubie","Zhaoyun",["zhouyu","daqiao"],"Machoa","Jiangwei","Masu","Zhaoyun"]names1=names    #  完全就是二者相等names2=names.copy()   # 浅copy,第二个列表里其实copy的是一个地址,可以用来实现一个联合账户的功能names3=copy.copy(names) # 三种不同的实现方法names4=names[:]names5=copy.deepcopy(names)   #  深copy一般不用因为会占用更多的空间names[2]="刘邦"     #  将位置2的元素改为刘邦names[4][0]="小乔"   # 将位置4列表里的位置0元素改为大乔print(names)   # 比较几种不同的复制print(names1)print(names2)print(names3)print(names4)print(names5)


(6) Cleanup of the list
Names.clear ()

names=["Zhangfei","Guanyu","Liubie","Zhaoyun","Machoa","Jiangwei","Masu","Zhaoyun"]print(names)names.clear()print(names)

Learning notes for Python/002-3 (2018-5-20)

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.