For Python sharding,

Source: Internet
Author: User

For Python sharding,

Just in the Forum python version http://bbs.byr.cn /#! Article/Python/1693 solves a python sharding problem.

 

 

Problem:

UesrList = ['1', '2', '3', '4', '5', '6'] subList1 = uesrList [2: 5] subList2 = uesrList [-3: -1] subList3 = uesrList [0:-2] subList4 = uesrList [0:-3] subList5 = uesrList [-3:0] print (subList1) print (subList2) print (subList3) print (subList4) print (subList5) # output ['3', '4', '5'] ['4', '5'] ['1 ', '2', '3', '4'] ['1', '2', '3'] []

Why [] is returned? Why is the partition result like this?

 

 

Cause:

 

We often see [a: B] sharding operations on the list. In fact, python has three parts, for example, [a: B: c] where a, B, and c can be omitted.

However, c defaults to 1. If you do not explicitly set it to a negative number, it will traverse from the front to the back (from left to right.

 

For example, [-2:]

The value is 5 at the position of index-2, which goes from left to right by default. Therefore, the result is from 5. The result will output ['5', '6']

If c is negative, the order is the opposite, from right to left. For example, [-2:-1] is ['5', '4', '3 ', '2', '1']

 

Therefore, you only need to locate the position based on the index, and then traverse to the specified position in order.

 

Note:

During traversal, the position of the second index (B) must beSequence directionIf it is [-1:-2], press

If the image is not found from left to right, an empty list is returned.

 

Therefore, any [a: 0] is an empty list, or the index 0 is the first one, but it cannot be calculated (the partition operation includes [] and the left side does not include the right side,

Either it is because the element after index 0 cannot find the first element in the order from left to right.

 

 

 

 

 

 

 

 


 

 




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.