zoukankan      html  css  js  c++  java
  • python基础__列表

    a_list = [1,2,3]
    another_list = ['printer',5,['start','circle','9']]
    a_new_list = a_list[:]
    print("{}".format(a_new_list))
    a = 2 in a_list
    print("{}".format(a))
    if a in a_list:
    print("{}".format(a))
    a_list.append(4)         --向列表未尾追加一个新元素
    a_list.append(5)
    print("{}".format(a_list))
    a_list.remove(5)       --从列表中删除一个特定元素
    print("{}".format(a_list))
    a_list.pop()              --从列表未尾删除一个元素
    print("{}".format(a_list))
    a_list.reverse         --原地反转一个列表会修改原列表
    print("{}".format(a_list))
    a_list.reverse()
    print("{}".format(a_list))

    [1, 2, 3]
    True
    True
    [1, 2, 3, 4, 5]
    [1, 2, 3, 4]
    [1, 2, 3]
    [1, 2, 3]
    [3, 2, 1]

    from operator import itemgetter   ---可以使用多个关键字对列表、元素和字典进行排序。
    unordered_list = [3,5,1,7,2,8,4,9,0,6]
    print("{}".format(unordered_list))
    list_copy = unordered_list[:]
    list_copy.sort()    --对列表进行原地排序会修改原列表
    print("{}".format(list_copy))
    my_lists = [[1,2,3,4],[4,3,2,1],[2,4,1,3]]
    my_lists_sorted_by_index_3 = sorted(my_lists,key=lambda a:a[3]) --对一个列表集合按照列表中某个位置的元素进行排序。
    print(("{}").format(my_lists_sorted_by_index_3))
    my_li = [[123,2,2,444],[22,6,6,444],[354,4,4,678],[236,5,5,678],[578,1,1,290],[461,1,1,290]]
    my_lists = [[1,2,3,4],[4,3,2,1],[2,4,1,3]]
    my_li_sorted_by_index = sorted(my_lists,key=itemgetter(3,0)) ---
    print("{}".format(my_li_sorted_by_index))

    [3, 5, 1, 7, 2, 8, 4, 9, 0, 6]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [[4, 3, 2, 1], [2, 4, 1, 3], [1, 2, 3, 4]]
    [[4, 3, 2, 1], [2, 4, 1, 3], [1, 2, 3, 4]]
  • 相关阅读:
    springboot文件上传: 单个文件上传 和 多个文件上传
    Eclipse:很不错的插件-devStyle,将你的eclipse变成idea风格
    springboot项目搭建:结构和入门程序
    POJ 3169 Layout 差分约束系统
    POJ 3723 Conscription 最小生成树
    POJ 3255 Roadblocks 次短路
    UVA 11367 Full Tank? 最短路
    UVA 10269 Adventure of Super Mario 最短路
    UVA 10603 Fill 最短路
    POJ 2431 Expedition 优先队列
  • 原文地址:https://www.cnblogs.com/wei23/p/13156587.html
Copyright © 2011-2022 走看看