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]]
posted @ 2020-06-18 14:42  逐梦无惧_数据分析  阅读(137)  评论(0编辑  收藏  举报