zoukankan      html  css  js  c++  java
  • 0-0 列表,元组,字典,集合

    (1)列表
     1 # -*-coding:utf-8-*-
     2 # !/usr/bin/env python
     3 # Author:@vilicute
     4 # 创建列表
     5 myList = [x for x in range(10) if not x % 2]  # myList = [0, 2, 4, 6, 8]
     6 myList1 = ["aaa", "bbb", "ccc", "ddd", "eee", "fff"]
     7 myList2 = ["ggg", "hhh", "iii", "jjj"]
     8 # 列表操作
     9 print(myList[2], myList[-2], myList1[:3], myList1[-3:-1], myList1[1:4]) 
    10 myList1.insert(1, "liangba")  # 在指定位置插入元素
    11 myList1.append("xxx")  # 在末尾插入元素
    12 myList1.remove("xxx")  # 直接删除某个元素
    13 del myList1[1]  # 通过下标删除元素
    14 myList1.pop()  # 默认无下标时删除最后一个,有则删除制定位置,例如:myList1.pop(1)
    15 myList1.count(myList1[2])  # 统计相同项个数
    16 myList1.reverse()   # 反转
    17 xx = myList1.index("ccc")  # 位置查询
    18 myList1.sort()  # 按字母顺序排序
    19 myList1.extend(myList2)  # 列表合并
    20 del myList2  # 删除 myList2
    (2)元组
    与列表不同,元组一旦创建,就无法对元组中的元素进行添加、删除、替换、或重新排序等操作,但可以访问,即可读而不可操作。
     1 # -*-coding:utf-8-*-
     2 # !/usr/bin/env python
     3 # Author:@vilicute
     4 # 元组的创建
     5 tuple1 = (1, 2, 3)  # 创建一个元组
     6 tuple2 = tuple([x for x in range(1, 10, 2)])  # 通过列表创建元组
     7 tuple3 = tuple("abcde")  # 通过字符串创建元组
     8 print("tuple1[2] = ", tuple1[2], "
    tuple2 = ", tuple2, "
    tuple3 = ", tuple3)  # 可通过下标访问
     9 '''
    10 tuple1[2] =  3 
    11 tuple2 =  (1, 3, 5, 7, 9) 
    12 tuple3 =  ('a', 'b', 'c', 'd', 'e')
    13 '''
    14 Len = len(tuple3)  # 元组长度
    15 Max = max(tuple2)  # 最大值
    16 Min = min(tuple2)  # 最小值
    17 Sum = sum(tuple2)  # 求和
    18 tuple4 = 2*tuple2  # 复制扩展
    19 tuple5 = tuple1 + tuple2  # 元组连接
    20 List = list(tuple2)  # 转为列表
    21 print(" Max = ",Max, " Min = ",Min, " Sum = ", Sum, '
    ', "tuple4 = ", tuple4, "
    tuple5 = ", tuple5, "
    List = ", List)
    22 '''
    23 Max =  9  Min =  1  Sum =  25 
    24  tuple4 =  (1, 3, 5, 7, 9, 1, 3, 5, 7, 9) 
    25 tuple5 =  (1, 2, 3, 1, 3, 5, 7, 9) 
    26 List =  [1, 3, 5, 7, 9]
    27 '''
    28 print('Tuple =',2*tuple([x for x in range(10) if not x%2]))
    29 # Tuple = (0, 2, 4, 6, 8, 0, 2, 4, 6, 8)
    (3)字典
    形式:dict = {关键字1 : 键值1, 关键字2 : 键值2, ...},字典不能包含重复的关键字!
     1 # -*-coding:utf-8-*-
     2 # !/usr/bin/env python
     3 # Author:@vilicute
     4 dict1 = {"1123": "Zhangsan", "1124": "Lisi", "1125": "Wangwu"}
     5 dict1["1126"] = "Zhaoliu"  # 添加一个条目
     6 for key in dict1:   # 访问
     7     print(key + ':' + str(dict1[key]))
     8 # len, ==,!= 略
     9 # 字典方法:
    10 xz = tuple(dict1.keys())  # 关键字序列
    11 xx = tuple(dict1.values())  # 值序列
    12 xc = tuple(dict1.items())  # 元组序列
    13 xv = dict1.get("1126")  # 关键字对应的值
    14 xb = dict1.pop("1123")  # 删除关键字对应的条目并返回它的值
    15 xn = dict1.popitem()  # 返回随机的键值作为元组并删除这个被选条目
    16 del dict1["1124"] # 删除对应条目
    17 dict1.clear()   # 删除所有条目
    18 print("xz=", xz, '
    ', "xx=", xx, '
    ', "xc=", xc, '
    ', "xv=", xv, '
    ', "xb=", xb, '
    ', "xn=", xn)
    19 '''
    20  xz= ('1123', '1124', '1125', '1126') 
    21  xx= ('Zhangsan', 'Lisi', 'Wangwu', 'Zhaoliu') 
    22  xc= (('1123', 'Zhangsan'), ('1124', 'Lisi'), ('1125', 'Wangwu'), ('1126', 'Zhaoliu')) 
    23  xv= Zhaoliu 
    24  xb= Zhangsan 
    25  xn= ('1126', 'Zhaoliu')
    26 '''
    (4)集合   
    特点:没有重复元素,无序。
     1 # -*-coding:utf-8-*-
     2 # !/usr/bin/env python
     3 # Author:@vilicute
     4 # 集合的创建,类似元组的创建方法
     5 Set1 = set()
     6 Set2 = {1, 2, 3, 4}
     7 Set3 = set([x for x in range(1, 10, 2)])
     8 Set4 = set("abcde")
     9 Set5 = set((2, 3))
    10 List = list(Set4)  # 转为列表
    11 # 集合的操作
    12 # len(), max(), min(), sum(), 略
    13 Set3.add(8)  # 添加一个元素
    14 Set3.remove(5)  # 删除一个*存在*的元素
    15 tf = (Set3 == Set4)  # 相等性测试  Ture or False
    16 Sub = Set3.issubset(Set1)  # Set3是否是Set1的子集  Ture or False
    17 Sup = Set3.issuperset(Set2)  # Set3是否是Set2的超集  Ture or False  Ture: Set2属于Set3
    18 Union = Set1 | Set2  # 并集  == Set1.union(Set2)
    19 Intersection = Set1 & Set2  # 交集  == Set1.intersection(Set2)
    20 Difference = Set1 - Set2  # 差集  == Set1.difference(Set2)
    21 xxx = Set1 ^ Set2  # 异或  == Set1.symmetric_difference(Set2)
  • 相关阅读:
    在MAC OS X系统上面安装mysql
    在项目中使用DSOFramer需要注意的一些地方
    记一次python安装PIL库所遇到的事
    Calendar.compareTo 比较时间的大小
    String 时间类型怎么进行比较大小?
    ElasticSearch--二、基本语法(创建索引,查询数据)
    ElasticSearch--一、使用场景以及对应软件配置安装
    Linux下nginx反向代理负载均衡几种方式以及配置
    node环境使用lowdb轻量数据库以及基本用法
    jQuery 日常笔记
  • 原文地址:https://www.cnblogs.com/vilicute/p/11607865.html
Copyright © 2011-2022 走看看