There are many problems to solve when using Python arrays. The following is one of the three Special Data Types of Python arrays in List. I hope you will always be helpful when using Python arrays. Next, let's take a detailed look.
- Nuances between Python tutorials and C #
- Introduction to arithmetic operations and arithmetic expressions in Python
- How to solve the problem through Python Encoding
- Python if application details
- Applications of whlie and for in Python loop statements
Similar to arrays, it supports direct use of the access method of the underlying object. The size of the List is variable, which is similar to that of ArrayList. However, a List is like an all-encompassing kaleidoscope. It can not only store objects, but also store basic data types, such as integer types. This is more powerful than the loading capability of Java ArrayList. in Java, ArrayList cannot store basic types. In addition to the strong ability to receive and package items, the flexibility of operations on items in the List is even more exciting. For example, if an array contains an integer series, we want to select a number larger than a certain number in the array to form a new series. In Java, I am afraid I have to do this. First construct a class and then create a method in the class:
- public static int[] select(int[] source, int bound) {
- int[] result = new int[source.length];
- int j = 0;
- for (int i = 0; i < source.length; i++) {
- if (source[i] > bound) {
- result[j] = source[i];
- j++;
- }
- }
- return source;
- }
Looking at this code, do you feel uncomfortable? This is a very simple thing. How do I need so much code and prepare more space for initialization of the returned array? If the real parameter is not an int array, what about double type? I'm afraid I have to write another method. So what should we do in the Python array? Very simple:
- [Item for item in source if item> bound] (Note: source is a List)
Are you surprised? OK, calm down, that's simple. No matter whether the source is int, double, or even string, this line of simple code can solve the problem for you. Because Python can easily process such tasks, we do not need to encapsulate them into a module.
In addition to these three types of special data, I am amazed at the ability of Python arrays to process strings. If you want to print a String multiple times, do you want to write your Java code like this:
- for (int i = 0; i < times; i++)
- System.out.print("Hello ");
In Python, you only need to: print "Hello" * 5. Oh, the '*' operator is used on the String, and the code becomes simple and easy to understand. In Python, the string type has a powerful function, that is, join (). The join function can receive ordered variables, such as Dictionary, List, Tuple, and String. Example:
- >>>"+".join("123")
- '1+2+3'
- >>> ";".join(["first", "second", "third"])
- 'first;second;third'
I am deeply fascinated by the simplicity of the Python array, and she does not understand the boring code. The disturbing code is wonderful because of her existence. Do you feel it?