python中append、extend、和insert的区别
程序员文章站
2023-12-29 14:29:22
a_list = [x for x in range(1, 11)] print(a_list) a_list.append('sdadfewf') # 将整个字符串放到列表的最后 print(a_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'sdadfewf']... ......
a_list = [x for x in range(1, 11)] print(a_list) a_list.append('sdadfewf') # 将整个字符串放到列表的最后 print(a_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'sdadfewf'] b_list = [x for x in range(1, 11)] print(b_list) b_list.extend('sdadfewf') # 将字符串中的每个元素放到列表的最后 print(b_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 's', 'd', 'a', 'd', 'f', 'e', 'w', 'f'] c_list = [x for x in range(1, 11)] print(c_list) c_list.insert(3, 'sdadfewf') # 将整个字符串插入到列表索引为3的位置 print(c_list) # [1, 2, 3, 'sdadfewf', 4, 5, 6, 7, 8, 9, 10]