使用reverse来让列表反转特别方便, 没事自己写了几种方式 In [59]: def reverse(nums): length = len(nums) for i in range(length-1): nums.insert(length-i-1, nums.pop(0)) print nums ....: In [60]: In [60]: In [60]: a = range(5) In [61]: reverse(a) [1, 2, 3, 4, 0] [2, 3, 4, 1, 0]
Python的列表就像是一个数组: 一.创建列表 movies=["The Holy Grail","Then Life of Brian","The Meaning of Life"] 这里的movies是一个变量,而且不需要声明变量的类型. 数组是从0开始计数的.如果要访问列表里的数据,可以这样: ['The Holy Grail', 'Then Life of Brian', 'The Meaning of Life'] >>&
python基础 生成式 列表生成式 格式 [表达式 for 表达式 in 迭代对象 (可加判断)] 原: res1 = [] for i in range(1,5): res1.append(i) print(res1) 改: res2 = [i for i in range(1,5)] print(res2) 字典生成式 格式 {key:value for 表达式 in 迭代对象 (可加判断)} a = "adasdsasad" b = "asdasdasdg"
Python实现比较两个列表(list)范围 有一道题: 比较两个列表范围,如果包含的话,返回TRUE,否则FALSE. 详细题目如下: Create a function, this function receives two lists as parameters, each list indicates a scope of numbers, the function judges whether list2 is included in list1. Function signature
eval()的使用 n = ["2.3","2.56"] m = [] for i in n: k = eval(i) #只是去了最外层的双引号,单引号, 规定是数字,字母类型 m.append(k) print(m) #输出结果为 [2.3, 2.56] list列表的基本函数 b = [12,23,45,67,23,12,34,56] b.insert(0,99) #在第几个位置插入元素 print(b) b.pop() #总是删除最后一个元素,获取删除的值 p