加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

有没有办法将列表中项目的“其余”分配给python中多个赋值中的最

发布时间:2020-12-20 11:27:33 所属栏目:Python 来源:网络整理
导读:参见英文答案 Pythonic way to split a list into first and rest?????????????????????????????????????3个 ???????????? python head and tail in one line????????????????????????????????????5个 在ruby中我可以做类似的事情: key,*rest = ["key1",1,2
参见英文答案 > Pythonic way to split a list into first and rest?????????????????????????????????????3个
>???????????? python head and tail in one line????????????????????????????????????5个
在ruby中我可以做类似的事情:

key,*rest = ["key1",1,2,3]

结果将是:

key = "key1"
rest = [1,3]

有没有办法在python中做同样的事情?

解决方法

key,rest = my_list[0],my_list[1:]

尽可能接近我的想法(在Python< = 2.7中).在Python 3中,您的代码按原样工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读