2018-5-31-python Full Stack development day12-list, dictionaries, tuples

Source: Internet
Author: User
Tags shallow copy

1. List

The list is expressed in brackets, separated by commas. Lists can be nested in various types, such as strings, lists, tuples, dictionaries, can be nested indefinitely.

S1=[]

1.1 Index

s1=[' Alex ', [123],{123}, (123)]

V1=S1[0]---' Alex '

1.2 Slices

V2=S1[0:3]----[' Alex ', [123], {123}]

The slice is still left closed right open interval.

The list can be modified, which is an iterator that can be used for loops and while loops, which can exist in memory in a discontinuous sequence.

Example: s1[1]=222

Print (S1)

[' Alex ', 222,{123}, (123)]

1.3 To judge a list

S= ' Alex ' in S1

Print (s)-----

True

Where the first level element in the list is a whole and can only be judged if the whole is in the list

2. Operation of the list

2.1 Find the children of the elements in the list

s1=['Alex', [123,'aa','bb'],{ 123}, (123)]# find ' AA 'v=s1[1][1]print(v)

----AA

2.2 Converting a string to a list

Converts a string to a list that is internally implemented in a circular way

For example, "Alex" is converted to a list, then

  

s1='Alex'v1=[] for in S1:    v1.append (i)  Print(v1)

Output: [' A ', ' l ', ' e ', ' X ']

To take advantage of this function, it cannot be an int number, because the number int is not iterative.

2.3 Converting a list to a string

2.3.1 lists have both numbers and strings

Write for loop yourself--+ =

Only strings in the 2.3.2 list

      

s1=['ALEX','AA','BB']v =". Join (S1)print(V)

-----

Alexaabb

2.4 list.append () append something to the tail of the list

    

 s1=[ " alex  , "  a  , "  b   " ]v=s1.append (  " good morning   " )  print  (S1) 
-------
['Alex'a'b '  Good morning']

There is a bit different from the string, because the list can be modified directly, so, in the last example, if it is a string, you need to use another V to replace the S1, but for the list, directly modified, is on the basis of the original list, so v just append this action is completed, So the output is still S1.

2.5 Clear

To clear a list

    

s1=['Alex','a','b'] S1.clear ()print(S1)## # # # # # #[]

2.6 Copy, Shallow copy

Copy a list to another list

s1=['Alex','a','b']s2 =s1.copy ()print(S2)
######

['Alex'a'b' ]

Count 2.7 count to calculate the number of occurrences of an element

     

s1=['Alex','a','b']s2 =s1.count ('Alex')print(s2)# #1

2.8 Extend () expands an iterative object

Iterative things must be iterative, and internal for a for loop

    

s1=['Alex','a','b']s2=s1.extend ('Alex')Print(S1)#########['Alex','a','b','a','L','e','x']

2.9 Index finds the location of an element

      

s1=[' Alex ', ' A ', ' B ']s2=s1.index (' Alex ') print (s2)
1 # ###### 2 0

2.10 Insert Inserts an element at a specified location

    

1s1=['Alex','a','b']2S1.insert (2,'Alex')3 Print(S1)4 5 6 #########7['Alex','a','Alex','b']

2.11 Pop Deletes a value and gets

    

1s1=['Alex','a','b']2S2=s1.pop (0)3 Print(S2)4 Print(S1)5 6 #####7 Alex8['a','b']

If the pop is not followed by a value, the last value is deleted by default

2.12 Remove

Deletes an element directly, but does not get

2.13 Reverse reverses the order of the elements

      

s1=[11,22,33,444,55,66,]s2=s1.reverse ()#print (s2)print(S1)  ######[66, 55, 444, 33, 22, 11]

2.14 Sort

Organize the order of the lists

      

s1=[11,22,33,444,55,66,]s2=s1.sort ()#print (s2)print(s1 ### # # # # # #[11, 22, 33, 55, 66, 444]

Summary: The list is sequential and can be modified

3. Tuple tuples

Tuples are processed on the list so that they cannot be modified

You can also include anything within a tuple.

3.1 You can get a special value within a tuple by index

3.2 You can get special values within tuples by slicing them

3.3 Strings, lists, and tuples can be converted to each other

3.4 can be used for loops and can also iterate over objects

3.5 tuples are also orderly.

##########################

Tuple methods: Count and Index

4. Dictionary Dict

Keys: Keys

Value: Values

info={' K1 ': v1, ' K2 ': v2}

Value can be of any type

Key cannot be a list, nor can it be a dictionary

It can be a Boolean, but it cannot be a default value of 1, but not other conflicts, such as true.

Where the default value of False is None, the empty string ' ', empty tuple (). Empty list [], empty Dictionary, and 0. All the others are true

Dictionaries are unordered.

4.1 Items

  

info={'Alex': 123, 'a':'nishishei' ,'v': 555} for in Info.items ():     Print123555

4.2 Clear Erase

4.3copy Shallow Copy

4.4 Fromkeys (Key,value)

Where key can be an iterator, value is a fixed value, which means that all elements within an iterator are uniformly corresponding to the same value, forming a dictionary

info='Alex's1={}v2=s1.fromkeys (info,222)print (v2) {'a'l'e ' ' x ': 222}

4.5 get, returns value according to Key, if none is returned, the none can be modified

    

info={'Alex': 123, 'a':'nishishei' ,'v': 555}#info= ' Alex 'v2= Info.get (567,222)print(v2)------222

Because there is no key567, so return parameter, 222

4.6 Pop (key, return default value)

If there is no key, the default value is returned

If there is, then delete and get

4.7 Popitem ()

Random deletion of a bunch of key-value pairs

4.8 SetDefault (Key,value)

If the key is in the dictionary, the value corresponding to this key is obtained, and if not, the key and value are created.

4.9update (Key,value)

Existing keys are modified and no additions are made.

     

2018-5-31-python Full Stack development day12-list, dictionaries, tuples

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.