Python sequence operations (advanced) and python sequence operations (advanced)

Source: Internet
Author: User

Python sequence operations (advanced) and python sequence operations (advanced)

Introduction

The sequence of Python is usually an iterative container that can store any type of elements. List and meta-group data types are the most commonly used sequences. There are six built-in sequences in python, except the two types mentioned earlier, there are also string, Unicode string, buffer object, and the last xrange object, which are not frequently used. This article describes how to use list derivation, slice naming, list element sorting, and list element grouping. After learning the basic list operations of Python, you can learn these advanced operations to make the code written more elegant and concise and pythonic.

List Derivation

When we want to construct a list based on certain rules, we should first consider the list derivation. List derivation simplifies loop operations. For example, if we want to retrieve all. py files from a list of original file names, we usually do this without list derivation:

file_list = ['foo.py', 'bar.txt', 'spam.py', 'animal.png', 'test.py']py_list = []for file in file_list:if file.endswith('.py'):py_list.append(file)print(py_list)# output['foo.py', 'spam.py', 'test.py']

The list derivation can be simplified:

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

List derivation describes many online resources. Here, we only 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 this requirement. Otherwise, you will return to the conventional method.

Name a slice

The list slicing of Python is very convenient to use, but sometimes it also affects the code readability. For example, there is a string:

record = '..........19.6..........100..........'

19.6 is the product price, and 100 is the product quantity. The total price is calculated as follows:

However, if this is the case, we may have forgotten it when we read the code later.record[10:14] ,record[24:27] What is it? To solve the preceding problem, you can add a name to the slice to enhance readability.

record = '..........19.6..........100..........'price = slice(10, 14)count = slice(24, 27)total_price = float(record[price])*int(record[count])

The parameter format received by slice isslice(stop),slice(start, stop[, step]) . If only one parameter is received, it is equivalent to the slice syntax.[:stop]If two parameters are received, the slice syntax is equivalent.[start:stop]If three parameters are received, the slice syntax is equivalent.[start:stop:step].

Sort

Sorting tasks are usually completed by the built-in function sorted. Elements to be sorted are generally stored in a list container, and the list can store any type of elements. The key keyword of the sorted function allows us to easily specify keywords for element sorting, this makes sorting very simple. Below are several common sorting examples to illustrate how to use the key keyword. Note that the sorting methods of Python3 and Python2 are not universal. The following example only applies to Python3. The sorting methods of Python2 are not included in this article.

Scenario 1

The element in the list is already a comparable element. You can directly pass the List into the sorted function to return a sorted list. The default value is ascending. You can specify the reverse parameter in descending order. For example:

>>> l = [3,5,4,1,8]>>> sorted(l)[1, 3, 4, 5, 8]>>> sorted(l, reverse=True)[8, 5, 4, 3, 1]>>>

Case 2

The elements to be sorted are a tuples or dictionary. You want to sort them by the keyword I specified. 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 and l_v2 is a list of dictionaries. For l_v1, we want to sort by the second element in the tuples. For l_v2, we want to sort by dictionary keyword uid.

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

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. X behind the lambda keyword is the parameter received by the function, and the expression behind the colon is the return value of the function. For Rochelle V1, the second element of the returned tuples is used for sorting. For Rochelle V2, each dictionary element in the list is transmitted to parameter x, the Return Value of the uid in the dictionary is used for sorting.

In addition to the general method of using anonymous function lambda, the Python standard library operator provides us with an itemgetter function to replace the lambda function we write, and its performance will be slightly higher than using lambda functions.

>>> 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': 1004, 'fname': 'Big'}]

In the preceding example, a single value is returned for sorting keywords. As mentioned above, the function receiving the keyword key can return any comparable object. For example, in python, tuples can be compared. The comparison rule for tuples is to first compare the elements at the first position in the tuples. If they are equal, compare the elements at the second position, and so on. Return to the l_v2 example. If the demand changes, we first sort the values corresponding to the lname. If the values corresponding to the lname are equal, then determine the order based on the fname.

>>> 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['lname'], x['fname']))[ {'lname': 'Beazley', 'uid': 1002, 'fname': 'David'},  {'lname': 'Cleese', 'uid': 1001, 'fname': 'John'},  {'lname': 'Jones', 'uid': 1004, 'fname': 'Big'},  {'lname': 'Jones', 'uid': 1003, 'fname': 'Brian'}]

In this example, the lambda function does not return a scalar value, but a tuples.(x['lname'], x['fname'])According to the comparison rules of tuples x['lname']Because there are two dictionaries in the list whose lname values are Jones, then sort the values based on the elements at the second position of the tuples.x['fname'] Because Big is smaller than Brian (compared in alphabetical order), Big is ranked first.

You can also use the itemgetter function, and the performance will be slightly improved. In addition, I think itemgetter is more concise and readable than lambda.

>>> 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('lname', 'fname'))[ {'lname': 'Beazley', 'uid': 1002, 'fname': 'David'},  {'lname': 'Cleese', 'uid': 1001, 'fname': 'John'},  {'lname': 'Jones', 'uid': 1004, 'fname': 'Big'},  {'lname': 'Jones', 'uid': 1003, 'fname': 'Brian'}]

Case 3

The element to be sorted is a Python object, which we want to sort based on a specific attribute value. For example, a list of stored User objects is as follows, sorted by the name attribute:

Class User: def _ init _ (self, name): self. name = namedef _ str _ (self): return 'user: % s' % self. name _ repr _ = _ str _ # user_list = [User ('john '), user ('David '), User ('Big'), User ('alen')]

The method is similar to the previous one. Define a function to return the value of the name attribute of the User and pass the function to the key parameter of sorted.

>>> 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, User: John]

However, the itemgetter method does not work anymore and is replaced by the attrgetter method.

>>> sorted(user_list, key=attrgetter('name'))[User: Alen, User: Big, User: David, User: John]

The usage of attrgetter is exactly the same as that of itemgetter, except that itemgetter is used to obtain the value of a location index or dictionary keyword, while attrgetter is used to obtain the attribute value of an object.

PS: sorted returns a sorted copy of the original list, but the order of the original list does not change. If you only want to sort in place (that is, sort the original list itself), you can directly call the sort method of list:list.sort(). Its usage is the same as that of the sorted function, but the function does not return a value. After the function is called, the original list is changed to an ordered list.

Group Elements in a sequence

Similar to sorting, You Want To group elements with the same keywords to the same group based on a keyword in the list, and further process the groups. For example, there is a list as follows:

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': '4801 N BROADWAY', 'date': '07/01/2012'}, {'address': '1039 W GRANVILLE', 'date': '07/04/2012'},]

The list element is a dictionary. Now, you want to group the elements with the same date value to one group. The groupby function in the itertools module of Python can solve this problem well. To use the groupby function, you must first sort the list:

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

Groupby also has a key keyword parameter like sorted, which receives a callable function, and the value returned by this function is used as the grouping keyword, which is the same as the sorted key keyword parameter.

>>> for date, items in groupby(sorted_rows, key=itemgetter('date')): print(date) for i in items:  print(' ', i)07/01/2012{'address': '5412 N CLARK', 'date': '07/01/2012'}{'address': '4801 N BROADWAY', 'date': '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'}{'address': '1039 W GRANVILLE', 'date': '07/04/2012'}

The value returned by groupby is the value corresponding to the keyword used for grouping and all members of the group. Groupby actually returns a generator. Each group can be processed by iteration. It is worth noting that sorting the list before grouping is essential, otherwise non-adjacent elements will be divided into different groups even if their values are the same.

Summary

The above is all about the python sequence advanced article. I hope this article will help you learn or use python. If you have any questions, please leave a message, thank you for your support.

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.