PythonThe Data Types of tuples, lists, and dictionaries are very Python (there python isAdjective. These structures are optimized enough, so if they are used well, they will be of great benefit in some areas.
Tuples
In my opinion, just like the Java array, the tuples in Python have the following features:
- An ordered set of any object. This article does not cover the same type of array;
- Read by offset;
- Once generated, it cannot be changed;
- Fixed Length, supporting nesting
For example:
Python Code
-
- >>> (0, 'hahaha', (4j, 'y '))
-
- (0, 'hahaha', (4j, 'y '))
-
- >>> T = (1, 3, 'B ')
-
- >>> T [2]
- 'B'
-
- >>> T [3]
-
-
- Traceback (most recent call last ):
-
- File"#41> ", line 1, in <module> </module>
-
- T [3]
-
- Indexerror:Tuple IndexOutRange
-
- >>> T [-1]
-
- 'B'
- >>> T [0:-1]
-
- (1, 3)
-
- >>> T * 2
-
- (1, 3, 'B', 1, 3, 'B ')
-
- >>>ForXInT:
-
- PrintX,
-
-
-
- 1 3 B
- >>> 'B'InT
-
- True
-
- >>> Q = T + (3, 'abc '))
-
- >>> Q
-
- (1, 3, 'B', 3, 'abc ')
-
- >>>ForXIn(2, (3, 'A ')):
-
- PrintX
-
-
- 2
-
- (3, 'A ')
-
- >>>Len(Q)
-
- 5
-
- >>>Len(2, (3, 'abc ')))
-
- 2
-
- >>> (1, 2, 3) [1]
-
- 2
-
- >>> Q [1] = 'D'
-
- Traceback (most recent call last ):
-
- File"#57> ", line 1, in <module> </module>
-
- Q [1] = 'D'
-
- Typeerror:'Tuple'ObjectDoesNotSupport item assignment
-
- >>> A = ('B', 'C', q)
- >>> 1InA
-
- False
-
- >>> QInA
-
- True
-
- >>>
-
- ('B', 'C', (1, 3, 'B', 3, 'abc '))
-
- >>> Q = 'D'
-
- >>>
-
- ('B', 'C', (1, 3, 'B', 3, 'abc '))
The above example is enough to illustrate the majority. The most important thing when using tuples is "Once generated, it will not be changed ".
List
The list is likeCollection, which has more features than tuples and is more flexible.Character is summarized as follows:
- An ordered set of any object;
- You can use offset access. Note that the elements in the list are variable, which is different from the tuples;
- Variable Length, supports nesting;
- There are also some object reference mechanisms similar to Java
These features make the list widely used in practical applications. The following are some examples.
1) The first is basic usage.
Python code
-
- >>> L = ['A', 'B', 'C']
- >>>Len(L)
-
- 3
-
- >>> L + ['D']
-
- ['A', 'B', 'C', 'D']
-
- >>> L * 2
-
- ['A', 'B', 'C', 'A', 'B', 'B']
-
- >>>ForXInL:
-
- PrintX,
-
-
- A B C
2) index and partition, assign values (assign values to a single element, assign values to a partition)
Python code
-
- >>> L = ['abc ','Def', 'Ghi', 123]
-
- >>> L [2]
-
- 'Ghi'
-
- >>> L [-3]
- 'Def'
-
- >>> L [: 3]
-
- ['Abc ','Def', 'Ghi']
-
- >>> L [1] = 'hahaha'
-
- >>> L
-
- ['Abc', 'hahaha', 'ghi', 123]
-
- >>> L [1:] = ['apple', 'Banana ']
- >>> L
-
- ['Abc', 'apple', 'bana']
-
- >>> L [2] = [123,345,456]
-
- >>> L
-
- ['Abc', 'apple ', [123,345,456]
-
- >>> L [1:] = [1, 123,234,345,456,567]
-
- >>> L
-
- ['AB', 123,234,345,456,567]
3) add, sort, and delete operations
Python code
- >>> L = ['abc ','Def', 'Ghi', 123]
-
- >>> L.Append(456)
-
- >>> L
-
- ['Abc ','Def', 'Ghi', 123,456]
-
- >>> L. Sort ()
-
- >>> L
-
- [123,456, 'abc ','Def', 'Ghi']
- >>>DelL [0]
-
- >>> L
-
- [456, 'abc ','Def', 'Ghi']
-
- >>>DelL [2:]
-
- >>> L
-
- [456, 'abc']
4) some interesting usage (from the Forum ID-Coffee dancer)
Remove the spaces at the beginning and end of each element in the list:
Python code
- >>> Freshfruit = ['bana', 'login', 'passion fruit']
- >>> [Str. Strip ()For Str InFreshfruit]
- ['Bana', 'loganberry ', 'passion fruit']
Multiply the element greater than 3 in the list by 2:
Python code
- >>> VEC = [2, 4, 6]
- >>> [2 * xForXInVECIfX> 3]
- [8, 12]
Multiply each element in List 1 and each element in List 2:
Python code
- >> lst1 = [2, 4, 6]
- >> lst2 = [4, 3, -9]
- >>> [x * Y for x in lst1 for Y in lst2]
- [8, 6,-18, 16, 12,-36, 24, 18, -54]
Obtain the square value of [0-10:
Python code
- [X ** 2ForXIn Range(10)]
Dictionary
The dictionary in python is like a hashmap in Java. It exists and operates as a key-value pair. Its features are as follows:
- Key-based access, rather than offset;
- Key-value pairs are unordered;
- Keys and values can be arbitrary objects;
- Variable Length, any nesting;
- In the dictionary, sequence operations are not allowed. Although the dictionary is similar to the list in some aspects, do not set the list on the dictionary.
1) basic operations
Python code
- >>> Table = {'abc': 1 ,'Def': 2, 'ghi': 3}
-
- >>> Table ['abc']
-
- 1
-
- >>>Len(Table)
-
- 3
-
- >>> Table.Keys()
-
- ['Abc', 'ghi ','Def']
-
- >>> Table.Values()
- [1, 3, 2]
-
- >>> Table.Has_key('Def')
-
- True
-
- >>> Table.Items()
-
- [('Abc', 1), ('ghi', 3 ),('Def', 2)]
2) modify, delete, and add
Python code
-
- >>> Table = {'abc': 1 ,'Def': 2, 'ghi': 3}
- >>> Table ['ghi'] = ('G', 'h', 'I ')
-
- >>> Table
-
- {'Abc': 1, 'ghi': ('G', 'h', 'I '),'Def': 2}
-
- >>>DelTable ['abc']
-
- >>> Table
-
- {'Ghi': ('G', 'h', 'I '),'Def': 2}
-
- >>> Table ['xyz'] = ['x', 'y', 'z']
- >>> Table
-
- {'Xyz': ['x', 'y', 'z'], 'ghi': ('G', 'h', 'I '),'Def': 2}
Here, you need to define a new key-value pair for the dictionary extension. For the list, you can only use the append method or multipart assignment.
3) dictionary Traversal
Python code
-
- >>> Table = {'abc': 1 ,'Def': 2, 'ghi': 3}
-
- >>>ForKeyInTable.Keys():
- PrintKey, '\ t', table [Key]
-
-
-
- ABC 1
-
- Ghi 3
-
- Def2
File
Compared with the file class in Java, the file class in Python must be narrower.
1) file writing
Python code
- >>> Myfile =Open('Myfile', 'w ')
- >>> Myfile.Write('Hello world \ n ')
- >>> Myfile.Close()
A python open statement opens a file (a new file is automatically created when the specified file does not exist ). The first parameter of open is the file name, and the second parameter is the operation mode. The so-called operation mode is what you use to open a file and read the file, or write (of course, the operation mode is not only read and write ). There is another thing to remember after the operation.
2) File Reading
Python code
- >>> Myfile =Open('Myfile', 'R ')
- >>> Myfile.ReadlineReadline()
- 'Hello world \ N'
It is very simple. In this case, the two sentences are nested with a long string of Java streams. Of course, it makes sense to do that in Java.
Okay, I learned a lot. To be honest, there are really few Python cores, and it's very easy. It's hard to know how to use python at the same time.