1. Characters force everything
If it is a string for coercion, just put quotation marks on both sides of the original data type
2.list: force conversion to a list
""" If it is a string, each character will be put into the new list separately as an element If it is a dictionary, only the keys are retained to form a new list. If it is another container, just simply put on both sides of the original data[] """ res = list(tuplevar) res = list(setvar) res = list(strvar) res = list(dictvar) # Convert the dictionary to a list and only keep the keys print(res,type(res))
3.tuple: force conversion to tuple
""" If it is a string, each character will be put into a new tuple separately as an element If it is a dictionary, only the keys are kept to form a new set of tuples. If it is another container, just simply put on both sides of the original data () """ res = tuple (listvar) res = tuple(strvar) res = tuple(dictvar) # Convert the dictionary into a tuple and only keep the keys # res = tuple(intvar) error Cannot convert integer print(res)
4.set: forced to transform into set disorder, de-duplication
""" If it is a string, each character will be put into the new set separately as an element, [there is disorder, de-duplication features] If it is a dictionary, only the keys are retained to form a new set. [There is a feature of disorder and deduplication] If it is another container, just simply put on both sides of the original data {} [there is disorder, de-duplication features] """ res = set(strvar) res = set (listvar) res = set(dictvar) print(res)
5. Dictionary strong transfer
# (1) The outer layer is a list, and the container inside can be a list or tuple (recommended) lst = [["a",1],("b",2)] res = dict(lst) print(res)#{'a': 1, 'b': 2}
The above is the whole content of this article, I hope it will be helpful to everyone's study, and I hope you can support it.