zoukankan      html  css  js  c++  java
  • 父与子的编程之旅4——列表与字典

    1、可以使用append()、insert()或extend()向列表增加元素。

    2、可以使用remove()、pop()或del()从列表删除元素。

    3、要得到一个有序副本,可以采用下面任意一种做法:

      (1)建立列表的副本,使用切片(分片):new_list = my_list[:],

        然后对新列表排序:new_list.sort()

      (2)使用sorted函数:new_list = sorted(my_list)

    4、使用in关键字可以得出一个特定值是否在一个列表中。

    5、使用index()方法可以得出一个值在列表中的位置。

    6、元祖是一个与列表类似的集合,只不过元祖不能改变。

    7、可以使用多种方法建立一个双重列表:

      (1)使用嵌套的中括号:

        my_list = [[1, 2, 3], ['a', 'b', 'c'], ['red', 'green', 'blue']]

      (2)使用append(),并追加一个列表

        my_list = []

        my_list.append([1, 2, 3])

        my_list.append(['a', 'b', 'c'])

        my_list.append(['red', 'green', 'blue'])

        print(my_list)

      (3)建立单个列表,再合并这些列表

        list1 = [1, 2, 3]

        list2 = ['a', 'b', 'c']

        list3 = ['red', 'green', 'blue']

        my_list = [list1, list2, list3]

    8、字典是键值对的集合

    9、可以通过指定键和值的方式在字典中添加条目:

      phone_numbers['John'] = '555-1234'

    10、要通过键在字典中查找一个条目,可以使用索引:

      print phone_numbers['John']

    来到这里的都是程序猿,所有资料自取~
  • 相关阅读:
    Leetcode 第 210 场周赛
    Leetcode 834. 树中距离之和
    Leetcode 第36场双周赛
    力扣 第 208 场周赛
    cf 665 DMaximum Distributed Tree
    Codeforces Round #672 (Div. 2) A~D
    Educational Codeforces Round 95 (Rated for Div. 2) A~D
    CCF CSP 201612-3 权限查询
    Codeforces Round #669 (Div. 2) A~C
    201703-4 地铁修建
  • 原文地址:https://www.cnblogs.com/ifan-he/p/9939095.html
Copyright © 2011-2022 走看看