Python通过某些内置函数在各种顺序容器中支持各种循环技术。这些方法在竞争性编程中非常有用,在各种项目中也非常有用,这些项目需要使用特定技术并通过循环来维护代码的整体结构。
使用Python数据结构的不同循环技术:
1.使用enumerate():enumerate()用于循环遍历容器,打印索引号以及该特定索引中的值。
# python code to demonstrate working of enumerate()
for key, value in enumerate(['The', 'Big', 'Bang', 'Theory']):
print(key, value)
输出:
0 The
1 Big
2 Bang
3 Theory
2.使用zip():zip()用于组合2个类似的容器(list-list或dict-dict),依次打印值。该循环仅在较小的容器结束之前存在。zip()和enumerate()的详细说明可以在这里(https://www.geeksforgeeks.org/using-iterations-in-python-effectively/)找到。
# python code to demonstrate working of zip()
# initializing list
questions = ['name', 'colour', 'shape']
answers = ['apple', 'red', 'a circle']
# using zip() to combine two containers
# and print values
for question, answer in zip(questions, answers):
print('What is your {0}? I am {1}.'.format(question, answer))
输出:
What is your name? I am apple.
What is your color? I am red.
What is your shape? I am a circle.
3.使用iteritem():iteritems()用于遍历字典,顺序打印字典键值对。
4.使用items():items()在字典上执行的任务与iteritems()类似,但与iteritems()相比有某些缺点。
1)这非常耗时。在大型词典上调用它会消耗大量时间。
2)这会占用大量内存。 在字典上调用时,有时会占用双倍的内存。
示例1:
# python code to demonstrate working of iteritems(),items()
d = { "geeks" : "for", "only" : "geeks" }
# using iteritems to print the dictionary key-value pair
print ("The key value pair using iteritems is : ")
for i,j in d.iteritems():
print i,j
# using items to print the dictionary key-value pair
print ("The key value pair using items is : ")
for i,j in d.items():
print i,j
输出:
The key value pair using iteritems is :
geeks for
only geeks
The key value pair using items is :
geeks for
only geeks
示例2:
# python code to demonstrate working of items()
king = {'Akbar': 'The Great', 'Chandragupta': 'The Maurya',
'Modi' : 'The Changer'}
# using items to print the dictionary key-value pair
for key, value in king.items():
print(key, value)
输出:
Akbar The Great
Modi The Changer
Chandragupta The Maurya
注意:iteritems()仅在python 2.x中有效,它已从python 3.x中删除,请使用dict.items()。
5.使用sorted():sorted()用于打印容器的排序顺序。它不会对容器进行排序,而只会按1个实例的排序顺序打印容器。可以结合使用set()来删除重复的事件。
示例1:
# python code to demonstrate working of sorted()
# initializing list
lis = [ 1 , 3, 5, 6, 2, 1, 3 ]
# using sorted() to print the list in sorted order
print ("The list in sorted order is : ")
for i in sorted(lis) :
print (i,end=" ")
print ("\r")
# using sorted() and set() to print the list in sorted order
# use of set() removes duplicates.
print ("The list in sorted order (without duplicates) is : ")
for i in sorted(set(lis)) :
print (i,end=" ")
输出:
The list in sorted order is :
1 1 2 3 3 5 6
The list in sorted order (without duplicates) is :
1 2 3 5 6
示例2:
# python code to demonstrate working of sorted()
# initializing list
basket = ['guave', 'orange', 'apple', 'pear',
'guava', 'banana', 'grape']
# using sorted() and set() to print the list
# in sorted order
for fruit in sorted(set(basket)):
print(fruit)
输出:
apple
banana
grape
guava
guave
orange
pear
6.使用reversed():reversed()用于以相反的顺序打印容器的值。
注意:它不反映对原始列表的任何更改。
示例1:
# python code to demonstrate working of reversed()
# initializing list
lis = [ 1 , 3, 5, 6, 2, 1, 3 ]
# using revered() to print the list in reversed order
print ("The list in reversed order is : ")
for i in reversed(lis) :
print (i,end=" ")
输出:
The list in reversed order is :
3 1 2 6 5 3 1
示例2:
# python code to demonstrate working of reversed()
# using reversed() to print in reverse order
for i in reversed(range(1, 10, 3)):
print (i)
输出:
7
4
1
与for,while循环相比,使用上述技术的优势
1)这些技术使用迅速,减少了编码工作。for,while循环需要更改容器的整个结构。
2)这些循环技术不需要对容器进行任何结构更改。它们有表示确切使用目的的关键字。然而,鉴于无法进行任何预测或猜测,for,while循环就难以理解。
3)上述循环技术使代码比在循环时使用for更为简洁。