python data type _ tuple dictionary common operation method of introduction

  • 2020-06-03 07:03:08
  • OfStack

tuples

Python's tuples are similar to lists, except that the elements of the tuples cannot be modified.

Tuples use curly braces and lists use square brackets.

Creating tuples is as simple as adding elements in parentheses and separating them with commas.


tp=(1,2,3,'a','b')
a = 'hello world'  # So this is defined as str type 
b = ('hello world') # When defining tuples, if only 1 Three elements. So b The type of str
c = ('hello world',)
print(type(c))

Tuples have only count and index methods, as follows:


tp = ('127.0.0.1', '3306', 'b', 'a', '3306', 1, 2, 3, 1, 44)
print(tp[0])       # You can also evaluate it by subscript 
print(tp.count('3306'))  # Finds the number of times an element appears in a tuple 
print(tp.index('a'))   # Find the index location of the element 

Cast of tuples:


lis = [1, 2, 3, 4, 5, 'b', 'c']
new_lis = tuple(lis) # will list Cast type to tuple
print(type(new_lis))

int(), str(), float(), list(), tuple()

The dictionary

Dictionaries are another variable container model and can store any type of object. Dictionaries store unordered data.

Each key value of the dictionary (key= > value) pairs are separated by a colon (:), each pair is separated by a comma (,), and the entire dictionary is enclosed in curly braces ({}). key in the dictionary is not repeatable and is formatted as follows:


d = {key1 : value1, key2 : value2 }

Get the dictionary element as follows:


# The dictionary , To define the key Can't repeat 
info = {'name': 'xiaoming', 'sex': 'man', 'age': 20, 'id': 1}
#2 The difference between two methods: If passed in key Nonexistent. Pass [] The value will report an error; through get Return value None
print(info['name'])   # The dictionary values, pass key Take out the corresponding value
print(info.get('name')) # You can also go through get Method acquisition, get(key)
print(info.get('addr', 'beijing')) # If passed in key Does not exist, the default value passed in is returned: beijing;  If you don't write it, you can't get it key , the return None

Add dictionary elements as follows:


# Add elements 
info['addr'] = 'beijing' # through [key] = value Method can add elements, if key Nonexistent, add 
print(info)
info.setdefault('phone','13000000000') # You can also go through setdefault(key,value)  Method add element 
print(info)

Modify the elements in the dictionary by:


info['id'] = 7 # if key Exists, then modify that key The corresponding value ; if key Does not exist, the method adds an element 
print(info)

Delete the elements in the dictionary as follows:


# delete 
del info['addr'] # Remove elements 
info.pop('addr') # The dictionary is unordered and is passed in to be deleted key, pop Method will return deleted key The corresponding value
print(info)
#info.pop('kk') # If you delete key If it does not exist, report an error: KeyError: 'kk'
info.clear()   # Empty dictionary 
info.popitem()  # Random delete 1 Four elements, not very useful 

The dictionary loops through the elements as follows:


info = {'name': 'xiaoming', 'sex': 'man', 'age': 20, 'id': 1}
print(info.keys())  # Get all of the dictionary key, Execution Results: dict_keys(['name', 'age', 'sex', 'id'])
print(info.values()) # Get all of the dictionary value , execution results: dict_values(['xiaoming', 20, 'man', 1])
print(info.items()) # Print the results for all key , value, Loop traversal lasts for use, and the execution result is: dict_items([('name', 'xiaoming'), ('age', 20), ('sex', 'man'), ('id', 1)])
# The dictionary loops through 
for k, v in info.items():
  print('key: %s value: %s' % (k, v))
 Execution Results: 
key: age value: 20
key: sex value: man
key: name value: xiaoming
key: id value: 1

update for dictionaries:


info = {'a': 'xiaoming', 'sex': 'man', 'age': 20, 'id': 1}
info2 = {'a':1,'b':2}
info.update(info2) # Merge the two dictionaries together 1 One, if there is 1 The sample of key , then update value, Execution Results: {'sex': 'man', 'id': 1, 'b': 2, 'age': 20, 'a': 1}
print(info)

Exercise: Define a dictionary, modify the value in the dictionary, and change the corresponding yellow to green, as follows:


tp = ('127.0.0.1', '3306', 'b', 'a', '3306', 1, 2, 3, 1, 44)
print(tp[0])       # You can also evaluate it by subscript 
print(tp.count('3306'))  # Finds the number of times an element appears in a tuple 
print(tp.index('a'))   # Find the index location of the element 
0

The code is as follows:


tp = ('127.0.0.1', '3306', 'b', 'a', '3306', 1, 2, 3, 1, 44)
print(tp[0])       # You can also evaluate it by subscript 
print(tp.count('3306'))  # Finds the number of times an element appears in a tuple 
print(tp.index('a'))   # Find the index location of the element 
1

Related articles: