Common ways to share lists of Python operations

  • 2020-04-02 13:23:36
  • OfStack

The following is a list of commonly used methods and small examples:

1.   Append
        To add an element at the end of the list, add an element at the end of the list.
        The parameters added in a. append are as A whole


 >>> name = list("scott")
 >>> name
 ['s', 'c', 'o', 't', 't']
 >>> name.append(list(" tiger"))
 >>> name
 ['s', 'c', 'o', 't', 't', [' ', 't', 'i', 'g', 'e', 'r']]

Resulting value not: [' s', 'c', 'o', 't', 't', ', 't', 'I', 'g', 'e', 'r']
If you want this kind of append, you can try sharding (or the extend method, as described below) :


 >>> name = list("scott")
 >>> name
 ['s', 'c', 'o', 't', 't']
 >>> name[len(name):] = list(" tiger")      # Append from the end 
 >>> name
 ['s', 'c', 'o', 't', 't', ' ', 't', 'i', 'g', 'e', 'r']

B.append can only add one element at a time


>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.append("A","B")        # Adding multiple elements is about to report an error 
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: append() takes exactly one argument (2 given)
>>> name.append("A")
>>> name
['s', 'c', 'o', 't', 't', 'A']

2. The Count

Count the number of times an element appears in a list


>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.count('s')
1
>>> name.count("t")
2
>>> name.count("A")
0
>>> name.append(list("Python"))
>>> name
['s', 'c', 'o', 't', 't', ['P', 'y', 't', 'h', 'o', 'n']]
>>> name.count(['P', 'y', 't', 'h', 'o', 'n'])
1

3. The Extend

    Appends multiple values from another sequence to the original list


 >>> name = list("scott")
 >>> name
 ['s', 'c', 'o', 't', 't']
 >>> name.extend(list(" tiger"))
 >>> name
 ['s', 'c', 'o', 't', 't', ' ', 't', 'i', 'g', 'e', 'r']

Of course, we can use shard assignment to achieve:


 >>> name = list("scott")
 >>> name
 ['s', 'c', 'o', 't', 't']
 >>> name[len(name):] = list(" tiger")
 >>> name
 ['s', 'c', 'o', 't', 't', ' ', 't', 'i', 'g', 'e', 'r']

At this point, people will think, we can directly use the operator "+", it is more convenient:


 >>> name = list("scott")
 >>> pwd  = list(" tiger")
 >>> name + pwd
 ['s', 'c', 'o', 't', 't', ' ', 't', 'i', 'g', 'e', 'r']
 >>> name
 ['s', 'c', 'o', 't', 't']

From the output of these three operations, it can be seen that:
Both extend and shard assignments modify the original list, extend is relatively readable, and the operator "+" generates a new list without affecting the original list if
We need to generate a new list without affecting the original list, so we can use the operator "+".

4. The Index
Find the index position of the first (note the first) match of a value in the list


>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.index('t')    ## First letter t The index position of 3
3   
>>> name.index('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: list.index(x): x not in list
>>> 'a' in name
False
>>> 'a' not in name
True

As you can see from the output, index looks for the index position of the first match, and if the element is not in the list, an error is reported (wouldn't it be better to return -1?). Of course if you want to avoid it
No, we can use the in operation first to determine whether an element is in a certain list, if so, and then we can use the index operation.

5. Insert
      Used to insert an object into a list with two arguments, the first being the index position and the second the inserted element object.


 >>> name = list("scott")
 >>> name
 ['s', 'c', 'o', 't', 't']
 >>> name.insert(2,'tiger')     ## In the index for 2 Where the string is inserted tiger  
 >>> name
 ['s', 'c', 'tiger', 'o', 't', 't']

We can also assign values by sharding:


 >>> name = list("scott")
 >>> name
 ['s', 'c', 'o', 't', 't']
 >>> name[2:2] = ['tiger']
 >>> name
 ['s', 'c', 'tiger', 'o', 't', 't']
 >>> name[2:2] = 'tiger'
 >>> name
 ['s', 'c', 't', 'i', 'g', 'e', 'r', 'tiger', 'o', 't', 't']

The important thing to note here is that if you're inserting an element, you need to enclose it with [], otherwise, if you're just using a string, you're inserting a list of strings and adding it after the index position.
Of course, inserts are more readable than slices.

6. Pop
      Removes an element from the list (the last element) and returns the value of that element


>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.pop()
't'
>>> name
['s', 'c', 'o', 't']
>>> name.append("t")
>>> name
['s', 'c', 'o', 't', 't']

Shard assignment to simulate pop:

 >>> name = list("scott")
 >>> name
 ['s', 'c', 'o', 't', 't']
 >>> name[len(name)-1:] = []
 >>> name
 ['s', 'c', 'o', 't']

This mimics a first-in-first-out LIFO of the stack with pop and append.

7. Remove
    Remove the first match of a value in the list: if there are two equal elements, just remove the matched element. If an element is not in a list, an error is reported, and only once
    Remove an element.


>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.remove("t")    # Get rid of the first one t
>>> name
['s', 'c', 'o', 't']
>>> name.remove("A")    # There is no error reporting 
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: list.remove(x): x not in list
>>> "A" not in name
True
>>> name.remove("s","c")  # You can only remove one element at a time 
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: remove() takes exactly one argument (2 given)

8. Revense

    Reverse the elements in the list


 >>> name = list("scott")
 >>> name
 ['s', 'c', 'o', 't', 't']
 >>> name.reverse()
 >>> name
 ['t', 't', 'o', 'c', 's']

9. The Sort & Sorted

      The sort method is used to sort a list, modify the original list, and does not return a sorted copy of the list


 >>> result = [8,5,5,3,9]
 >>> result.sort()
 >>> result
 [3, 5, 5, 8, 9]

If we want to return a sorted copy of the list without affecting the original list, one way we can do this is we can assign the original list first (which can be copied by sharding) and then
The other way to do sort on the replicated list is to use the sorted function, which returns a copy of the sorted list:


 >>> result = [8,5,5,3,9]
 >>> result2 = sorted(result)
 >>> result
 [8, 5, 5, 3, 9]
 >>> result2
 [3, 5, 5, 8, 9]

For custom comparison methods, like javascript sort passes in the compare function, Java passes in Comparable < T > Example, Python is similar, let's leave it to the next ~(@^_^@)~.


Related articles: