List python learning, tuples, sets, dictionaries Essays

data structure

A, [] a list of operations list as follows:

 List is a variable sequence, usually used to store a collection of similar projects.
 
list_one = [1, 2, 3, 4, True, False, 'pig', 1, 1, 1, 1, 0, 0]
list_two = [1, 8, 10, 50, 400, 1000, 600, 2, 3, 99]
 
# 1, additive elements, one element at the end of the list
list_one.append('U')
print(list_one)

# 2, an expanded list, use all the elements in iterable be extended
list_one.extend(list_one)
print(list_one)

# 3, is inserted, the insertion element to a specified position
list_one.insert(1, 'A')
print(list_one)

# 4, remove, remove the first value in the list, if not throw ValueError exception
list_one.remove('A')
print(list_one)

# 5, delete, delete to the elements in a given position, if not given the default position on the list the last element deleted
Elements list_one.pop (1) # given position, the position is to develop deleted
print(list_one)
list_one.pop () # not a given position, the last element to delete default list
print(list_one)

# 6, empty, empty the list of all the elements
list_one.clear()
print(list_one)
 
# 7, index, return the zero-based index of the first element in the list
print(list_one.index(2))
print(list_one.index('pig', 6))

# 8, the number of elements appear
# True and False in the list represents the 1,0
print(list_one.count(0))

# 10, sort, sort the list in the list
Does not support sorting between # str type and an int
list_two.sort () # positive sequence does not pass sort parameters
print(list_two)
list_two.sort (reverse = False) # reverse = False Positive sequence ordering
print(list_two)
list_two.sort (reverse = True) # reverse = True decommitment
print(list_two)

# 11, reverse elements in the list
list_one.reverse()
print(list_one)

# 12, copy, shallow copy
list_tree = list_one.copy()
print(list_tree)
 

# List comprehensions create a new list
# Equation: [formula loop for determining if conditions]
squares = []
for i in range(5):
    squares.append(i)
print(squares)

list_tour = [j+1 for j in range(5)]
print(list_tour)

list_five = [(x, y) for x in squares for y in list_tour if x != y]
print(list_five)
 
# Del statement, del statement to remove slices from a list or clear the entire list
del list_one [0] # removes an element
print(list_one)
del list_one [4: 7] # removed under standard element 4-7
print(list_one)
del list_one [:] # remove the entire list of elements
print(list_one)
del list_one # delete the entire variable list_one
 

Second, the method of operation] [tuple tuple as follows:

Tuples are immutable sequence, usually used to store a number of sets of heterogeneous data 
 
# 1, a tuple is a number of values ​​are well separated, for instance:
tuple_one = 1234, 5463, 888
print('tuple_one类型:{}'.format(type(tuple_one)))
 
# Tuples are immutable, not allowed to modify the value inside
# Tuples comprising parentheses always be output again when, in order to correctly represent the tuple
# Empty tuple can be used directly to create a pair of parentheses
# Tuple contains one element may be constructed by adding a comma after this element
tuple_two = (1, 2, 'hello') # tuple
print('tuple_two类型:{}'.format(type(tuple_two)))
tuple_three = () # empty tuple
print('tuple_three类型:{}'.format(type(tuple_three)))
tuple_four = ( 'hello baby',) # tuple
print('tuple_four类型:{}'.format(type(tuple_four)))
 
# 2, the value tuples used directly to index and slice values
print(tuple_one[0])
print(tuple_one[:2])
print(tuple_one[:])
 

Third, the method of operating a set of [] set as follows:

# 1, sets are unordered set by the non-repetition of elements, it will detect and eliminate members of repetitive elements
basket = {'hello world', 'apple', 'orange', 'banana', 'orange'}
print ( 'basket type {}:'. format (type (basket)))

# Create a set of curly braces and set (), should the empty set can only be used to create a set (), {} can not be used to create, which is to create an empty dictionary
# Collection set () can only put a string (str) types of elements
gather_one = set()
print('gather_one类型:{}'.format(type(gather_one)))
gather_two = set('hello')
print('gather_two类型:{}'.format(type(gather_two)))

# Create a collection set of derivations
gather_three = {z for z in 'abcdefghijk'}
print(gather_three)
 

Fourth, the operation of the dictionary of [] Dictionary as follows:

 
# Dictionary key can make almost any value, but the key is the dictionary immutable
# Create a dictionary, the dictionary can be separated by a comma key: value list contained within the curly braces to create, can also be created by dict constructor.
dict_one = { 'jack': 4098, 'sent': 4127}
print(dict_one)
dict_two = {4098: 'jack', 4127 'sent'}
print(dict_two)
dict_three = dict (one = 1, wto = 2, three = 3) # constructor creates Dictionary
print(dict_three)
dict_four = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
print(dict_four)
dict_five = dict({'one': 1, 'two': 2, 'three': 3})
print(dict_five)
dict_six = {} # Create an empty dictionary
print(dict_six)
 
print (list (dict_one)) # returns the dictionary used dict_one list of all the keys.
print (len (dict_one)) # Returns the number of entries in the dictionary dict_one
print (dict_one [ 'jack']) # Returns the value of the dictionary dict_one 'jack', and if the key does not exist will throw KeyError
dict_one [ 'jack'] = 'hello' # dict_one modified value 'jack' in
print(dict_one)
print (dict_one.copy ()) # shallow copy dict_one dictionary
# Value (dict_one.get ( 'jack')) take the dictionary print, if present, is the return value, it returns the default value does not exist, if the default is not given, the default I None
dict_two.clear () # Empty dictionary
print(dict_two)
del dict_five [ 'one'] # dict_five in the 'one', is removed from the dict_one, if there is no return KeyError
print(dict_five)
print (dict_four.items ()) # Returns a new view of dictionary entries ((key, value) pairs) consisting of
print (dict_four.keys ()) # returns a new view of the composition of the dictionary key
print (dict_four.values ​​()) # Returns a new view of dictionary values ​​consisting of
 
# Dictionary comprehension to create the dictionary
dict_seven = {x: y for x in [1, 2, 3, 4] for y in [4, 5, 6, 7]}
print(dict_seven)
 
 

Guess you like

Origin www.cnblogs.com/lifeng0402/p/11788760.html