Re-reading LPTHW-Lesson25, lpthw. web

Source: Internet
Author: User

Re-reading LPTHW-Lesson25, lpthw. web

1. ex25 contacts the string functions split (), list functions sorted (), pop (), and sorts out their usage and other common string and list operation functions as follows.

(1) split () function

Split () is a string sharding function that slices strings by specifying delimiters and returns a list of separated strings. Each string can also be assigned a value (or is this called unpacking ?) New Variable

Syntax: str. split (str = "", num) [n]

Description of parameters: str indicates the delimiter. The default Delimiter is a space and cannot be blank (""). If no separator exists in the string, the entire string is used as an element of the list and the list is returned.

Num indicates the number of separation times. The default value is the maximum number of separation times. If the num value is specified, the number is separated into num + 1.

① Separate by Default Characters

1 str = "www.cnblogs.com" 2 print str3 str_split = str. split () # use the default parameter 4 print str_split

Output:

② Use "." As the Separator

str_split = str.split(".")   
print str_split

Output:

Separated n times

Print str. split (". ", 0) # Separate 0 print str. split (". ", 1) # Separate print str once. split (". ", 2) # separate two times

Output:

Separate two times and take the item with a sequence of 1

Print str. split (".", 2) [1] # separate two times, take the item whose sequence is 1

Output:

③ Assign the separator string to the variable.

str1,str2,str3 = str.split(".",2)print str1print str2print str3

Output:

(2) The join () function is opposite to split (), used to connect strings

The usage is as follows:

list = ['My','name','is','Jer']print ' '.join(list)print '_'.join(list)print '..'.join(list)

Output:

(3) sorted () function

In Python, there are two types of sorting: the built-in function sorted (), which generates a new sorted list; the other is the sort method, which sorts the list directly.

Sorted (): There are four parameters: sorted (iterable, cmp = none, key = none, reverse = False): ① here iterable is the sorting object, Chinese meaning is iterator, including string, list, dictionary, and Other Object Types

② Cmp specifies a custom comparison function. This function receives two parameters (the iterable element). If the first parameter is smaller than the second parameter, a negative number is returned; if the first parameter is equal to the second parameter, zero is returned. If the first parameter is greater than the second parameter, a positive number is returned. The default value is None.

③ Key specifies a function that receives a parameter. This function is used to extract a keyword for comparison from each element. The default value is None.

④ Reverse is a Boolean value that indicates the order of arrangement. If it is True, it indicates the order from large to small. The default value is False.

See the example below:

A. Basic usage:

As you can see, the list. sort () method modifies the original list and returns none. sorted (list) returns a new sorting list. Note that list. sort () is defined only for list. The sorted () function applies to all iterators.

B. key function Application

① Sort by the length of the elements in the list:

def func(x):    return len(x)list = ['Python','is','a','great','language']print sorted(list,key = func)

Output:

② The elements in the conversion list are sorted in lower case

List = ['zza', 'aaz', 'nnew', 'merww '] print sorted (list, key = str. lower) # Sort data after case Conversion

Output:

2. Common list functions and operations

(1) list () convert string to list

 

(2) len () obtains the number of list elements.

(3) Use indexes to obtain the elements at the corresponding position in the list. The index starts from 0. 0 indicates the first element, and-1 indicates the last element. Similarly,-2 indicates the last element.

(4) using indexes, you can directly assign values to the elements at the corresponding position.

(5) The list slicing operation uses the list slicing operation to easily obtain some elements of the list.

① Retrieve some elements

If the instance is visible, list [index1: index2] displays the list elements from list [index1] to list [index2-1], list [index2] is not displayed; When index1 is omitted, list [: index2]. The default value is index1 = 0. If index2 is omitted, that is, list [index1:]. The default value is index2 = len (list ). therefore, list [:] = list [:-1] will display the entire list

② Modify elements

(6) When the insert () method inserts a string or list into a position in the list, each character of the string and each item in the list are inserted as an element of the list, rather than the whole insert. To insert the entire data, you can use the insert method of list.

(7) The append method list is an ordered list. The append method of the list can append new elements to the end of the list.

(8) The pop method pop (index) can delete the element at the specified position in the list and return the element. If the index value is omitted, the last element is deleted and returned.

(9) remove () method to delete the first specified Element

Loop allows you to delete all specified elements.

(10) index method the index method is used to find the location of the first matching item in the list that matches the specified element, that is, the index.

If the specified element does not exist in the list, an exception is triggered.

 

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.