淘宝网站开发类目没法上架商品平台网站建设所需资质
news/
2025/9/23 20:27:39/
文章来源:
淘宝网站开发类目没法上架商品,平台网站建设所需资质,在线优化seo,各大网站搜索引擎入口1.3 List(列表)列表由一系列按特定顺序排列的元素组成。在Python中#xff0c;用方括号[ ]来表示列表#xff0c;并用逗号来分隔其中的元素。1.3.1 访问列表元素在Python中#xff0c;第一个列表元素的索引为0#xff0c;而不是1。 bicycles [trek, cannondal…1.3 List(列表)列表由一系列按特定顺序排列的元素组成。在Python中用方括号[ ]来表示列表并用逗号来分隔其中的元素。1.3.1 访问列表元素在Python中第一个列表元素的索引为0而不是1。 bicycles [trek, cannondale, redline, specialized] print(bicycles[0])trek print(bicycles[3])specialized print(bicycles[-1])specialized注意通过将索引指定为-1可让Python返回最后一个列表元素。索引-2返回倒数第二个列表元素索引-3返回倒数第三个列表元素以此类推。1.3.2 添加元素 motorcycles [honda, yamaha, suzuki] print(motorcycles)[honda, yamaha, suzuki] motorcycles.append(ducati) print(motorcycles)[honda, yamaha, suzuki, ducati]1.3.3 修改元素 motorcycles [honda, yamaha, suzuki] print(motorcycles)[honda, yamaha, suzuki] motorcycles[0] ducati print(motorcycles)[ducati, yamaha, suzuki]1.3.4 在列表中插入元素 motorcycles [honda, yamaha, suzuki] motorcycles.insert(1, ducati) print(motorcycles)[honda, ducati, yamaha, suzuki]1.3.5 删除元素 motorcycles [honda, yamaha, suzuki] print(motorcycles)[honda, yamaha, suzuki] del motorcycles[1] print(motorcycles)[honda, suzuki]弹出列表元素 motorcycles [honda, yamaha, suzuki] popped_motorcycle motorcycles.pop() print(popped_motorcycle)suzuki print(motorcycles)[honda, yamaha] print(motorcycles.pop(0))honda print(motorcycles)[yamaha]有时候你不知道要从列表中删除的值所处的位置。如果你只知道要删除的元素的值可使用方法remove()。 motorcycles [honda, yamaha, suzuki] print(motorcycles)[honda, yamaha, suzuki] motorcycles.remove(yamaha) print(motorcycles)[honda, suzuki]1.3.6 对列表排序 cars [bmw, audi, toyota, subaru] cars.sort() print(cars)[audi, bmw, subaru, toyota] print(len(cars))41.3.7 遍历列表 magicians [alice, david, carolina] for magician in magicians:print(magician)alicedavidcarolina1.3.8 创建数值列表 for value in range(1,5):print(value)1234 numbers list(range(1,6)) print(numbers)[1, 2, 3, 4, 5] even_numbers list(range(2,11,2)) print(even_numbers)[2, 4, 6, 8, 10]对数字列表执行简单的统计计算 digits [1,2,3,4,5,6,7,8,9,0] min(digits)0 max(digits)9 sum(digits)451.3.9 列表生成式(List Comprehensions) squares [value**2 for value in range(1,11)] print(squares)[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] [x * x for x in range(1, 11) if x % 2 0][4, 16, 36, 64, 100] [m n for m in ABC for n in XYZ][AX, AY, AZ, BX, BY, BZ, CX, CY, CZ] d {x: A, y: B, z: C } [k v for k, v in d.items()][xA, yB, zC] L [Hello, World, IBM, Apple] [s.lower() for s in L][hello, world, ibm, apple]1.3.10 使用列表的一部分你可以处理列表的部分元素Python称之为切片。要创建切片可指定要使用的第一个元素和最后一个元素的索引。 players [charles, martina, michael, florence, eli] print(players[0:3])[charles, martina, michael] print(players[:4]) # 如果你没有指定第一个索引Python将自动从列表开头开始[charles, martina, michael, florence] print(players[2:]) # 要让切片终止于列表末尾也可使用类似的语法[michael, florence, eli] print(players[-3:]) # 负数索引返回离列表末尾相应距离的元素[michael, florence, eli]复制列表my_foods [pizza, falafel, carrot cake]friend_foods my_foods[:] # 使用[:]来复制列表my_foods.append(cannoli)friend_foods.append(ice cream)print(My favorite foods are:)print(my_foods)print(\nMy friends favorite foods are:)print(friend_foods)# My favorite foods are:# [pizza, falafel, carrot cake, cannoli]## My friends favorite foods are:# [pizza, falafel, carrot cake, ice cream]1.4 Tuple(元组)Python将不能修改的值称为不可变的而不可变的列表称为元组。元组看起来犹如列表但使用圆括号而不是方括号来标识。元组中只包含一个元素时需要在元素后面添加逗号否则括号会被当作运算符使用。 tup1 (50) type(tup1) tup1 (50,) type(tup1)参考资料
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/913794.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!