zoukankan      html  css  js  c++  java
  • Day02--python数据类型之列表、元组、字典和集合

                                                  python中的列表

    # 列表
    # list 类,列表
    # 中括号括起来 ,逗号分隔每个元素,列表中的元素可以是数字,字符串,列表,布尔值等等。
    # 列表还可以嵌套列表

    =========列表的基本操作=========
    (1)列表的常用操作
    list1 = [11,22,33,44,55]
    # len 查看列表的元素的个数
    print(len(list1))
    
    # 通过索引取值
    print(list1[3])
    
    # 对列表进行切片
    print(list1[0:2])
    print(list1[:])

    # for循环操作
    for i in list1:
      print(i)

    (2)列表元素,可以被修改

    # 1.通过列表索引修改元素
    list1 = [11,22,33,44,55]
    list1[1] = 123
    print(list1)
    
    # 2.通过列表切片修改元素
    list1 = [11,22,33,44,55]
    list1[1:3] = [222,333]
    print(list1)

    以上的运行结果为:

     (3)列表转换成字符串的注意事项:
    1.如果列表中既有数字又有字符串时,需要用for循环转换。

    # 列表中既有数字又有字符串
    list1 = [11,22,33,44,"hello","python"]
    string = ""
    for i in list1:
        string = string + str(i)
    print(string)

    运行结果为:

     2.如果列表中只有字符串,列表转换成字符串可以用join方法

    list1 = ["hello","python"]
    v1 = "".join(list1)
    print(v1)

    运行结果为:

                                                                                    ============列表的常用方法=========

    list1 = [11,22,33,44,55]
    
    # 1.append 追加元素到列表,也可追加列表
    v1 = list1.append(235)
    print(list1)
    
    # 2.clear 清空列表
    list1.clear()
    print(list1)
    
    # 3.copy 拷贝,浅拷贝
    list1 = [11, 22, 33, 44]
    v1 = list1.copy()
    print(v1)
    
    # 4.计算元素出现的次数
    v1 = list1.count(11)
    print(v1)
    # 5.insert 在指定索引位置插入元素,有两个参数,参数一指定插入的索引,参数二参入的元素 list2 = [11, 22, 33, 44, "hello", "python"] list2.insert(1, 99) print(list2) # 6.列表的删除操作 # remove 删除列表中指定的元素 list2.remove(22) del list2[1] # 通过索引删除 del list2[1:5] # 通过切片删除 # pop() 删除某个值,并获取删除值,没有指定索引,默认删除最后一个元素 # v1 = list2.pop(1) # print(list2) # print(v1) # 7.extend 扩展原来的列表,必须是可迭代对象(内部执行了for循环) list2 = [11, 22, 33, 44, "hello", "python"] list2.extend([111, 222]) list2.extend("hello") print(list2) # 8.reverse,对当前列表进行翻转 li = [11, 22, 33, 44, 55] li.reverse() print(li) # 9.sort 排序(默认升序排列)sort(reverse=False) li = [11, 23, 10, 24, 26, 39, 73, 55] li.sort(reverse=True) # 降序排列 print(li)
    第七题运行的结果:

                                                                 python中的元组

    # 元组,元组不可被修改,不能被增加或者删除
    # 一般写元组的时候,推荐在最后加上逗号
    # 元组的一级元素不可修改/删除或增加
    # 元组用法同列表,切片,成员运算in / not in

    # 索引
    age = (11, 22, 33, 44, 55)
    v = age[2]
    
    # 切片
    v1 = age[0:3]
    print(v, v1)
    
    # for循环
    for i in age:
        print(i)
    
    # in or not in
    print(22 in age)
    print(123 not in age)        

                                                           python中的字典

    # 字典的value可以是任意值,字典、列表、元组、字符串等。
    # 列表和字典不能作为字典的key,元组可以作为字典的key,
    # 布尔值可以作为key,但是当有字典的键为1/0时,哪一个在前面,后面的键值对就不显示。
    # 字典是无序的
    info = {'name': 'egon', 'age': '18', 'sex': 'male'}
    
    # 字典的取值
    print(info['name'])
    
    # 字典的删除
    del info['name']
    print(info)
    
    # 字典的for循环,默认循环输出所有的key
    for i in info:
        print(i)

    # 通过values()方法可以得到字典的values
    info = {'name': 'egon', 'age': '18', 'sex': 'male'}
    for i in info.values():
        print(i)
    
    # for 循环得到key和value
    for k, v in info.items():
        print(k, v)

    以上运行结果为:

                                                         =============字典的常用方法========

    dict1 = {'hello': "python", "world": "java"}
    
    # 1.fromkeys() 根据序列,创建字典,并指定统一的值
    
    v1 = dict.fromkeys(["k1", "999", 123], 111)
    print(v1)
    
    # 2.get() 根据key获取值,key不存在时,可以指定默认值(None)
    
    dict1 = {'hello': "python", "world": "java"}
    v = dict1.get('hello', 'hadoop')
    print(v)
    
    # 3.pop() 删除并获取值
    
    dict1 = {'hello': "python", "world": "java"}
    v = dict1.pop('hello')
    print(v)
    
    # 删除字典所有元素 v1
    = dict1.popitem() print(v1) # 4.setdefault() 设置值,已存在不设置,不存在设置,获取当前key对应的值 dict1 = {'hello': "python", "world": "java"} v1 = dict1.setdefault('hello', 123) print(dict1, v1)
    v2
    = dict1.setdefault('jsddd', 'jjshhd') print(dict1, v2)
    # 5.update() 更新 dict1 = {'hello': "python", "world": "java"} dict1.update({'k1': 1111, 'k2': 999}) print(dict1) dict1.update(k1=234, k2=3344, k3=9988) print(dict1)

    第四题运行结果为:

                                                           python中的集合

    # 集合中是由一组无序的可hash值,可以作为字典的key
    # 集合由不同的元素组成,并且无序,集合中的元素必须是不可变类型
    # 不可变类型:数字,字符串,元组
    # 集合也是可迭代类型,可迭代类型还有字符串,列表,元组,集合,字典
    
    
    # set 定义
    s = {11, 22, 33, 44, 55}
    
    # add() 添加元素
    s.add('hello')
    print(s)
    
    # clear() 清除集合
    s.clear()
    
    # 集合删除元素
    # 1.s.pop() 随机删除元素
    s.pop()
    print(s)
    
    # 2.s.remove()  # 指定删除集合元素
    s.remove(11)
    print(s)
    
    # 3.s.discard()  # 删除元素,如果元素不存在,不会报错
    s.discard('hello')
    print(s)
    print("========================")
    # 4.update() 更行多个值 s1 = {1, 2, 3, 4} s2 = {1, 3, 5, 7, 8} s1.update(s2) s1.update((5,6,7,8,9)) update 后面跟的数必须是可迭代类型 s1.add((2, 3, 4, 5, 6, 7, 8, 9)) # add后面跟的数必须是不可变类型 s1.add("hello world") # add() 只能更新一个值 s1.union(s2) print(s1)
    # union() 求交集操作,原集合数据不变,update()更新操作,源集合发生变化。
    # 5.for循环 for i in s1: print(i)


  • 相关阅读:
    如何保证access_token长期有效
    微信自定义菜单的创建
    是否同一棵二叉搜索树
    Tree Traversals Again(根据前序,中序,确定后序顺序)
    List Leaves 树的层序遍历
    leetcode-优美的排列
    leetcode-下一个排列
    leetcode-二进制手表
    leetcode-组合总数III(回溯)
    leetcode-累加数(C++)
  • 原文地址:https://www.cnblogs.com/shaojie1986/p/12764080.html
Copyright © 2011-2022 走看看