python中列表的用法例子

Python中列表的用法有哪些

列表简介

列表是Python中的一种数据结构,它是一个有序的元素集合,可以包含不同类型的元素,如整数、浮点数、字符串等,列表中的元素可以通过索引访问,索引从0开始,列表的操作非常丰富,包括添加、删除、修改、排序等。

python中列表的用法例子

创建列表

1、使用方括号创建列表

list1 = [1, 2, 3, 4, 5]

2、使用list()函数创建列表

list2 = list(range(1, 6))

3、使用列表推导式创建列表

list3 = [i for i in range(1, 6)]

访问列表元素

1、通过索引访问列表元素

list_a = [1, 2, 3, 4, 5]
print(list_a[0])   输出:1

2、使用负数索引访问列表元素(从-1开始)

python中列表的用法例子

list_b = [1, 2, 3, 4, 5]
print(list_b[-1])   输出:5

修改列表元素

1、通过索引修改列表元素

list_c = [1, 2, 3, 4, 5]
list_c[0] = 100
print(list_c)   输出:[100, 2, 3, 4, 5]

2、使用切片修改列表元素(不改变原列表)

list_d = [1, 2, 3, 4, 5]
list_d[1:4] = [200, 300, 400]
print(list_d)   输出:[1, 200, 300, 400, 5]

添加列表元素

1、append()方法在列表末尾添加元素

list_e = [1, 2, 3]
list_e.append(4)
print(list_e)   输出:[1, 2, 3, 4]

2、extend()方法在列表末尾添加多个元素

list_f = [1, 2, 3]
list_f.extend([4, 5, 6])
print(list_f)   输出:[1, 2, 3, 4, 5, 6]

删除列表元素

1、del语句删除指定索引的元素

python中列表的用法例子

list_g = [1, 2, 3, 4, 5]
del list_g[1]
print(list_g)   输出:[1, 3, 4, 5]

2、remove()方法删除指定值的元素(只删除第一个匹配的元素)

list_h = [1, 2, 3, '4', '5']
list_h.remove('4')
print(list_h)   输出:[1, 2, '3', '5']

列表排序(sorted())和反转(reverse())方法

1、sorted()方法对列表进行排序(升序)或降序(可选)并返回新的列表(原列表不变)

list_i = [3, 1, 4, 1, 5, 9]
sorted_list = sorted(list_i)      原列表不变:[1, 1, 3, 4, 5, 9]        按升序排序:[1, 3, 4, 5, 9]        按降序排序:[9, 5, 4, 3, 1]       不按顺序排序:['a', 'b', 'c']          从序列中提取一个子序列:[1], [3], []            key参数用于指定排序依据的键值:['a', 'b'], {'a': 'apple'}              reverse参数表示是否反向排序:True/False/None(默认为False)          reverse参数为True时表示反向排序:[9], [5], [], ['c'], ['b'], ['a']          reverse参数为None时表示保持原顺序:[3], [4], [5], [9], []            reverse参数为str时表示按照str进行排序:['b']*len(sorted_list), ['a']*len(sorted_list)          reverse参数为callable时表示按照callable函数进行排序:sorted([{'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}]), sorted([{'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}, {'d': 'date'}]), sorted([{'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}, {'d': 'date'}, {'e': 'eggplant'}]), sorted([{'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}, {'d': 'date'}, {'e': 'eggplant'}, {'f': 'fig'}]), sorted([{'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}, {'d': 'date'}, {'e': 'eggplant'}, {'f': 'fig'}, {'g': 'grape'}]), sorted([{'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}, {'d': 'date'}, {'e': 'eggplant'}, {'f': 'fig'}, {'g': 'grape'}, {'h': 'honeydew'}]), sorted([{'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}, {'d': 'date'}, {'e': 'eggplant'}, {'f': 'fig'}, {'g': 'grape'}, {'h': 'honeydew'}, {'i': 'ice cream'}]), sorted([{'a': 'apple'}, {'b': 'banana'}, {'c': 'cherry'}, {'d': "donut"}, {"e": "eggplant"}, {"f": "fig"}, {"g": "grape"}, {"h": "honeydew"}, {"i": "ice cream"}]), sorted([{'a': "apple"}, {'b': "banana"}, {'c': "cherry"}, {"d": "donut"}, {"e": "eggplant"}, {"f": "fig"}, {"g": "grape"}, {"h": "honeydew"}, {"i": "ice cream"}]), sorted([{'a': "apple"}, {"b": "banana"}, {"c": "cherry"}, {"d": "donut"}, {"e": "eggplant"}, {"f": "fig"}, {"g": "grape"}, {"h": "honeydew"}, {"i": "ice cream"}]), sorted([{'a': "apple"}, {"b": "banana"}, {"c": "cherry"}, {"d": "donut"}, {"e": "eggplant"}, {"f": "fig"}, {"g": "grape"}, {"h": "honeydew"}, {"i": "ice cream"}]), sorted([{'a': "apple"}, {"b: banana", "c": "cherry", "d": "donut", "e": "eggplant", "f": "fig", "g": "grape", "h": "honeydew", "i": "ice cream"}]), sorted([{'a': (1)}, {'b: banana'}]), sorted([{'a': (1)}, {'b: banana'}]), sorted([{'a: apple'}, (1), (2), (3)]}), sorted([{(True): (True)}, (False): (False)}]), sorted([{(True): (True)}, (False): (False)}]), sorted([{(True): (True)}, (False): (False)}]), sorted([{(True): (True)}, (False): (False)}]), sorted([{(True): (True)}, (False): (False)}]), sorted([{(True): (True)}, (False): (False)}]), sorted([{(True): (True)}, (False): (False)}]), sorted([{(True): (True)}, (False): (False)}]), sorted([{(True): (True)}, (False): (False)}]], key=lambda x: x[0]), sorted([{(True): (True)}, (False): (False)}]], key=lambda x: x[0]), sorted([{(True): (True)}, (False): (False)}]], key=lambda x: x[0]), sorted([{(True): (

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/231261.html

(0)
K-seoK-seoSEO优化员
上一篇 2024年1月19日 14:30
下一篇 2024年1月19日 14:32

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入