2018-05-24--python Fourth day

Source: Internet
Author: User
Tags shallow copy

You can nest lists in a list

The elements in the list can be numbers, characters, booleans, lists, and so on

You can take values by index, slice. Nested values can be indexed multiple times

String conversion list List[str]. Internal use for

List conversion string, you need to write the For loop yourself: If there are only strings in the list, use str = "" directly. Join (LI)

The values in the list can be modified, or they can be deleted

Del li[1] or del Li [2:6]

List.clear ()//Clear List

Test=list.pop (x)//delete the element of index position x, and test gets the deleted value. Default x is-1

List.remove (x)//delete specified value in list x, default from left, delete once

List.reverse ()//Reverses the list order

List.sort (key,reverse)//list default from small to large (reverse=false)

List.append (parameter)//append parameter after list

List.extend (Can iterate object x)//Append x iteration to List

List.insert (x, y)//Go to the specified index position in the list x insert Y

Test=list.copy ()//list shallow copy to test

Test=list.count (x)//calculate the number of X elements of the list

Test=list.index (x)//Find the index position of x in the list. Default from left to right, you can set the starting value

Ganso is actually a list of two times the processing

Tuple= (111,222,333,444)

The first-level elements of the Yongzu cannot be modified and cannot be added or deleted

Secondary and below can be modified, added and deleted

Elements can be any type, or can be nested

#一般元祖在最后一个元素加个逗号, separate from the method area. Ganso length and number of elements does not change

Ganso can be indexed, sliced, for loops, and can iterate over objects

can be converted directly to strings and lists

Tuple.count (x)//Find the number of X elements in a tuple

Tuple.index (x)//finds the index position of x in a tuple. Default from left to right, you can set the starting value

Dictionary Dict

Dict = {"K1": "V1", "K2", "V2"}//k1, K2 two values, respectively V1 and v2.

K can not be a list, dictionary

V can be any type

The dictionary is unordered and cannot be sliced.

Indexed way to find v = dict[' K1 ']

You can use Del to delete all levels of key and the corresponding value

The default for the For loop is key ... The for Test in Dict.values () will not take values.

Dict.clear Copy Items keys values

Dict.items ()//The value of Dict is iterated, returned to the list

Dict.fromkeys (["Key1",............],value)//Create dict based on key sequence and give unified value

Dict.get ("Key", Vlaue)//To get the corresponding value in Dict according to Key, if the mismatch is returned value, the default return is None

Dict.pop (' key ', value)//delete key and get corresponding value, can delete more than one key, if no match then return value, default return None

Dict.popitem ()//delete a value randomly

# #以上两个如果是k, V=dict.pop, will return key and value two values

Dict.setdefault (' key ', ' value ')//If key does not exist in Dict, create and assign a value, return the assignment. Returns the existing value if it exists

Dict.update ({' K1 ': ' v1 '})//Will K1 update v1 and create if it does not exist. Or it can be. Update (K1=V1,..) This notation

2018-05-24--python Fourth day

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.