参见英文答案 >
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, 2, 3]
有没有办法在python中做同样的事情?
最佳答案
key,rest = my_list[0],my_list[1:]
尽可能接近我的想法(在Python< = 2.7中).在Python 3中,您的代码按原样工作.