Introduction to Python looping techniques (with code)

不言
Release: 2019-04-15 10:54:09
forward
2266 people have browsed it

This article brings you an introduction to Python looping skills (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

When looping in the dictionary, use the items() method to take out the keywords and corresponding values at the same time

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'} >>> for k, v in knights.items(): ... print(k, v) ... gallahad the pure robin the brave
Copy after login

When looping in the sequence, useenumerate()The function can take out the index position and its corresponding value at the same time

>>> for i, v in enumerate(['tic', 'tac', 'toe']): ... print(i, v) ... 0 tic 1 tac 2 toe
Copy after login

When looping in two or more sequences at the same time, you can usezip()The function matches the elements inside it one by one.

>>> questions = ['name', 'quest', 'favorite color'] >>> answers = ['lancelot', 'the holy grail', 'blue'] >>> for q, a in zip(questions, answers): ... print('What is your {0}? It is {1}.'.format(q, a)) ... What is your name? It is lancelot. What is your quest? It is the holy grail. What is your favorite color? It is blue.
Copy after login

When looping a sequence in the reverse direction, first position the sequence in the forward direction, and then call thereversed()function

>>> for i in reversed(range(1, 10, 2)): ... print(i) ... 7 3
Copy after login

If you want to press a certain To cycle through a sequence in a specified order, you can use thesorted()function, which can return a new sorted sequence without changing the original sequence

>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] >>> for f in sorted(set(basket)): ... print(f) ... apple banana orange pear
Copy after login

Sometimes you may want to modify the contents of the list while python is looping. Generally speaking, it is simpler and safer to create a new list instead

>>> import math >>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8] >>> filtered_data = [] >>> for value in raw_data: ... if not math.isnan(value): ... filtered_data.append(value) ... >>> filtered_data [56.2, 51.7, 55.3, 52.5, 47.8]
Copy after login

[Related recommendations:python tutorial

The above is the detailed content of Introduction to Python looping techniques (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!