This is the 26th day of my participation in Gwen Challenge
There are three ways to concatenate a list
There are three main ways to join lists:
- The simplest way is to use “+”;
- Using the method of slice assignment;
- Use the extend method that comes with the list
Method 1: Simple “+”
>>> list1 = [1,2,3]
>>> list2 = [4,5,6]
>>> id(list1)
4408465160
>>> id(list2)
4408325512
>>> list1 = list1 + list2
>>> list1
[1, 2, 3, 4, 5, 6]
>>> id(list1)
4408504776
Copy the code
This method is easy to understand, but you will notice that list1 is no longer the same as list1. The ID of list1 has changed.
Method two: slice assignment
Here, we find that splicing with slice assignment is executed in-place, that is, it modifies the original list, but is not very legible
> > > list1 = [1, 2, 3] > > > list2 = (4 and 6) > > > id (list1) 4408465160 > > > id (list2) 4408504776 > > > list1[len(list1):len(list1)] = list2 >>> list1 [1, 2, 3, 4, 5, 6] >>> id(list1) 4408465160Copy the code
Method 3: Extend () from the list
> > > list1 = [1, 2, 3] > > > list2 = (4 and 6) > > > id (list1) 4408325512 > > > list1. The extend (list2) > > > list1 [1, 2, 3, 4, 5, 6] >>> id(list1) 4408325512Copy the code
Here, you’ll notice that the list’s own extend() is also executed in-place, which means that it modifies the original list by concatenating the new list directly after the original list.
Combining the above three methods, the first method uses “+” concatenation to be easy to understand, but not in place, but to create a new space for a new list. The second is harder to understand; The extend() concatenated list function that comes with Python lists is more user-friendly.
Three ways to de-weight a list
For example, if you have a list: What can you think of that you need to de-weight?
Num_list =,8,9,1,2,3,4,6,5,7,10,5,6,8,3,4 [1]Copy the code
Take advantage of the properties of set
The first method is to use the property of sets: elements cannot be repeated
> > > num_list,8,9,1,2,3,4,6,5,7,10,5,6,8,3,4 [1] = > > > new_list = list (set (num_list)) > > > new_list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Copy the code
Take advantage of the non-repeatable property of the dictionary key
Dict.fromkeys (a sequence that sets the value of a key) returns a dictionary of all key values from the elements of the sequence. If values are omitted, all keys default to None. If the value is set, all keys will be assigned the same value. See example:
>>> dict1 = dict. dict1 {1: None, 2: None, 3: None, 4: dict1 = dict. dict1 ([1,2,3,4]) None} > > > dict1 = dict. Fromkeys (5) [1, 2, 3, 4], > > > dict1 {1, 5, 2, 5, 3, 5, 4, 5}Copy the code
Use the dictionary key feature to remove weights.
> > > num_list,8,9,1,2,3,4,6,5,7,10,5,6,8,3,4 [1] = > > > num_dict = dict. Fromkeys (num_list) > > > num_dict {1: None, 8: None, 9: None, 2: None, 3: None, 4: None, 6: None, 5: None, 7: None, 10: None} >>> new_list = list(num_dict.keys()) >>> new_list [1, 8, 9, 2, 3, 4, 6, 5, 7, 10]Copy the code
Using index() gets the first occurrence of the index
The third way: use index() to obtain the index that appears for the first time by judging whether the index value that appears for the first time in the list is equal to the cursor value of the traversed counter. If the index value is equal, it is the first time to appear, otherwise, it is repeated. See the examples:
> > > num_list,8,9,1,2,3,4,6,5,7,10,5,6,8,3,4 [1] = > > > new_list = [] > > > cursor = 0 > > > while cursor < len (num_list) :... if num_list.index(num_list[cursor]) == cursor: ... new_list.append(num_list[cursor]) ... cursor += 1 ... >>> new_list [1, 8, 9, 2, 3, 4, 6, 5, 7, 10]Copy the code