python insert()函数解析(最清晰的解释)

欢迎关注WX公众号:【程序员管小亮】

python insert()函数用于将指定对象插入列表的指定位置。

list.insert(index, obj)

参数:

  • index:对象obj需要插入的索引位置。

  • obj:要插入列表中的对象。

共有如下5种场景:

  • 1:index=0时,从头部插入obj。

  • 2:index > 0 且 index < len(list)时,在index的位置插入obj。

  • 3:当index < 0 且 abs(index) < len(list)时,从中间插入obj,如:-1 表示从倒数第1位插入obj。

  • 4:当index < 0 且 abs(index) >= len(list)时,从头部插入obj。

  • 5:当index >= len(list)时,从尾部插入obj。

list.insert(index = -1, obj)除外,当index = -1时,是插在倒数第二位的,也就是:

lst = [2,2,2,2,2,2]
lst.insert(-1,6)
print(lst)
> [2, 2, 2, 2, 2, 6, 2]
例子1:
lst = [2,2,2,2,2,2]
lst.insert(0,0)# index=0时,从头部插入obj
print(lst)
> [0, 2, 2, 2, 2, 2, 2]
例子2:
lst = [2,2,2,2,2,2]
lst.insert(6,7)# index > 0 且 index < len(list)时,在index的位置插入obj
print(lst)
> [2, 2, 2, 2, 2, 2, 7]
例子3:
lst = [2,2,2,2,2,2]
lst.insert(-2,6)# 当index < 0 且 abs(index) < len(list)时,从中间插入obj
print(lst)
> [2, 2, 2, 2, 6, 2, 2]
例子4:
lst = [2,2,2,2,2,2]
lst.insert(-20,10)# 当index < 0 且 abs(index) >= len(list)时,从头部插入obj
print(lst)
> [10, 2, 2, 2, 2, 2, 2]
例子5:
lst = [2,2,2,2,2,2]
lst.insert(30,20)# 当index >= len(list)时,从尾部插入obj
print(lst)
> [2, 2, 2, 2, 2, 2, 20]

python课程推荐。
在这里插入图片描述

  • 54
    点赞
  • 228
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
引用:mean()函数是一个numpy库中的函数,用于计算给定数组的均值。它的功能是求取均值,可以根据指定的参数axis来决定是对行还是列进行求均值操作。通常,axis参数的取值为0表示对列进行求均值操作,取值为1表示对行进行求均值操作。 在numpy中,mean()函数的使用方式是np.mean(array, axis),其中array是要进行求均值操作的数组,axis是指定进行求均值操作的轴。例如,np.mean(array, 0)表示对array数组的各列求均值。 因此,当你使用python的mean()函数时,可以根据需要选择对行或列进行均值计算,并将相应的参数传入函数中。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python函数综合案例.md](https://download.csdn.net/download/qq_42431718/88241199)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python中mean函数解析](https://blog.csdn.net/qq_40730862/article/details/83045017)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是管小亮

一口吃掉你的打赏,嗝~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值