Python列表方法(Python list method)

lst = [2,3,4]
lst.append(10)
print(lst)

  输出结果:[2, 3, 4, 10]

  你可能心存疑虑,为何给列表取lst 这样糟糕的名字,不称之为list 呢?我原本是可以这样做的,但你可能还记得,list 是一个内置函数2 ,如果我将前述列表命名为list ,就无法调用这个函数。在特定的应用程序中,通常可给列表选择更好的名称。诸如lst 等名称确实不能提供任何信息。因此,如果列表为价格列表,可能应该将其命名为prices 、prices_of_eggs 或pricesOfEggs 。

lst = [2,3,4]
lst.clear()
print(lst)

  输出结果:[ ]

  这类似于切赋值语句lst[:] = [ ] 。

a = [2,3,4]
b = a
b[1] = 6
print(a)

  输入结果:[2, 6, 4]

  要让a 和b 指向不同的列表,就必须将b 关联到a 的副本。

a = [2,3,4]
b = a.copy()
b[1] = 6
print(a)

  输入结果:[2, 3, 4]

  这类似于使用a[:] 或list(a) ,它们也都复制a 。

x = [[1, 2], 1, 1, [2, 1, [1, 2]]]
print(x.count(1))
print(x.count([1,2]))

  输出结果:2, 1

a = [2,3,4]
b = [5,6,7]
a.extend(b)
print(a)

  输出结果:[2, 3, 4, 5, 6, 7]

  这可能看起来类似于拼接,但存在一个重要差别,那就是将修改被扩展的序列(这里是a )。在常规拼接中,情况是返回一个全新的序列。

a = [2,3,4]
b = [5,6,7]
print(a + b)
print(a)

  输入结果:[2, 3, 4, 5, 6, 7],[2, 3, 4]

  如你所见,拼接出来的列表与前一个示例扩展得到的列表完全相同,但在这里a 并没有被修改。鉴于常规拼接必须使用a 和b 的副本创建一个新列表,因此如果你要获得类似于下的效果,拼接的效率将extend 低:

  另外,拼接操作并非就地执的,即它不会修改原来的列表。要获得与extend 相同的效果,可将列表赋给切,如下所示:

a = [2,3,4]
b = [5,6,7]
a[len(a):] = b
print(a)

  输入结果:[2, 3, 4, 5, 6, 7]

  这虽然可,但可读性不是很。

————————
lst = [2,3,4]
lst.append(10)
print(lst)

Output result: [2, 3, 4, 10]

You may wonder why you give the list such a bad name as LST instead of calling it list? I could have done this, but you may remember that list is a built-in function 2. If I name the above list list list, I can’t call this function. In a particular application, you can usually choose a better name for the list. Names such as LST do not provide any information. Therefore, if the list is a price list, it should probably be named prices, prices_ of_ Eggs or pricesofggs.

lst = [2,3,4]
lst.clear()
print(lst)

Output result: []

This is similar to the cut assignment statement LST [:] = [].

a = [2,3,4]
b = a
b[1] = 6
print(a)

Input result: [2, 6, 4]

For a and B to point to different lists, you must associate B with a copy of A.

a = [2,3,4]
b = a.copy()
b[1] = 6
print(a)

Input result: [2, 3, 4]

This is similar to using a [:] or list (a), both of which copy a.

x = [[1, 2], 1, 1, [2, 1, [1, 2]]]
print(x.count(1))
print(x.count([1,2]))

Output result: 2, 1

a = [2,3,4]
b = [5,6,7]
a.extend(b)
print(a)

Output result: [2, 3, 4, 5, 6, 7]

This may look like splicing, but one important difference is that the extended sequence (here a) will be modified. In normal splicing, the situation is to return a new sequence.

a = [2,3,4]
b = [5,6,7]
print(a + b)
print(a)

Input result: [2, 3, 4, 5, 6, 7], [2, 3, 4]

As you can see, the spliced list is exactly the same as the list expanded in the previous example, but a has not been modified here. Since regular splicing must create a new list with copies of a and B, if you want to obtain an effect similar to the following, the splicing efficiency will be low:

In addition, the splicing operation is not performed in place, that is, it does not modify the original list. To achieve the same effect as extend, you can assign the list to tangent, as shown below:

a = [2,3,4]
b = [5,6,7]
a[len(a):] = b
print(a)

Input result: [2, 3, 4, 5, 6, 7]

Although this is, it is not very readable.