Python中的循环技巧

Python中的循环技巧

Python具有特定的内建函数,因此它支持多种迭代技术在几个连续的容器中。这些迭代函数和方法对于竞技编程非常有用。它可以在不同的项目中使用,其中用户必须使用循环的一些特定技术来维护整个程序的结构。这些技术有助于节省时间和内存空间,因为用户不需要为传统方法的循环声明额外的变量。

这些循环技术在哪里使用

不同类型的循环技术在用户不需要操作代码的结构或整个容器的顺序的地方使用。相反,用户必须为单次使用实例打印元素,并且容器中不会发生就地更改。这些也可用于节省时间和内存。

使用Python数据结构的循环技巧

  • enumerate(): 该函数用于遍历容器并打印具有特定索引中的值的索引号。

示例:

for key, value in enumerate(['Joe', 'waited', 'for', 'the', 'train', ',', 'but', 'the', 'train', 'was', 'late', '.']):
    print(key, value)

输出:

0 Joe
1 waited
2 for
3 the
4 train
5 ,
6 but
7 the
8 train
9 was
10 late
11 .
  • zip()函数: zip()函数用于将两个相似的容器结合在一起,例如将字典与字典或列表与列表打印出它们的值。循环只会在较小容器的末尾结束。

示例:

# first, we will initialize the list
question = ['animal', 'shape', 'time']
answer = ['tiger', 'square', '11 o clock']

# the zip() function will be used for combining these two containers 
for question, answer in zip(question, answer):
    print('What is this {0}?  this is {1}.'.format(question, answer))

输出:

What is this animal?  this is tiger.
What is this shape?  this is square.
What is this time?  this is 11 o clock.
  • items(): items()函数用于循环遍历字典,并按顺序打印键和它们的值。

示例:

dict = { "Joe" : "waited", "for" : "the", "train" : "but", "the" : "train", "was" : "late" }

# the use items for print the dictionary key-value pair
print ("The key value pair by using items is : ")
for a, b in dict.items():
    print(a, b)

输出:

The key value pair by using items is : 
Joe waited
for the
train but
the train
was late
  • sorted(): sorted()函数用于以排序的顺序打印容器。此函数不会对容器进行排序,但可用于以排序的顺序打印容器。用户可以使用sorted()函数和set()函数一起使用以从输出中删除重复值。

示例1:

# first, initialize the list
list = [ 1 , 4, 6, 7, 1, 2, 4 ]

# using sorted() to print the list in sorted order
print ("The list in sorted order is : ")
for a in sorted(list) :
    print (a, end = " ")

print ("\r")

# now use the sorted() function and set() function for printing the list in sorted order
# use of set() to removes duplicates in output value.
print ("The list in sorted order (without duplicates) is : ")
for a in sorted(set(list)) :
    print (a, end = " ")

输出:

The list in sorted order is : 
1 1 2 4 4 6 7 
The list in sorted order (without duplicates) is : 
1 2 4 6 7

示例2:

# initializing list
list2 = ['Joe', 'waited', 'for', 'the', 'train', 'but', 'the', 'train', 'was', 'late']

# now use the sorted() function and set() function for printing the list in sorted order
for sentence in sorted(set(list2)):
    print(sentence)

输出:

Joe
but
for
late
the
train
waited
was
  • reversed(): reversed() 函数用于以相反的顺序打印容器的值。

示例1:

# initializing list
list = [ 1 , 3, 5, 7, 9, 11, 13, 15, 17, 19, 21 ]

# by using the revered() function for printing the list in reversed order
print ("The list in reversed order is : ")
for a in reversed(list) :
    print (a, end = " ")

输出:

The list in reversed order is : 
21 19 17 15 13 11 9 7 5 3 1

示例2:

for b in reversed(range(1, 20, 2)):
    print (b)

输出:

19
17
15
13
11
9
7
5
3
1

结论

在本教程中,我们讨论了不同类型的循环技术,这些技术对于节省内存和时间非常有用。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程