Python data types have a lot of knowledge in common applications and need to be learned in detail. The following describes the related technical issues. Set in Python data type and collection in C #) is not a concept, which is a translation problem.
A set in Python is an unordered, non-repeating element set. Similar to the Set Concept in mathematics, a set can perform logical operations such as intersection, addition, difference, and population.
Common Set Syntax:
- s = set(['a', 'b', 'c'])
However, set has undergone major changes in Python 3.0, and the syntax for creating a set is changed to: s = {1, 2, 3}. Use the method of curly arc, similar to the dict mentioned later.
If repeated elements are input in the set, the set automatically merges them. This feature is very useful. For example, it is very efficient to remove a large number of duplicate elements from the list and use set. The example is as follows:
- #-*-Coding: UTF-8 -*-
- A = [11,22, 33,44, 11,22, 11,11, 22,22, 33,33, 33]
- B = set ()
- Print B
- # Output set ([33, 11, 44, 22])
In another example, the code is as follows:
- #-*-Coding: UTF-8 -*-
- A = ["11", "22", "33"]
- B = ["11", "33"]
- C = set (a) & set (B)
- Print c
- # Output set (['11', '33'])
Think about how you can write this algorithm by yourself? Then you can find two larger lists to compare the efficiency with the set implementation. Use set in the program later.
At present, C # Collections does not seem to have Set yet, but C ++ STL does. I don't know why C # Doesn't implement this interesting thing.
Dictionary (dict)
Those who have used Collections in C # should be familiar with Hashtable, And the hash table in Python data type is the dictionary dict. Similar to set, a dictionary is an unordered storage structure, which includes the key of a keyword) and the value corresponding to a keyword ).
- How to bind a C ++ program to Python
- Python display UTF-8 Chinese Text specific operation method
- Main features of Python objects
- Summary of Python experience
- Python scripts solve difficulties in Game Development
C # what programmers need to know is that dict is a built-in data type in Python, defined as: dictionary = {key: value }, when multiple key-value pairs exist, use commas to separate them.
The keywords in the dictionary are of an unchangeable type, such as strings, integers, tuples containing only unchangeable objects, and lists. A key in the dictionary can only be associated with one value. For the same key, the added value overwrites the previous value.
People who have learned the data structure should have a certain understanding of the Efficiency of dictionary hash search. Therefore, I suggest using dictionaries whenever possible, and not writing any other words. For the main methods provided by the dict type and list, tuple, and set in Python data types, refer to various books dedicated to Python. Most of them will provide a detailed list of methods.