Slicing and iterating of Python

Source: Internet
Author: User

Taking a partial element of a list or tuple is a very common operation.

N=[] (n[]n[])

When fetching multiple elements, you can use Python's own slices (slice)

N=[] (n[:])
[123, 234]

N[0:2] indicates that starting at index 0, until index 2, but not index 2, that is, index 0, 1, exactly 2 elements, if the first element is 0 can also be omitted

Print (N[:2])

You can also start with index 1 and take two elements

N=[] (n[:])
[234, 456]

The index of the first element of the countdown is-1

It is useful to manipulate slices by first creating a 0-99 sequence:

L= (()) (L) (l[:])

Take the last 10

L= (()) (l[-:])
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

All numbers, every 5 fetch one, at "Start: End: Step"

L= (()) (l[::])
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]

Copy a list

l[:]

The tuple is also a list, but the tuple is immutable, so the tuple can also be sliced, the result is still a tuple

Print ((1,2,3,4,5,6) [: 3])
(1,2,3,4,5,6) is a slice operation for a tuple [: 3]

A string can also be considered a list, each element is a string, so the string can also be used for slicing, the result is still a string

([:]) ([::])

Iteration

Given a list or tuple, we can iterate with a for loop, which is called an iteration.

List this data type although there is subscript, but many other data types are not subscript, but as long as the object can be iterated, whether or not subscript, can iterate, such as dict can iterate

Because dict are not ordered in list order, the resulting order of the iterations is likely to be different. By default, the dict iteration is key, and if you want to iterate over value, you can use the

For value in D.values (): If you want to iterate both key and value at the same time, you can use for k,v in D.items ().

Strings can also iterate


So for a for loop, as long as it works on an iterative object, the for will work.

A method of judging whether an object can iterate

of the Collections module

Iterable type

To implement the subscript loop for a list

The enumerate function can turn a list into an index-element pair so that both the index and the element itself can be iterated in the For loop.

Ivalue ([]): (ivalue)

The above loop, which references two variables at the same time, is common in Python, and the following example



Slicing and iterating of Python

Related Article

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.