Python implements the nested list de duplication method example

  • 2020-06-23 00:42:56
  • OfStack

Found the problem

Most of you are familiar with python nested lists, but recently I encountered a problem. This is a pit you encounter at work. Let's look at the first problem


raw_list = [[" baidu ", "CPY"], [" jingdong ", "CPY"], [" Huang Xuan ", "PN"], [" baidu ", "CPY"]]

Nested list list, and have a repeated list [" baidu ", "CPY"], now requires the repeating element for heavy (repetition is nested within the list of the two elements are the same), and ensure that element order constant, the output or nested list, the final result should look like this: [[" baidu ", "CPY"], [" jingdong ", "CPY"], [" Huang Xuan ", "PN"]]

Normal Python deweighting is using set, so I also use this idea to deal with 1


In [8]: new_list = [list(t) for t in set(tuple(_) for _ in raw_list)]
In [9]: new_list
Out[9]: [[' jingdong ', 'CPY'], [' baidu ', 'CPY'], [' Huang Xuan ', 'PN']]

=. = When you think you're done, it turns out the nested list order has changed

All right, one step at a time, one step at a time, which way did the order change


In [10]: s = set(tuple(_) for _ in raw_list)
In [11]: s
Out[11]: {(' jingdong ', 'CPY'), (' baidu ', 'CPY'), (' Huang Xuan ', 'PN')}

See two key words about set: disorder and non-repetition =. =

Therefore, there is no hope to solve the sorting problem from set. However, I haven't given up yet. Now the problem becomes how to sort new_list according to the elements of raw_list

Turn to the Python document below to find the following paragraph

The document address


sort(*, key=None, reverse=False)

This method sorts the list in place, using only < comparisons between 
items. Exceptions are not suppressed - if any comparison operations  
fail, the entire sort operation will fail (and the list will likely be left in a 
 partially modified state).

 [`sort()`](https://docs.python.org/3/library/stdtypes.html?highlight=sort#list.sort "list.sort") 

accepts two arguments that can only be passed by keyword ( [keyword-only arguments](https://docs.python.org/3/glossary.html#keyword-only-parameter) ):

key specifies a function of one argument that is used to extract a 
comparison key from each list element (for example, key=str.lower). 
 The key corresponding to each item in the list is calculated once and  then used for the entire sorting process. The default value of None 
means that list items are sorted directly without calculating a separate
 key value.

Start to highlight:

The sort method specifies 1 method via the parameter key, in other words, the value of the key parameter is a function.

This function and each element on new_list produce a result, and sort sorts by this result.

So the idea here is to figure out the index of every element in new_list in raw_list and sort by that index.

The code implementation is as follows:


In [13]: new_list.sort(key=raw_list.index)
In [14]: new_list
Out[14]: [[' baidu ', 'CPY'], [' jingdong ', 'CPY'], [' Huang Xuan ', 'PN']]

The result and the expectation are equal to 1. =

conclusion


Related articles: