zoukankan      html  css  js  c++  java
  • 【Python学习笔记】之数据结构

    本文主要记录python学习过程中与数据结构相关的内容。主要会涉及到列表、字典、元组等。

    1 字典

    1.1 如何判断字典中是否存在某个key?

    在Python 2.x 时可以使用内置函数 has_key(),Python 3.x 以后不再支持该函数,被__contains(‘keyname’)所替代。

    推荐使用如下的 in 方法

    ~ $ python3
    
    >>> d = {'name':{},'age':{},'sex':{}}
    >>> print name in d.keys()
    
    True
    

    1.2 删除字典内所有元素

    clear()方法

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    dict = {'name': 'blogsite', 'alexa': 10000, 'url': 'http://blog.csdn.net/'}
    dict.clear()
    

    1.3 删除字典给定键 key 所对应的值,返回值为被删除的值

    pop()方法

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    site= {'name': 'blogsite', 'alexa': 10000, 'url':'http://blog.csdn.net/uuihoo/'}
    pop_obj=site.pop('name') # 删除要删除的键值对,如{'name':'blogsite'}
    print pop_obj   # blogsite
    

    1.4 为字典添加键值对

    如果要为 dict 添加键值对,只需为不存在的 key 赋值即可。实现此操作的语法格式如下:

    dict[key] = value
    

    1.5 计算字典元素个数

    可以使用内置函数 len() 来计算字典元素个数,即键的总数。

    1.6 遍历字典

    items()函数将返回键值对列表,keys()函数将返回键列表,values()函数将返回一个值列表。

    • (a) 遍历所有键值对

      dict = {0:'a', 1:'b', 2:'c', 3:'d'}
      
      for key,value in dict.items():
      	print("\nKey:" + key)
      	print("Value:" + str(value))
      
    • (b) 遍历所有键

      dict = {0:'a', 1:'b', 2:'c', 3:'d'}
      
      for key,value in dict.keys():
      	print(key)
      

      其实还有一种更简便的写法,可以把 keys() 方法省略。
      因为遍历字典时,如果你在 for 后面仅声明一个变量,Python会默认遍历所有的键并依次将键赋值给这个变量。

      dict = {0:'a', 1:'b', 2:'c', 3:'d'}
      
      for key,value in dict:
      	print(key)
      
    • (c) 遍历所有值

      dict = {0:'a', 1:'b', 2:'c', 3:'d'}
      
      for key,value in dict.values():
      	print("Value:" + str(value))
      

    1.7 合并字典

    合并字典有两种方法,一种是使用 update() 函数,还有一种是利用 ** 的用法自定义合并。

    • (a) 使用 update() 方法

      使用内置函数,将新的字典合并到当前字典中。函数参数为希望添加到指定字典dict里的字典。该方法没有任何返回值。

      合并过程可能出现以下两种情况:
      (1)有相同的键时:会使用最新的字典中该键对应的值做为最终结果。
      (2)有新的键时:会直接把字典中的键值对加入到当前字典中。

      dict_1 = {'Id001': 1, 'Id002': 2}
      dict_2 = {'Id001': 0, 'Id003': 3, 'Id004': 4}
      
      dict_2.update(dict_1)
      
      print(dict_2)
      

      输出结果如下:{'Id001': 1, 'Id003': 3, 'Id004': 4, 'Id002': 2}

    • (b) 使用 **,函数将参数以字典的形式导入

      def Merge(src_dict_1, src_dict_2):
      	dst_dict = {**src_dict_1, **src_dict_2}
      	return dst_dict
      
      dict_1 = {'Id001': 1, 'Id002': 2}
      dict_2 = {'Id003': 3, 'Id004': 4}
      dict_3 = Merge(dict_1, dict_2)
      print(dict3)
      

      输出结果如下:{'Id001': 1, 'Id002': 2, 'Id003': 3, 'Id004': 4}

    2 列表

    2.1 判断元素是否在列表

    可以使用 in 方法,也可以使用内置函数 index()。

    num = [1,2,3,4,5]
    a = 3
    if a in num:
        print("a=%d is in list num" % a)
    
    b_idx = num.index(a)
    print("The index of a=%d in the list is %d" % (a, b_idx))
    

    (全文完)


    本文作者 :phillee
    发表日期 :2021年12月20日
    本文链接https://www.cnblogs.com/phillee/p/15711768.html
    版权声明 :自由转载-非商用-非衍生-保持署名(创意共享3.0许可协议/CC BY-NC-SA 3.0)。转载请注明出处!
    限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。

  • 相关阅读:
    使用SO_REVTIMEO套接字选项为recvfrom设置超时
    使用select为描述符设置超时
    套接字超时设置方法
    使用SIGALARM为recvfrom设置超时
    使用SIGALARM为connect设置超时
    20200410 阿里巴巴Java开发手册
    20200409 Vue 视频学习【归档】
    20200319 Spring MVC 官方文档【归档】
    20200319 Spring Web MVC 2-5
    20200319 Spring Web MVC 1
  • 原文地址:https://www.cnblogs.com/phillee/p/15711768.html
Copyright © 2011-2022 走看看