zoukankan      html  css  js  c++  java
  • 02.Python基础--数据类型

    Python数据类型2:

    list 列表

    1.列表组合:
    list1 = [1,2,3]
    list2 = [4,5,6]
    print(list1 + list2)
    结果: [1, 2, 3, 4, 5, 6]
    
    2.列表重复:
    list = [1,2,3]
    print(list*3)
    结果: [1, 2, 3, 1, 2, 3, 1, 2, 3]
    
    3.判断元素是否在列表里
    list = [1,2,3,4,5]
    print(2 in list)
    print(6 in list)
    结果:  True
          False
    
    4.列表截取:
    list = [1,2,3,4,5,6,7,8,9]
    print(list[0:2]) #索引从0开始,0,1,2索引对应的应该是1,2,3,但是左闭右开(左边包含,右边不包含)
    结果: [1,2]
    
    5.二维列表拿数据
    list = [[1,2,3],[4,5,6],[7,8,9]]
    print(list[0][0])
    print(list[1][0:2])
    # 依然是左闭右开原则
    结果: 1
    结果: [4,5]
    
    *********************************************************************************************
    list 列表
    
    list.append()
    在列表的末尾添加新的元素(且只能是一个元素)
    
    list.extend()
    在列表末尾追加另一个列表中的多个元素
    
    list.insert()
    在下标出添加一个元素(这个元素也可以是个列表),原数据不变,原数据向后顺延
    list.insert(index,元素/列表)
    也可以直接插入:list[index] = 元素/列表
    
    list.pop()
    list[-1]是列表最后一个元素的下标
    移除列表中指定下标处的元素(默认移除最后一个元素),并返回删除的数据
    
    list.remove()
    移除列表中的某个元素(不按照索引来计算)
    
    list.clear()
    清除列表中所有数据
    
    index = list.index()
    找出列表里某个元素对应的索引
    
    len() 元素个数
    max() 最大值
    min() 最小值
    
    list.count()
    查看元素在列表中出现的次数
    
    list.reverse()
    排序,倒叙
    list.sort()
    排序,升序
    
    list.copy()
    拷贝
    list2 = list1.copy()
    
    将元祖转化为列表
    tuple1 = (1,2,3)
    list = list(tuple1)

    tuple 元祖

    操作与list基本一致,区别于list是不可变,具有较好的安全性

       tuple(list)

    set 集合

     交集 、并集 、供列表去重

      set(list)

      set1 | set2

      set & set2

    字典 dict

    key value 键值对储存

    key是唯一对象 不可变对象

      value = dict.get('key')

      for key in dict:

      for value in dict.values():

      for k , v in dict.items():

      for i , v in enumerate(dict): 

      







  • 相关阅读:
    linux下apache(httpd)安装部署
    基于redis分布式缓存实现(新浪微博案例)
    python--字典
    django(四)
    django(三)
    django(二)
    django(一)
    053(八十)
    053(七十九)
    053(七十八)
  • 原文地址:https://www.cnblogs.com/zhouA/p/14470341.html
Copyright © 2011-2022 走看看