The advanced step of Python sequence operation _python

Source: Internet
Author: User
Tags comparable python list in python

Brief introduction

The Python sequence (sequence) usually refers to an iterative container in which any type of element can be stored. Lists and tuples are the most commonly used sequences of data types, there are six of Python's built-in sequences, except for the two types that have just been said, as well as strings, Unicode strings, buffer pairs, and the last xrange, which are infrequently used. This article explains how to use list derivation, slice naming, list element sorting, and list element grouping. After learning Python's basic list operations, learn these advanced operations, and let us write the code more elegant and concise and pythonic.

List inference Type

When we want to construct a list based on some rules, the first thing to think about is the list derivation. The list derivation simplifies looping, for example, if we want to get all the. py files from a list of original file names, which we would normally do without the listing derivation:

File_list = [' foo.py ', ' bar.txt ', ' spam.py ', ' animal.png ', ' test.py ']
py_list = [] for
file in file_list:
if f Ile.endswith ('. Py '):
py_list.append (file)

print (py_list)
# output
[' foo.py ', ' spam.py ', ' Test.py ']

If you use a list derivation, you can simplify to:

Py_list = [F for f in file_list if F.endswith ('. Py ')]
print (py_list)
# output
[' foo.py ', ' spam.py ', ' Test. Py ']

A list of the derivation of the introduction of a lot of resources online, no longer repeat. Just to emphasize that when you need to construct a list based on a rule, you should first think about whether you can use a concise list derivation to implement that requirement, or go back to the normal way.

Name a slice

A Python list slice is handy to use, but sometimes it can also affect code readability. For example, there is a string:

Record = ' ..... 19.6..........100 ......

19.6 for product price, 100 for product quantity, then calculate the total price as:

But if you write this, it might be a while before we read the code and forget record[10:14] record[24:27] what we cut out. To solve the above problem, you can give the slice a name to enhance readability.

Record = ' ..... 19.6..........100 ... ' price
= slice
count = slice (in)
Total_price = float (Record[price]. "". " ) *int (Record[count])

The parameter format received by Slice is slice(stop) , slice(start, stop[, step]) . If you receive only one parameter, it is equivalent to slicing syntax [:stop] , and if you receive two parameters, it is equivalent to slicing syntax [start:stop] , and if you receive three parameters, it is equivalent to slicing syntax [start:stop:step] .

Sort

Sort-related tasks are usually completed by the built-in function sorted. The elements that need to be sorted are generally stored in a list container, and the list can hold any type of element, and the key keyword of the sorted function allows us to easily specify the keyword ordering of the elements, making the order unusually simple. Here are a few common sorting examples to illustrate how key keywords are used. Note that the Python3 and Python2 sorting methods are not generic, and the following examples apply only to Python3, and Python2 sorting methods are not included in this article.

Situation One

The elements in the list are already comparable elements, and the list is passed directly into the sorted function to return a sequenced table. The default is ascending order, in descending order you can specify reverse parameters, such as:

>>> L = [3,5,4,1,8]
>>> sorted (L)
[1, 3, 4, 5, 8]
>>> sorted (L, Reverse=true)
[8, 5, 4, 3, 1]
>>>

Situation Two

The element that needs to be sorted is a tuple or a dictionary, and you want to sort by the keywords I specify, for example, there are two lists:

L_V1 = [(' B ', 2), (' A ', 1), (' C ', 3), (' d ', 4)]
L_V2 = [
 {' fname ': ' Brian ', ' lname ': ' Jones ', ' uid ': 1003},
 {' FName ': ' David ', ' lname ': ' Beazley ', ' uid ': 1002},
 {' fname ': ' John ', ' lname ': ' Cleese ', ' UID ': 1001},
 {' fname ': ' Big ', ' lname ': ' Jones ', ' uid ': 1004}
]

L_V1 is a list of tuples, L_V2 is a list of dictionaries. For L_V1 we want to sort by the second element in the tuple, and for l_v2 we want to sort by the dictionary's keyword UID.

The sorted function receives a keyword parameter key that specifies a callable function that returns a value (as long as it is comparable), then the sorted function sorts the elements in the list based on the returned keyword.

For example, for the above example:

>>> L_V1 = [(' B ', 2], (' A ', 1), (' C ', 3), (' d ', 4)]
>>> sorted (L_V1, Key=lambda x:x[1])
[(' A ', 1], (' B ', 2), (' C ', 3), (' d ', 4)]
>>> l_v2 = [
{' fname ': ' Brian ', ' lname ': ' Jones ', ' uid ': 1003},
{' fname  ': ' David ', ' lname ': ' Beazley ', ' uid ': 1002},
{' fname ': ' John ', ' lname ': ' Cleese ', ' uid ': ' 1001} ',
{' fname ': ' Big ', ' lname ': ' Jones ', ' uid ': 1004}
]
>>> sorted (l_v2, Key=lambda x:x[' uid ')]
[{' lname ': ' Cleese ', ' UID ': 1001, ' fname ': ' John '}, {' lname ': ' Beazley ', ' uid ': 1002, ' fname ': ' David '}, {' lname ': ' Jones ', ' uid ': 1003, ' fname ' : ' Brian '}, {' lname ': ' Jones ', ' uid ': 1004, ' fname ': ' Big '}]

Lambda functions are a common technique here. The x behind the Lambda keyword is the parameter that the function receives, and the expression that is followed by the colon is the return value of the function. For L_V1, each tuple is passed to the parameter x, which returns the second element of the tuple for sorting; for L_V2, the parameter x is passed to each dictionary element in the list, which returns the value of the UID in the dictionary for sorting.

In addition to the generic method of using the anonymous function lambda, the Python standard library operator provides us with a itemgetter function that replaces the lambda function we write with a slightly higher performance than the lambda function.

>>> from operator import itemgetter
>>> l_v1 = [(' B ', 2], (' A ', 1), (' C ', 3), (' d ', 4)]
>> > Sorted (L_V1, Key=itemgetter (1))
[(' A ', 1], (' B ', 2), (' C ', 3), (' d ', 4)]
>>> l_v2 = [
{' fname ': ' Brian ', ' lname ': ' Jones ', ' uid ': 1003},
{' fname ': ' David ', ' lname ': ' Beazley ', ' uid ': 1002},
{' fname ': ' John ', ' LName ': ' Cleese ', ' UID ': 1001},
{' fname ': ' Big ', ' lname ': ' Jones ', ' uid ': 1004}
]
>>> Sorted (l_v2 , Key=itemgetter (' uid ')]
[
{' lname ': ' Cleese ', ' uid ': 1001, ' fname ': ' John '}, 
{' lname ': ' Beazley ', ' UID ': 1002, ' fname ': ' David '}, 
{' lname ': ' Jones ', ' uid ': 1003, ' fname ': ' Brian '}, 
{' lname ': ' Jones ', ' UID ': 100 4, ' fname ': ' Big '}
]

All of the above examples are to return a single value for the sort key, as mentioned previously, the function received by the keyword key can return any comparable object. In Python, for example, tuples can be compared. The comparison rule for a tuple is to first compare the elements in the first position in the tuple and, if they are equal, to compare the elements in the second position, and so on. Back to L_v2 's example, assuming that now that the demand has changed, we first sort the values corresponding to lname, and if the lname corresponding values are equal, then the order is determined according to the fname.

>>> L_V2 = [
 {' fname ': ' Brian ', ' lname ': ' Jones ', ' uid ': 1003},
 {' fname ': ' David ', ' lname ': ' Beazley ', ' u Id ': 1002},
 {' fname ': ' John ', ' lname ': ' Cleese ', ' UID ': 1001},
 {' fname ': ' Big ', ' lname ': ' Jones ', ' uid ': 1004}
   ]
>>> sorted (L_V2, Key=lambda x: (x[' lname '), x[' fname ']))
[
 {' lname ': ' Beazley ', ' uid ': 1002, ' FName ': ' David '}, 
 {' lname ': ' Cleese ', ' uid ': 1001, ' fname ': ' John '}, 
 {' lname ': ' Jones ', ' uid ': 1004, ' fname ': ' B IG '}, 
 {' lname ': ' Jones ', ' uid ': 1003, ' fname ': ' Brian '}
]

In this example, the lambda function returns no longer a scalar value, but a tuple (x['lname'], x['fname']) , based on the comparison rule of the tuple, first sorted by the size of the elements in the first position of the tuple, x['lname'] because there are two dictionaries in the list whose lname corresponding values are Jones, As a result, the value of the element in the second position of the tuple is x['fname'] sorted, and because big is smaller than Brian (in alphabetical order), great is in the front.

It is also possible to use the Itemgetter function, and performance can be slightly elevated. Besides, I think Itemgetter is a little more concise and readable than a lambda.

>>> L_V2 = [
 {' fname ': ' Brian ', ' lname ': ' Jones ', ' uid ': 1003},
 {' fname ': ' David ', ' lname ': ' Beazley ', ' u Id ': 1002},
 {' fname ': ' John ', ' lname ': ' Cleese ', ' UID ': 1001},
 {' fname ': ' Big ', ' lname ': ' Jones ', ' uid ': 1004}
   ]
>>> sorted (l_v2, Key=itemgetter (' lname ', ' fname '))
[
 {' lname ': ' Beazley ', ' uid ': 1002, ' FName ': ' David '}, 
 {' lname ': ' Cleese ', ' uid ': 1001, ' fname ': ' John '}, 
 {' lname ': ' Jones ', ' uid ': 1004, ' fname ': ' B IG '}, 
 {' lname ': ' Jones ', ' uid ': 1003, ' fname ': ' Brian '}
]

Situation Three

The element that needs to be sorted is a Python object that we want to sort by one of its property values. For example, a list of User objects is listed below, sorted by their Name property:

Class User:
 def __init__ (self, name):
  self.name = name
def __str__ (self): return
' User:%s '% Self.name

__repr__ = __str__ # In order to allow user to display in the interpreter as ' user:name ' in the format

user_list = [User (' John '), User (' David '), Us ER (' big '), User (' Alen ')]

method, as before, defines a function that returns the value of the User's Name property, passing the function to the sorted key argument.

>>> user_list = [User (' John '), User (' David '), User (' big '), User (' Alen ')]
>>> sorted (user_list, Key=lambda x:x.name)
>>> sorted (user_list, Key=lambda x:x.name)
[User:alen, User:big, User:david, Us Er:john]

However, the Itemgetter method no longer works and is replaced by the Attrgetter method.

>>> Sorted (user_list, Key=attrgetter (' name '))
[User:alen, User:big, User:david, User:john]

Attrgetter is exactly the same as the Itemgetter usage, except that itemgetter is used to get the value of a location index or dictionary keyword, and attrgetter to get the object's property value.

Ps:sorted returns an ordered copy of the original list, and the order of the original list does not change. If you just want to sort in place (that is, sort the original list itself), call the list's sort method directly: list.sort() . The usage is the same as the sorted function, except that the function does not return a value, and the original list has been changed to a sequenced table after the call.

Grouping elements in a sequence

Like sorting, you want to group the elements of the same keyword into the same group based on a keyword in the list, and you can further process the grouped groups. For example, a list of the following:

rows = [
 {' address ': ' 5412 n Clark ', ' Date ': ' 07/01/2012 '},
 {' address ': ' 5148 N Clark ', ' Date ': ' 07/04/2012 '},
   
    {' address ': ' 5800 E 58TH ', ' Date ': ' 07/02/2012 '},
 {' address ': ' 2122 N CLARK ', ' Date ': ' 07/03/2012 '},
 {' Address ' : ' 5645 N Ravenswood ', ' Date ': ' 07/02/2012 '},
 {' address ': ' 1060 W ADDISON ', ' Date ': ' 07/02/2012 '},
 {' Address ': ' 4 801 N BROADWAY ', ' Date ': ' 07/01/2012 '},
 {' address ': ' 1039 W Granville ', ' Date ': ' 07/04/2012 '},
]
   

The elements of the list are dictionaries, and you want to group the elements of the same date by one group based on the date of the dictionary. The GroupBy function in Python's Itertools module can solve the problem well. In order to use the GroupBy function, you first need to sort the list:

>>> from operator import itemgetter
>>> sorted_rows = sorted (rows, key=itemgetter (' Date '))

GroupBy, like sorted, has a key key parameter that receives a callable function that returns a value that is used as a grouping keyword with the same usage as the sorted key keyword parameter.

>>> for date, items in GroupBy (Sorted_rows, Key=itemgetter (' Date '):
 print (date) to
 i in items:
  Print (', i)
07/01/2012
{' address ': ' 5412 n CLARK ', ' Date ': ' 07/01/2012 '}
{' address ': ' 4801 n BROADWAY ', ' da Te ': ' 07/01/2012 '}
07/02/2012
{' address ': ' 5800 E 58TH ', ' Date ': ' 07/02/2012 '}
{' address ': ' 5645 N Ravenswood ', ' Date ': ' 07/02/2012 '}
{' address ': ' 1060 W ADDISON ', ' Date ': ' 07/02/2012 '}
07/03/2012
{' Address ': ' 2122 n Clark ', ' Date ': ' 07/03/2012 '}
07/04/2012
{' address ': ' 5148 N Clark ', ' Date ': ' 07/04/2012 '}< c15/>{' address ': ' 1039 W Granville ', ' Date ': ' 07/04/2012 '}

You can see that the value returned by GroupBy is the value for the keyword that is used for grouping and all members of the group. GroupBy actually returns a generator, which can be processed separately for each group by iteration. It is important to note that sorting the list before grouping is essential, otherwise it is divided into groups for elements that are not immediately adjacent to each other, even if their values are the same.

Summarize

The above is about the Python sequence in the entire content of the order, I hope the content of this article for you to learn or use Python can help, if there is doubt you can message exchange, thank you for the cloud Habitat Community support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.