"Python" List & dict & Str

Source: Internet
Author: User

List&dict&string

These three types are the most commonly used data types in Python. They're all a kind of sequence.

Sequence common operations

1. sharding

S[A:B] Returns the fragment from S[a] to s[b-1] in the sequence s. Note s[0:0] is an empty set and not a s[0]

S[A:B:C] Add a third parameter to set the sampling step. Can be set to negative to sample from right to left

2. Subtraction

[None] * * => [None,none]

3. Operation of some built-in functions

Len (s), Max (s), Min (s), etc.

List

For list ls

Assignments can be assigned to shards as long as they don't cross the boundary

The Del del statement is followed by List[index], not the value or index

Ls.append () return value is None

Ls.count (...) Count the number of occurrences of a value

Ls.extend (Another_ls) merges two lists

Ls.index (value) Gets the index of a value

Ls.insert (Index,value) inserts a value somewhere

Ls.pop () does not add a parameter to the default pop out of the last element and returns its value

Ls.remove (..) Remove the first match from left to right to the value, if no error is found

Ls.reverse () returns none, and the LS itself is reversed

Ls.sort () returns none, sorts the LS itself

The above two and sorted (LS), reversed (LS) difference, these two are the LS as the material to return a sort or an inverted iterator.

  By the way, sorted and sort can then pass a key as a parameter, and key is a function object, meaning that it is not the default when sorting, but rather the return value that is processed after the value is passed as a parameter to the key function. For example, if you want a list of dictionaries to be sorted by the value of a key "Dict_key" in each dictionary, you can sorted (Dict,key=lambda x:x.get ("Dict_key"))

  

Dict

for dictionary D

D.clear () Empty dictionary

D.copy () deep copy

D.get (...)

D.has_key (...) Determine if a dictionary contains a key

D.items () dictionary list, each element is a tuple, tuple[0] is key,tuple[1] is value

D.keys ()

D.update (e) Update D with E, create a new item for key not in D, and for some overwrite the old value

D.values ()

Str

For string s

S.find (...) Returns the location of a substring

' Mark ' joins a list by combining mark as a delimiter into a single string.

S.lower () All lowercase

S.upper () All Caps

S.title () Capitalize all words in the first word

S.capitalize () string first capitalization

S.isupper () Determines whether all characters are uppercase

S.islower ()

S.istitle ()

The above six or seven processing of the literal character of a string is a return value that does not change the character of the string itself.

S.replace (A, B)

S.split ()

S.strip ()

S.format ("A", "B") replaces "{1}" in S with "a", "B", etc., and "{2}" parts, equivalent to%s% (...). 's role. If S is not written in {1},{2} This can write {Some_text}, and then in the format parameter will write some_text= ... To specify the name of the replacement variable (a bit of the meaning of the template language)

String module

In fact, some of the Str class itself is not strong enough, so there is a dedicated string module to deal with some things

The member variables of the string module are:

String.lowercase refers to all lowercase letters, equivalent to "abcd....xyz"

String.uppercase

String.letters all letters, uppercase and lowercase, equivalent to "ABCD...XYZABC." XYZ "

String.whitespace all whitespace characters

String.punctuation some punctuation

Method of the String module:

Capitlize (s) returns the first uppercase string of s

Capwords (s,sep) with Sep to split S, and then split out the first letter of each part of the capital, and then use Sep to connect these parts, Sep default is a space

Center (S,width[,fllchar]) put s in width (an int number) long string solemn, both sides with Fillchar fill. can be used to generate a delimiter in a text file.

Count (S,sub[,start[,end]]) returns the number of occurrences of a sub substring in a s[start:end] fragment

Swapcase (s) toggle uppercase and lowercase letters

Zfill (S,width) on the left side of s add 0 know width to reach widths

As mentioned above, s.format similar to the function of template language (such as Velocity,python JINJA2 module, etc.), in fact, this is based on some of the classes in string. There is a template class in string that allows users to manually construct simple templates:

 from Import  = Template ("Hello, ${name}")    # variable with ${...} Form S.substitute (name="Frank")    # return Hello, Frank S.safe_substitute (namae="Frank")    #save_substitute () No errors for variables not found

"Python" List & dict & Str

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.