• Python的list、tuple、dict常用方法


    重点:List里面的元素是可以更改值的,tuple里面的元素值是无法改变的。为了安全,可以使用tuple的时候尽可能使用tuple

    dict是key:values的值的,可以进行修改。

    List-列表的常用方法:

    list.append 在列表中追加(最后面添加)元素的方法:

     1 # #! /usr/bin/env python
     2 # # _*_coding:UTF - 8*_*
     3 # shoplist = ['apple', 'mango', 'carrot', 'banana']
     4 # print "\"shoplist\" 的数据类型是:%s,总共有%s个商品。" % (type(shoplist) ,len(shoplist))
     5 # print "当前的购物列表是:%s" % shoplist
     6 #
     7 # B="列表list的input使用"
     8 # # print "********************list的input使用********************"
     9 # print "***" * 10 +B + "***" * 10
    10 # x = raw_input("请输入需要追加到购物列表后面的商品:\n")
    11 # shoplist.append(x)
    12 # print shoplist
    13 # print "使用\"list\"的\"append\"追加的购物列表是:%s" % shoplist
    14 #
    15 # print "***list的insert使用" * 20
    16 # insertA = input("请输入需要插入到列表位置的序号:\n")
    17 # insertB = raw_input("请输入需要添加的商品名:\n")
    18 # shoplist.insert(insertA, insertB)
    19 # print "使用\"list\"的\"insert\"的插入功能的购物列表是:%s"  %shoplist
    20 #
    21 # print "***list的pop使用" * 20
    22 # popA = input("请输入需要删除的商品的序号:\n")
    23 # shoplist.pop(popA)
    24 # print "使用\"list\"的\"pop\"的删除功能的购物列表是:%s"%shoplist
    25 #
    26 # print "***list的remove使用" * 20
    27 # removeA = raw_input("请输入需要删除的商品的名称:\n")
    28 # shoplist.remove(removeA)
    29 # print "使用\"list\"的\"remove\"的删除功能的购物列表是:%s"%shoplist
    30 #
    31 # shoplist.sort()
    32 # print shoplist
    33 #
    34 # shoplist.reverse()
    35 # print (shoplist)
    36 
    37 x = ("1a","2b","3c")
    38 print (dir(x))
    Python list

    tuple两个参数:count    index

    conut:统计所属tuple中元素的个数

    index:统计所属tuple中元素的下标

    1 tupA = ("abc" , "aaa" , "bbc" , "abbccc" , "123" , 456 , "aaa")
    2 
    3 tupB = ("DEF" , "aaa" , "bbc" , "abbccc" ,  "789" , 777 , "aaa")
    4 #count()方法是查元组中元素的个数。
    5 print tupA.count("aaa")
    6 #index()方法是查元组中元素的坐标(下标)。
    7 print tupB.index("abbccc")
    Python tuple

    dict常用的方法:

    clear()

    get

    setdefault

    keys

    values

    iteritems

    update

    fromkeys  

    zip

    pop

     测试截图:

    1 dictA = {"name":"King", "age":29, "sex":"man"}
    2 
    3 print dictA
    4 
    5 print dictA.keys()
    6 print dictA.values()
    7 print dictA.get("sex")
    8 print dictA.setdefault("address","beiJing")
    9 print dictA.items()
    Python dict
  • 相关阅读:
    第九周周记
    第七周周记
    第三次作业第一题
    第五周周记
    《世界是数字的》读后感想
    第十周周记
    迷茫
    测试作业
    价值观作业
    作业二 感想
  • 原文地址:https://www.cnblogs.com/kelake/p/7718410.html
Copyright © 2020-2023  润新知