简短的python列表之前的惯用语法是什么? [英] What's the idiomatic syntax for prepending to a short python list?

查看:97
本文介绍了简短的python列表之前的惯用语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

list.append()是添加到列表末尾的明显选择.这是丢失的list.prepend()合理的解释.假设我的清单很短,而对性能的关注可以忽略不计,则是

list.append() is the obvious choice for adding to the end of a list. Here's a reasonable explanation for the missing list.prepend(). Assuming my list is short and performance concerns are negligible, is

list.insert(0, x)

list[0:0] = [x]

惯用吗?

推荐答案

s.insert(0, x)形式是最常见的形式.

The s.insert(0, x) form is the most common.

不过,只要您看到它,也许就该考虑使用集合了. deque 而不是列表.

Whenever you see it though, it may be time to consider using a collections.deque instead of a list.

这篇关于简短的python列表之前的惯用语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆