Summary of Python sequence methods

WBOY
Release: 2016-12-05 13:27:14
Original
1083 people have browsed it

I am currently working on a Python project, and I have specially sorted out the Python sequence methods. Sequence is the most basic data structure in Python. This article will first give a brief summary of the sequence, and then briefly explain the operation methods that are common to all sequences.

Any sequence can refer to its elements (items).

The following built-in functions can be used for lists (tables, fixed value tables, strings)

#s is a sequence

len(s) Returns: The number of elements contained in the sequence
min(s) Return: the smallest element in the sequence
max(s) Return: the largest element in the sequence
all(s) Return: True, if all elements are true
any(s) Return: True, if any element is True

The following method mainly functions as a query and does not change the sequence itself. It can be used for tables and fixed value tables:

# x is the element value, i is the subscript (the position of the element in the sequence)

sum(s) Returns: the sum of all elements in the sequence
s.count(x) Return: the number of times x appears in s
s.index(x) Returns: the subscript of the first occurrence of x in s

Since the elements of the fixed value table cannot be changed, the following method is only applicable to the table:

#l is one table, l2 is another table


l.extend(l2) Add all elements of table l2 at the end of table l
l.append(x) Append x element to the end of l
l.sort() Sort the elements in l
l.reverse() Reverse the order of the elements in l
l.pop() Return: the last element of table l and delete the element in table l
del l[i] Delete this element

(The above methods all operate on the original table and will affect the original table instead of returning a new table)

Here are some methods for strings. Although strings are a special type of constant value table, the string class has methods that mutate strings. The essence of these methods is not to operate on the original string, but to delete the original string and create a new string, so it does not conflict with the characteristics of the fixed value table.

#str is a string, sub is a substring of str. s is a sequence, and its elements are all strings. width is an integer used to describe the width of the newly generated string.

str.count(sub) Return: the number of times sub appears in str
str.find(sub) Return: Starting from the left, find the position of the first occurrence of sub in str. If str does not contain sub, return -1
str.index(sub) Return: Starting from the left, find the position where sub first appears in str. If str does not contain sub, raise an error
str.rfind(sub) Return: Starting from the right, find the position where sub first appears in str. If str does not contain sub, return -1
str.rindex(sub) Return: Starting from the right, find the position of the first occurrence of sub in str. If str does not contain sub, raise an error
str.isalnum() Return: True, if all characters are letters or numbers
str.isalpha() Return: True if all characters are letters
str.isdigit() Return: True if all characters are numbers
str.istitle() Returns: True if the first letter of all words is capitalized
str.isspace() Return: True if all characters are spaces
str.islower() Returns: True if all characters are lowercase letters
str.isupper() Returns: True if all characters are uppercase letters
str.split([sep,[max]]) Return: Starting from the left, using spaces as separators, split str into multiple substrings, max times in total. Return the resulting substring in a table. You can use comma or other delimiters using str.split(',')
str.rsplit([sep,[max]]) Return: Starting from the right, using spaces as separators, split str into multiple substrings, max times in total. Return the resulting substring in a table. You can use comma or other delimiters in the form of str.rsplit(',')
str.capitalize() Return: Capitalize the first letter of str
str.lower() Return: Change all letters of str to lowercase
str.upper() Return: Change all letters of str to uppercase
str.swapcase() Return: Change str uppercase letters to lowercase and lowercase letters to uppercase
str.title() Returns: Capitalize the first letter of each word of str (separated by spaces)
str.center(width) Return: a string with length width, put the original string into the center of the string, and leave other empty positions as spaces.
str.ljust(width) Return: a string with length width, put the original string into this string with left alignment, and leave other empty positions as spaces.
str.rjust(width) Return: a string with length width, put the original string into this string with right alignment, and leave other empty positions as spaces.
str.join(s) Return: Combine the elements in s into a string using str as the separator.
str.strip([sub]) Return: Remove spaces at the beginning and end of the string. You can also provide the parameter sub to remove the sub at the beginning and end of the string
str.replace(sub, new_sub) Return: replace sub in str with a new string new_sub

Thanks for reading, I hope it can help you, thank you for your support of this site!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!