码迷,mamicode.com
首页 > 编程语言 > 详细

Python常用命令之集合

时间:2020-04-18 22:54:46      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:mod   nio   not   遍历   one   this   set   批量添加   输出   

  • 判断数据项是否存在list
  • 
    thislist = ["apple", "banana", "cherry"]
    if "apple" in thislist:
      print("Yes, ‘apple‘ is in the fruits list")
    • 遍历list
      thislist = ["apple", "banana", "cherry"]
      for x in thislist:
      print(x)

    获取list长度

    thislist = ["apple", "banana", "cherry"]
    print(len(thislist))
    

    追加元素

    thislist = ["apple", "banana", "cherry"]
    
    thislist.append("orange")
    
    print(thislist)

    添加固定位置

    thislist = ["apple", "banana", "cherry"]
    thislist.insert(1, "orange")
    print(thislist)

    删除某元素【只删除一个】

    thislist = ["apple", "banana", "cherry"]
    thislist.remove("banana")
    print(thislist)

    移除最后一个元素

    thislist = ["apple", "banana", "cherry"]
    thislist.pop()
    print(thislist)

    清空List

    thislist = ["apple", "banana", "cherry"]
    thislist.clear()
    print(thislist)

    修改元组值

    
    x = ("apple", "banana", "cherry")
    y = list(x)
    y[1] = "kiwi"
    x = tuple(y)
    
    print(x)

    声明元组

    thistuple = ("apple",)
    print(type(thistuple))
    
    #NOT a tuple type is str
    thistuple = ("apple")

    合并元组Tuple 【不会去重】

    tuple1 = ("a", "b" , "c")
    tuple2 = (1, 2, 3)
    
    tuple3 = tuple1 + tuple2
    print(tuple3)

    构造元组【2个括号】

    thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
    print(thistuple)

    声明Set

    thisset = {"apple", "banana", "cherry"}
    print(thisset)

    Set添加元素

    thisset = {"apple", "banana", "cherry"}
    
    thisset.add("orange")
    
    print(thisset)
    

    批量添加

    thisset = {"apple", "banana", "cherry"}
    
    thisset.update(["orange", "mango", "grapes"])
    
    print(thisset)

    Set 删除元素

    
    thisset = {"apple", "banana", "cherry"}
    //不存在  抛异常
    thisset.remove("banana")
    //不存在  不抛异常
    thisset.discard("banana")
    
    // 不知道删的内容
    x = thisset.pop()
    
    print(thisset)
    

    合并set 内容

    set1 = {"a", "b" , "c"}
    set2 = {1, 2, 3}
    
    set3 = set1.union(set2)
    print(set3)

    合并set 内容(不需要新变量)

    set1 = {"a", "b" , "c"}
    set2 = {1, 2, 3}
    
    set1.update(set2)
    print(set1)
    

    声明set

    thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
    print(thisset)

    Dictionary

    创建字典

    thisdict = {
      "brand": "Ford",
      "model": "Mustang",
      "year": 1964
    }

    访问字段元素

    # 如果没有值,异常
    x = thisdict["mod1el"]
    print(x)
    
    # 如果没有值,返回None
    x = thisdict.get("mo1del")
    print(x)

    输出字典的值

    for x in thisdict.values():
      print(x)

    输出字典的k,v

    for x, y in thisdict.items():
      print(x, y)
    

    复制 Dictionary

    thisdict = {
      "brand": "Ford",
      "model": "Mustang",
      "year": 1964
    }
    mydict = thisdict.copy()
    print(mydict)

    Python常用命令之集合

    标签:mod   nio   not   遍历   one   this   set   批量添加   输出   

    原文地址:https://blog.51cto.com/dba10g/2488431

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!