List:
A = [' A ', ' B ', ' C ', ' abc ', 1, 2, 3]
Print a
# append is added by default in the last
A.append (4)
Print a
Output: [' A ', ' B ', ' C ', ' abc ', 1, 2, 3, 4]
# The Index method is able to see what the subscript index of the 4 element is in the list
Print (A.index (4))
Output: 7
# Use the Insert method to insert elements into the list
A.insert (2, ' hh ')
Print a
Output: [' A ', ' B ', ' hh ', ' C ', ' abc ', 1, 2, 3, 4]
# default Delete Last one, can add subscript delete
A.pop (7)
Print a
Output: [' A ', ' B ', ' hh ', ' C ', ' abc ', 1, 2, 4]
# Use the Remove method to remove the element a from the list
A.remove (' a ')
Print a
Output: [' B ', ' hh ', ' C ', ' abc ', 1, 2, 4]
# Use the Sort method to sort the list, but the list needs to be full of numbers or strings, not both numbers and strings
b = [1, 2, 8, 9, 5, 6, 7]
B.sort ()
Print (b)
Output: [1, 2, 5, 6, 7, 8, 9]
# Use Reverse to sort the list in reverse order
B.reverse ()
Print (b)
Output: [9, 8, 7, 6, 5, 2, 1]
String, which can be indexed and sliced, the string is immutable and can be iterated
A= ' ABCD '
A[0] for a
A[2] for C
A[0:2] for AB (starting from 0 to take 2, omitting 0 for starting from the first, omitting the last for direct fetch to the last one)
A[0:4:2] for ad (starting from 0, take 4th, step 2)
Lists [], mutable types of data structures that can be added to delete and change values in the list, can iterate over the
List1=[' A ', 1, (1,), [' Hello ', ' world ']
LIST1[1] is 1
List1.append (' 123 ') appends an object to a list
List1.instert (1, ' abc ') (add an ABC object to the position labeled 1 in the List1 list)
Del can delete strings, lists, tuples, etc.
Del List1[4] (delete the fourth element in the List1 list)
list1.remove (1) (the first 1 element in the list is removed)
List1.reverse () (reverses the entire list of List1)
List1.pop () (do not add subscript by default from the last delete, add subscript delete the subscript element, delete will output deleted element content)
list1.extend (' ABCD ') (append a,b,c,d four elements to a list)
Zip (list1,list2) (Combine list 1 and List 2 into a new list)
Operations on strings can also be manipulated against a list
An iterative content can be accessed through a for loop
Meta-group
You can use indexes and slices, which are the same as strings, are immutable, and can be iterated
t= (' A ', 1, (2,)) (tuples can be stored in strings, numbers, variables and tuples, etc.; tuples must be added, which is the tuple)
First,second,third=t (This method can be used to accept a value from a tuple, also called a split of a tuple)
# Use Count to view the number of an element
Tuple1= (1, 2, 3, ' abc ', ' 789 ')
Print (Tuple1.count (3))
Output: 1
# Use the index method to see the subscript of an element
Print (Tuple1.index (' 789 '))
Output: 4
Dictionary
Dictionary {}
Dictionary is the only mapping type (hash table) in Python
Dictionary objects are mutable, but dictionary keys must use immutable objects and key values cannot be duplicated, and different types of key values can be used in a dictionary
Dic1 = {' A ': 1, 1:123, (1,): ' ABC '}
DIC[1] to access the elements in the dictionary for 123
Len (DIC1) See how many elements are in the dictionary
Dic1.keys () View all the key values in the Dic1 dictionary
Dic1.get (' a ') returns the value of an element with a key value of 1
' A ' in Dic1 You can use in to determine if the key value is in the dictionary
Dic1.has_key (' a ') ibid .
Dic1.values () returns all elements in the Dic1
Dic1.items () Save the key and value in the dictionary to a tuple and save the tuple as a list
Dic1.iteritems () with the items () method, returns the key and value in the dictionary as an object
Dic2 = dic1.copy () Copy
Dic1.pop (1) Delete the key and value of key 1 in the dictionary and return value, if key does not exist, returns Keyerror
dict (Zip (List1, list2)) You can create a merged list as a dictionary
Dict ([' A ', 1],[' B '],2) can create a dictionary directly
dict (a=1,b=2) dictionaries can be created directly
Dic.fromkeys (range), (+) Create dictionary, value can be none
For K, V in Dic1: use for traversal of key and value in dictionary
Print "%s,%s"% (k, v)
Dictionary Exercises
#!/usr/bin/python
#coding =utf-8
DIC = {}
Name = Raw_input ("Please input your name:")
Age = raw_input ("Please input your Age:")
Xingbie = raw_input ("Please Enter your gender: (m/w)")
Time.sleep ()
dic[' name '] = Name
dic[' age ') = Age
dic[' Xingbie '] =xingbie
Print dic
A1={' A ': 1, ' B ': 2, ' C ': 3}
Print (A1)
# Use the Get method to get the value of key in the dictionary, or none if the key in the dictionary does not have the corresponding value
Print (A1.get (' a '))
Output: 1
# using the SetDefault method, you can also get the value of key in the dictionary, if the key in the dictionary does not have the corresponding value, then none will be displayed, but you can add the parameter later, if there is no value, the following argument will be displayed.
Print (A1.setdefault (' C '))
Output: 3
Print (A1.setdefault (' d ', 123))
Output: 123
# Use the keys method to get all the key values in the dictionary
Print (A1.keys ())
Output: Dict_keys ([' B ', ' A ', ' C '])
# Use the values method to get all the values in the dictionary
Print (A1.values ())
Output: Dict_values ([1, 2, 3])
# may consume more memory in actual operation
Dic1.items () Save the key and value in the dictionary to a tuple and save the tuple as a list
# The value taken is an object that must be traversed before the object's value can be fetched
Dic1.iteritems () with the items () method, returns the key and value in the dictionary as an object
# using Formkeys, you can use the L list as the key value of the N dictionary, and the following argument as the value of the N dictionary
L = [' A ', ' B ', ' C ']
m = {}
n = M.fromkeys (l, 789)
Print (n)
# Overlay two lists into a dictionary using the Zip method
This article is from the "Coarse bread" blog, make sure to keep this source http://culiangmianbao.blog.51cto.com/10475024/1975420
Python list, tuples, dictionaries