Python常用命令之集合

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python常用命令之集合相关的知识,希望对你有一定的参考价值。

  • 判断数据项是否存在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常用命令之集合的主要内容,如果未能解决你的问题,请参考以下文章

    常用python日期日志获取内容循环的代码片段

    python常用代码片段总结

    前端开发常用js代码片段

    Python之常用模块学习

    python数据类型之集合(set)和其常用方法

    laravel特殊功能代码片段集合