day4 - how to manipulate lists and tuples in python

day4 - manipulation of python lists and tuples

1. List: Store a large number of ordered data of various data types, retaining the format of the original data

CRUD

Add: three methods

append (variable expression) method

list.append(variable expression) appends to the end of the list

insert(index, variable expression) method, insert by index

list.insert(index, variable expression)

extend("variable expression")

list.extend("variable expression"), split the variable into next-level elements, iteratively append to the end of the list

 

Deletion: Four Methods

pop(index) delete by index

remove(element) remove by element

clear(list) clear

del, delete by index, delete by slice, delete from memory

del list[index], del list[index 1, index 2], del list

 

Change: two methods, change by index, change by slice

list[index] = new variable

list[index1,index2] = new variable, delete elements between indexes (regardless of head and tail), split the variable into next-level elements, and iteratively fill in the index space,

Lookup: index, slice, for loop

Other methods

len(list) method, list.count(variable) method, sort sort() method, forward sort, reverse sort sort(reverse=True), reverse order reverse() method

index() method, list has no find method

Nesting of for loops, iterating over each element of each level in the output list (from left to right)

type() method, query element data type, and can be used for conditional judgment

The range() method is equivalent to generating a list of numbers with a set range. The starting number is 0 by default. It can be omitted. The positive and negative steps can be added to traverse the array.

Nesting of lists: multi-level index queries, traversing lists with multi-level for loops

Tuple: It is called an immutable list, there is no addition, deletion and modification function, only query function, through index, slice query, and general methods len(), count()

Although the next level of the tuple, the elements of the tuple cannot be modified, but the next level of the tuple and the non-element elements of the lower level can be modified, and the modification method is the same as that of the list.

 

2. Review of yesterday's content

Integer method bit_length() method, calculation

How to work with strings

center(width,"fill character"), str is centered, filling characters on both sides

strip ("string") removes the leading and trailing spaces, tab keys, newlines by default, and can also remove the specified characters at the beginning and the end. If the specified character contains multiple elements, all the elements of the specified character at the beginning and the end are removed.

lstrip() to the head, rstrip() to the tail,

"Character".join(list), convert the list to a string, connect with empty elements by default

list.split("split character", count), split the string into lists from left to right, the number can be set

upper(), lower() method, all uppercase, all lowercase

The capitalize() method changes the first element of the string to uppercase (if the first element is not an English letter, there is no change), and the rest of the English letter elements are lowercase

title() method, capitalize the first letter of English words separated by non-English characters, and lowercase the rest of the letters

swapcase() method, swap the upper and lower case of English letters

find(), index() methods, return the first index of the first specified character, return -1 if the find is not found, and report an error if the index is not found

startswith(), endswith() method, can be sliced, query whether the specified string starts and ends

 

Generic methods, len(), count()

format formatted output three methods

str = "{}{}{}".format (the first {} padding character, the second {} padding character, the third {} padding character), ordered

Format method by index, str=”{2}{0}{2}”.format (string at index 0, string at index 1, string at index 2), ordered

Format method by keyword, str = "{keyword1}{keyword3}{keyword2}".format(keyword3:"str1",keyword1:"str2",keyword3:" str3")

How to determine the composition of string elements

.isalbum() numbers and letters

.isdigit() digit composition

.isalpha() letter composition

The type()== method can also be used as a judgment condition for if

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325274384&siteId=291194637