One: Data structure
Data structures can be thought of as being used to process some data or to store it.
The introduction of data structures is related to the definition of classes and objects, which are described in this two definition.
What is a class: Say class first we can think of the type, in the data structure of the type there are commonly used types have int integer, float float type, and so on. There are methods for classes in Python, and we can simply understand what can be done with this class.
What is an object: An object is a variable that is actually defined, the type of i = 5 i is an int integer, and the object is I.
II: Classification of Python data structures
list, tuple, dictionary, sequence. The following categories describe the respective data structures.
Three: List
list
sequence
#!/usr/bin/python# filename: using_list.py# this is my shopping listshoplist = [' apple ', ' mango ', ' carrot ', "banana ']print ' i have ', Len (shoplist), ' Items to purchase. ' print ' These items are: ', # notice the comma at end of the linefor item in shoplist: print item,print ' \nI Also have to buy rice. ' Shoplist.append (' rice ') print ' my shopping list is now ', shoplistprint ' I will sort my list now ' Shoplist.sort () print ' sorted shopping list is ', shoplistprint ' the first item i will buy is ', shoplist [0]olditem = shoplist[0]del shoplist[0]print ' i bought the ', olditemprint ' My shopping list is now ', &NBSp;shoplist
shoplist
A variable is someone's shopping list. In shoplist
, we only store the string of the names of the things that are purchased, but remember that you can add any kind of objects including numbers or even other lists to the list.
We also used for..in
loops to recursively iterate through the items in the list. From now on, you must have realized that the list is also a sequence.
Notice that we print
use a comma at the end of the statement to eliminate print
the line breaks that are automatically printed for each statement. It's a bit ugly, but it's really simple and effective.
Next, we use the append
method to add an item to the list, as discussed earlier. We then verify that the item is actually added to the list by printing the contents of the list. The Print list simply passes the list to the print
statement, and we can get a neat output.
Next, we use a list of sort
methods to sort the list. It should be understood that this method affects the list itself rather than returning a modified list-this differs from how the string works. This is what we call the list to be mutable and the string immutable .
Finally, but when we finished buying something in the market, we wanted to remove it from the list. We use del
statements to do this work. Here, we point out which item in the list we want to delete, and the del
statement removes it from the list for us. We indicate that we want to delete the first element in the list, so we use del shoplist[0]
(remember, Python counts from 0)
The output result is
$ python using_list.pyi has 4 items to purchase. These items are:apple Mango carrot Bananai also has to buy rice. My shopping list is now [' Apple ', ' mango ', ' carrot ', ' banana ', ' rice ']i'll sort my list nowsorted shopping list is [' app Le ', ' banana ', ' carrot ', ' mango ', ' rice ']the first item I'll buy is Applei bought the Applemy shopping list was now [' ban Ana ', ' carrot ', ' mango ', ' rice ']
Quad: tuples
tuples and lists are very similar, except that tuples and strings are Immutable that is, you cannot modify tuples. Tuples are defined by a comma-separated list of items in parentheses. Tuples are typically used when a statement or user-defined function can safely take a set of values , that is, the value of the tuple being used does not change.
#!/usr/bin/python# Filename:using_tuple.pyzoo = (' Wolf ', ' Elephant ', ' penguin ') print ' number of animals in the zoo is ', l En (Zoo) New_zoo = (' monkey ', ' Dolphin ', zoo) print ' Number of animals in the new zoo are ', Len (new_zoo) print ' all animals in New Zoo was ', New_zooprint ' Animals brought from the old Zoo was ', New_zoo[2]print ' last animal brought from the old Zoo is ', new_ ZOO[2][2]
Len (Zoo) is the length that gets the tuple, stating that the tuple is a sequence.
The definition of a new_zoo is a tuple that is newly created, which should be a tuple that is one that cannot become, cannot be changed directly, and needs to define a new tuple, and the new tuple has a zoon that is superimposed over it.
Gets the new tuple sequence.
Output a new tuple sequence
Gets the 3rd sequence within the new tuple sequence, which should be counted starting at 0.
Gets the third element within a third tuple.
Output results
$ python using_tuple.pynumber of animals in the zoo was 3Number of animals in the new zoo was 3All animals in New zoo was (' Monkey ', ' Dolphin ', (' Wolf ', ' Elephant ', ' penguin ')) Animals brought from the old Zoo is (' Wolf ', ' Elephant ', ' penguin ') last A Nimal brought from the old Zoo is Penguin
Five: Dictionary
A dictionary is similar to having a key value to find the corresponding information through this key value. Note that the key must be unique
note that You can only use immutable objects (such as strings) as keys to the dictionary, but you could use mutable or immutable objects as values for the dictionary. Basically, you should only use simple objects as keys. The key-value pairs are marked in this way in the dictionary: d = {key1:value1, Key2:value2}
key/value pairs are split with colons , and individual pairs are separated by commas , all of which are included in curly braces.
Remember that the key/value pairs in the dictionary are not sequential. If you want a particular order, then you should sort them yourself before using them. Dictionary is dict
the instance/object of the class.
#!/usr/bin/python# filename: using_dict.py# ' ab ' is short for ' a ' ddress ' B ' ookab = { ' Swaroop ' : ' [email Protected] ', ' Larry ' : ' [email protected] ', ' Matsumoto ' : ' [email protected] ', ' spammer ' : ' [email protected] ' }print "Swaroop ' s address is %s" % ab[' Swaroop ']# adding a key/value pairab[' Guido '] = ' [email protected] ' # deleting a key/value pairdel ab[' spammer ']print ' \nthere are %d contacts in the address-book\n ' &NBSp;% len (AB) for name, address in ab.items (): print ' contact %s at %s ' % (name, address) if ' Guido ' in ab: # Or ab.has_key (' Guido ') print "\nguido ' s address is %s" % ab[' Guido ']
Dictionary for AB output dictionary AB internal key value is Swaroop
Add a key value of guido:[email protected]
Delete a key value is Sapmmer
Number of sequences of output dictionary AB
Change the dictionary to the corresponding group.
If the dictionary ab has Guido then output the dictionary
Output results
$ Python Using_dict.pyswaroop's address is [email protected]there be 4 contacts in the Address-bookcontact swaroop at [em Ail protected]contact Matsumoto at [email protected]contact Larry @ [email protected]gcontact Guido at [email protected]g Uido ' s address is [email protected]
VI: Sequence
list, Tuples and strings are sequences, but what are the sequences and why are they so special? The two main features of a sequence are index slice
#!/usr/bin/python# filename: seq.pyshoplist = [' apple ', ' mango ', ' carrot ', ' Banana ']# indexing or ' Subscription ' operationprint ' Item 0 is ', shoplist[0]print ' Item 1 is ', shoplist[1]print ' item 2 is ', shoplist[2 ]print ' Item 3 is ', shoplist[3]print ' item -1 is ', shoplist[-1]print ' Item -2 is ', shoplist[-2]# slicing on a listprint ' Item 1 to 3 is ', shoplist[1:3]print ' item 2 to end is ', shoplist[2 :]print ' Item 1 to -1 is ', shoplist[1:-1]print ' Item start to end is ', shoplist[:]# slicing on a stringname = ' swaroop ' Print ' Characters 1 to 3 is ', name[1:3]print ' characters 2 to end is ', name[2:]print ' Characters 1 to -1 is ', name[1:-1]print ' Characters start to end is ', name[:]
The sequence slice starts at 0, and 1 represents the countdown and the last.
Shoplist[1:3] represents starting from 1 sequence to 3 sequence
Shoplist[:] represents all sequences
Shoplist[:-1] represents all but the last sequence.
VI: Reference
When you create an object and assign a variable to it, this variable is only Reference that object, rather than representing the object itself! In other words, the variable name points to the memory of the object stored in your computer. This is called the name-to-object binding .
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/59/E6/wKioL1Tu_JLDdFEqAABORQIZLi4743.jpg "title=" parameter. png "alt=" Wkiol1tu_jlddfeqaaborqizli4743.jpg "/>
Variable one equals variable two. In fact, the variable two pointer to the object of the variable one, if the variable is changed, then the variable two will follow the change.
This article is from the "Slayer" blog, make sure to keep this source http://slayer.blog.51cto.com/4845839/1615528
Python data structure