Python Lists(Slice method)

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

I am a newbie to python,everywhere I read about list methods I see one thing

The slice method returns a "new" list

What is here meant by "new" list,and why is it faster then changing the original list?

Does it really matter if python manipulates the original list,I mean I cant use it anyway.

回答1:

I hope that this helps explain what it means by making a new list:

>>> lista = [1, 2, 3, 4]          >>> listb = lista >>> print lista [1, 2, 3, 4] >>> print listb [1, 2, 3, 4] >>> lista[0] = 3 >>> print listb [3, 2, 3, 4] >>> listc = lista[:] >>> print listc [3, 2, 3, 4] >>> lista[0] = 1 >>> print listc [3, 2, 3, 4] 

When doing listb = lista you are not making a new list, you are making an additional reference to the same list. This is shown by changing the first element in lista with lista[0] = 3, this also changes the first element in listb. However, when slicing lista into listc with listc = lista[:] you are copying over the values. When changing the first element of lista back to 1 with lista[0] = 1, the first element of listc is still 3.

For speed I would expect slicing to be slower but this should not be a consideration for which one to use. As I've shown they both have a very different implication and it depends on what you are going to do with the list, rather than on speed (this is in general. There are occasion where the speed might be important).



回答2:

With lists, you can do both:

1) create a new list (the original is left intact):

In [1]: l = [1, 2, 3, 4, 5]  In [2]: l[:3] Out[2]: [1, 2, 3]  In [3]: l Out[3]: [1, 2, 3, 4, 5] 

2) modify the list in-place:

In [6]: del l[3:]  In [7]: l Out[7]: [1, 2, 3]  In [8]: l.append(15)  In [9]: l Out[9]: [1, 2, 3, 15] 

It's up to you to choose which way makes more sense for your problem.

In contrast to lists, tuples are immutable, which means that you can slice them, but you cannot modify them in place.



回答3:

  1. "new" means a shallow copy of the portion of the list you sliced.

  2. It depends on what you are trying to do. For your particular implementation, you might not care about the original data, but I'm sure you could come up with scenarios where you want to work with a subset of data without modifying the originals (though do keep in mind it is only a shallow copy, so there are many instances where you will be modifying the data in the original and slice when working on a slice). Also, it's not faster; in fact, it's actually slower, since the system needs to allocate memory and construct new objects. The gain is not speed, but functionality.



回答4:

When a function/method create a new list, it means that your script has to consume the double amount of memory and has a little (or not so little) overhead while creating a duplicate of the old list.

If the list is really big, the performance of your script can drop very fast. That's why changing lists in-place is preferred when you have large amounts of data.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!