常见的python插入元素方式一般是insert或者手动挪动后修改值,然后其实还有种比较少用的通过零宽切片插入元素的方式,比较有意思,随便记录下.
Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> a = [1,2,3,4,5] >>> a[:0] = [0,0,0] >>> a [0, 0, 0, 1, 2, 3, 4, 5] >>> a[4:4] = [8,8,8,8,8,8] >>> a [0, 0, 0, 1, 8, 8, 8, 8, 8, 8, 2, 3, 4, 5] >>> a[4:8] = [] >>> a [0, 0, 0, 1, 8, 8, 2, 3, 4, 5]
无正文.