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.