With the old Ziko python for Loop statement _python

Source: Internet
Author: User
Tags zip

Talk less, work on.

For basic operations

A for is a loop that reads an element sequentially from an object. Look at the example below and use the For loop for the data objects you've learned to see which ones are available and which cannot be used. It is also a review of past content.

Copy Code code as follows:

>>> name_str = "Qiwsir"
>>> for I in Name_str: #可以对str使用for循环
... print I,
...
Q I w s i R

>>> name_list = List (NAME_STR)
>>> name_list
[' Q ', ' I ', ' W ', ' s ', ' I ', ' R ']
>>> for I in Name_list: #对list也能用
... print I,
...
Q I w s i R

>>> Name_set = set (NAME_STR) #set还可以用
>>> Name_set
Set ([' Q ', ' I ', ' s ', ' R ', ' W '])
>>> for I in Name_set:
... print I,
...
Q i s R w

>>> name_tuple = tuple (NAME_STR)
>>> Name_tuple
(' Q ', ' I ', ' W ', ' s ', ' I ', ' R ')
>>> for I in Name_tuple: #tuple也能呀
... print I,
...
Q I w s i R

>>> name_dict={"name": "Qiwsir", "Lang": "Python", "website": "Qiwsir.github.io"}
>>> for I in Name_dict: #dict也不例外
... print i, "-->", Name_dict[i]
...
Lang--> python
Website--> Qiwsir.github.io
Name--> Qiwsir

In addition to the above data type, the file can also be used for, which is preceded by a special "don't red-headed" two articles on how to use for to read the contents of a file object. If the reader forgets, can go to browse.

For in list resolution, use also can not be underestimated, this explanation in the list of the time, has been explained, but, or review for good, the so-called study and often review, not also haha.

Copy Code code as follows:

>>> one = range (1,9)
>>> One
[1, 2, 3, 4, 5, 6, 7, 8]
>>> [x for x in one if x%2==0]
[2, 4, 6, 8]

Do not say anything, list resolution of the strong, in later learning will be more and more experience, admire admire AH.

If you use Python3, you will find that the dictionary parsing, tuple parsing is also wonderful AH.

To go up a notch, you have to generalize. To sum up the for loop described above, the following figure shows:

Please enter a picture description

To express in a literal:

Copy Code code as follows:

For Iterating_var in sequence:
Statements

Iterating_var is an iterative variable of an object sequence, that is, sequence must be an object that can have some sort of sequence, with special attention to not a sequence, that is, the ability to get the element according to a certain foot mark. Of course, the file object belongs to the sequence, we do not use the pin to get each row, if read it out, because it is also a str, so can still read its contents with the pin.

Zip

What's a zip? In interactive mode with help (Zip), get the official document is:

Zip (...)
Zip (seq1 [, SEQ2 [...]])-> [(Seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th element from each of the argument sequences. The returned list is truncated at length to the length of the shortest argument sequence.
Through the experiment to understand the above documents:

Copy Code code as follows:

>>> a = "Qiwsir"
>>> B = "GitHub"
>>> Zip (a,b)
[(' Q ', ' G '), (' I ', ' I '), (' W ', ' t '), (' s ', ' H '), (' I ', ' U '), (' R ', ' B ')]
>>> C = [1,2,3]
>>> d = [9,8,7,6]
>>> Zip (c,d)
[(1, 9), (2, 8), (3, 7)]
>>> e = (1,2,3)
>>> f = (9,8)
>>> Zip (e,f)
[(1, 9), (2, 8)]

>>> m = {"Name", "Lang"}
>>> n = {"Qiwsir", "Python"}
>>> Zip (m,n)
[(' Lang ', ' Python '), (' Name ', ' Qiwsir ')]
>>> s = {"name": "Qiwsir"}
>>> t = {"Lang": "Python"}
>>> Zip (s,t)
[(' Name ', ' Lang ')]

A zip is a built-in function whose arguments must be some sort of sequence data type and, if it is a dictionary, the key is considered a sequence. Then the elements that correspond to the sequence are then formed into tuples as elements of a list.

Here is a special case where a parameter is a sequence of data and the resulting result looks like this:

Copy Code code as follows:

>>> A
' Qiwsir '
>>> C
[1, 2, 3]
>>> Zip (c)
[(1,), (2,), (3,)]
>>> Zip (a)
[(' Q ',], (' I ',), (' W ',), (' s ',), (' I ',), (' R ',)]

This function is used with the for, and is implemented:

Copy Code code as follows:

>>> C
[1, 2, 3]
>>> D
[9, 8, 7, 6]
>>> for x,y in Zip (c,d): #实现一对一对地打印
... print x,y
...
1 9
2 8
3 7
>>> for x,y in Zip (c,d): #把两个list中的对应量上下相加.
... print x+y
...
10
10
10

The addition of the above function, if you do not use a zip, you can also write:

Copy Code code as follows:

>>> length = Len (c) If Len (c) <len (d) Else Len (d) #判断c, d length, take the short length out
>>> for I in range (length):
... print c[i]+d[i]
...
10
10
10

The above two kinds of writing that better? Former? Latter? Ha ha. I think it's almost done. You can do it again:

Copy Code code as follows:

>>> [x+y for x,y in Zip (c,d)]
[10, 10, 10]

Many times before, list analysis tough ah. Of course, you can also do this:

Copy Code code as follows:

>>> [C[i]+d[i] for I in range (length)]
[10, 10, 10]

The For Loop statement is often used later, but it is already used a lot earlier. Therefore, reader should not feel too unfamiliar.

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.