简体   繁体   English

Python:如何使用切片扩展列表列表?

[英]Python: How to Extend List with List using Slicing?

Note: python Extend will extend your given list with existing list at the end. 注意:python Extend将在最后使用现有列表扩展您的给定列表。

>>>a = [1,2,3]
>>>a.extend([4,5])
>>>[1,2,3,4,5]

How to do the same using slicing? 如何使用切片做同样的事情?

at 0th position, middle or somewhere else it works as expected, 在第0个位置,中间或其他地方,它按预期工作,

>>>l = [2,4,6,8,10]
>>>l[:0] = [0, 1]
>>>l
>>>[0,1,2,4,6,8,10]

Even i have tried, 即使我尝试过,

>>> l[len(l):] = [100]
>>> l
[2, 4, 6, 8, 10, 100]
>>> 

Is there better way without using function like len() ? 没有使用像len()这样的函数有更好的方法吗?

but at the end its insert before last value. 但最后它在最后一个值之前插入。

>>> l[-1:-1] = [100]
>>> l
[2, 4, 6, 8, 100, 10]

expected output, 预期产量,

>>>[2, 4, 6, 8, 10, 100]

I know using extend() method but i want to achieve using slicing at the end.. 我知道使用extend()方法,但我想在最后使用切片实现..

这是有效的,虽然它不是那样的:

l[len(l):] = [100]

In short, not exactly, because of how slices work (emphasis mine): 简而言之,不完全是因为切片如何工作 (强调我的):

The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j . ij的s的片被定义为具有索引k的项的序列,使得i <= k < j If i or j is greater than len(s) , use len(s) . 如果ij大于len(s) ,则使用len(s) If i is omitted or None , use 0 . 如果省略iNone ,则使用0 If j is omitted or None , use len(s) . 如果省略j或None ,则使用len(s) If i is greater than or equal to j , the slice is empty. 如果i大于或等于j ,则切片为空。

Nevertheless, note that lists actually have a maximum size, which we can find with sys.maxsize . 不过,请注意, lists实际上具有最大大小,我们可以使用sys.maxsize找到sys.maxsize If we use that as the start of the slice , in conjunction with the emphasised part above, we can actually do something that will work for all lists : 如果我们使用它作为slice的开头,结合上面强调的部分,我们实际上可以做一些适用于所有lists东西:

import sys

END = sys.MAXSIZE

a = [1, 2, 3]
b = [4, 5]

a[END:] = b
print(a)

Output: 输出:

[1, 2, 3, 4, 5]

Of course, practically speaking, just use len(a) . 当然,实际上,只需使用len(a)

Just use += which isn't using slicing, but is one of the ways without using an inbuilt function like len 只使用不使用切片的+= ,但是不使用像len这样的内置函数的方法之一

In [1]: a = [1,2,3]                                                                                                                                                               

In [2]: a += [4,5]                                                                                                                                                                

In [3]: a                                                                                                                                                                         
Out[3]: [1, 2, 3, 4, 5]

To use slicing, the python docs already provide a solution, also noted in Amadan's answer . 为了使用切片,python文档已经提供了一个解决方案,在Amadan的回答中也有提到。 https://docs.python.org/3/tutorial/datastructures.html https://docs.python.org/3/tutorial/datastructures.html

list.extend(iterable) . list.extend(iterable)。
Extend the list by appending all the items from the iterable. 通过附加iterable中的所有项来扩展列表。 Equivalent to a[len(a):] = iterable. 相当于[len(a):] = iterable。

As per document shared by @Devesh, 根据@Devesh分享的文件,

Data Strutures it extend your list we need length of list. Data Strutures它扩展了你的列表,我们需要列表的长度。

so we can use built in function len() to do it in simple way. 所以我们可以使用内置函数len()以简单的方式完成它。

otherwise we can use sys.maxsize 否则我们可以使用sys.maxsize

>>> l = [2,4,6,8,10]
>>> import sys
>>> l[sys.maxsize:] = [100, 200]
>>> l
[2, 4, 6, 8, 10, 100, 200]
>>> 

One more Solution, 还有一个解决方案

Seems little better, 似乎好一点,

>>> l = [2,4,6,8,10]
>>> l[-1:] = l[-1:]+[100, 200, 300]
>>> l
[2, 4, 6, 8, 10, 100, 200, 300]
>>> 

this is another method i have tried using [].reverse() method. 这是我尝试使用[].reverse()方法的另一种方法。

>>> l = [2,4,6,8,10]
>>> l.reverse()
>>> l[:0] = [3,5,7][::-1]
>>> l
[7, 5, 3, 10, 8, 6, 4, 2]
>>> l.reverse()
>>> l
[2, 4, 6, 8, 10, 3, 5, 7]
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM