Python's detailed use of lists, tuples, dictionaries, collections, and strings

Source: Internet
Author: User

1. List

The list is equivalent to the array in C + +, is an orderly project, through the index to find, but the use of a lot of convenience, the specific operation to see the code, the practice of their own once is very simple.

Note: The list is generally used in brackets "[]"

#list (array)Name_list = ['Huahua','Weiwei','Jack','Dabao']name_list.append ('Wenyen')#add an element to the back#For exampleName_list.insert (1,"Jack") Name_list.count ('Jack') Name_list.index ('Jack') Name_list.remove ('Jack')#can only fill in the real value, otherwise with del Name_list[1]Name_list.pop ()#Delete LastName_list.reverse ()#ReverseName_list.sort ()#Sort by ASC2 code#For exampleNum_list = ['a','b','C','D']name_list, Num_listname_list.extend (num_list)#extension list equivalent to name_list + = Num_listName_list[2:5]#slices, Gu Tou regardless of the tailName_list[-5:]#from the back to the next,: Nothing in the back to take the last numberName_list[name_list.index ('a'): Name_list.index ('a') +3]name_list[1::3]#from the first start, take one out of two.

2, meta-group

Tuples are the collection of diverse objects together, can not be modified by the index to find, the main characteristics of the tuple is not modifiable, equivalent to the constant, is used as a whole.

Note: Tuples are generally enclosed in parentheses "()"

Tuples and lists can be converted to each other, as follows

# tuple (equivalent to a constant cannot be modified) with a called tuple () with [] called list a = (1,2,3,4)# tuple and list of mutual conversions  == tuple (a) Type (a)

3. Dictionaries

A dictionary is a set of keys (key) and a combination of values (value) that are searched by key (key), with no order

Note: Use curly braces "{}"

#dictionary (all keys must be unique) with {}Person_info = {"name":"Vivi",' Age':' -','Job':'Engineer'}person_info['Salary'] = 300000#Newperson_info['Job'] ='it'  #ChangePerson_info.pop ('Salary')#Pop can specify deletePerson_info.popitem ()#Popitem randomly deletes one without specifying it (because the dictionary has no data)Person_info.get ('name')#with this key return value, no return to none, not like "" Will errorPerson_info.has_key ('Job')#determine if there is a key, no then can writePerson_info.keys ()#Show only KeyPerson_info.setdefault ('Gentle','male')#Set the default value, there is no change, no joinNew_info= {' Age': 19,'Addr': ['Wuhan','Beijing']}person_info.update (new_info)#Update the new dictionary to the old dictionaryPrintperson_info['name']#print is used in the file to outputPrintPerson_info.items ()#items default to the index and value (in the form of a list)PrintPerson_info

Note: The following are two types of loops about dictionaries

 for  in Person_info:  #  high efficiency    print  i, person_info[i] for   in Person_info.iteritems ():  #  first print in the form of a list and then cycle, low efficiency    Print K, V

4. Collection

Collections are unordered, auto-de-weight, index not supported but can be tested for relationships

Note: The collection is represented by "{}"

# Set (unordered auto-de-weight, relationship test, unsupported index) with {}a = range (= Set (a) A.add (-1) A.pop ()print  = {1, 2, 3, 4= {3, 4, 5, 6}print x & yprint x | y print x- yprint x ^ y

5. String

String actually does not belong to the basic structure, just lazy to carry out to write ~

#stringmsg ="What's your company ' s name?"msg=msg.capitalize () msg=msg.upper () msg=msg.lower () msg+='Weiwei'  #This operation to re-open two memory space, with join only need to open up oncemsg =msg.swapcase () msg_list= Msg.split ()#by default, a space is separated into a list (which can be set by itself) string into list'hhhhhhhhh'. Join (Msg_list)#the cloze of the list to a string is converted to ~list.Msgmsg.startswith (' What') Len (msg) CMP ('a','b')#is less than-1cmp'b','a')#is greater than 1cmp'a','a')#equal to 0Ord ('h')#Ask ASC2 code

The above is my own book to check the summary of some of the more commonly used commands, more chaotic, but because this is written before the note is too lazy to change, in fact, copy to the computer inside run all the command meaning is very clear ~ a little own experience ~

Python's detailed use of lists, tuples, dictionaries, collections, and strings

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.