Application manual of Python array in List

Source: Internet
Author: User

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:

 
 
  1. public static int[] select(int[] source, int bound) {  
  2. int[] result = new int[source.length];  
  3. int j = 0;  
  4. for (int i = 0; i < source.length; i++) {  
  5. if (source[i] > bound) {  
  6. result[j] = source[i];  
  7. j++;  
  8. }  
  9. }  
  10. 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:

 
 
  1. [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:

 
 
  1. for (int i = 0; i < times; i++)  
  2. 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:

 
 
  1. >>>"+".join("123")  
  2. '1+2+3'   
  3. >>> ";".join(["first", "second", "third"])  
  4. '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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.