📜  在Python中迭代一个集合

📅  最后修改于: 2022-05-13 01:54:26.988000             🧑  作者: Mango

在Python中迭代一个集合

在Python中,设置 是可迭代、可变且没有重复元素的数据类型的无序集合。
有多种方法可用于迭代 Set。与其他方式相比,其中一些方式提供更快的时间执行。其中一些方法包括使用 for/while 循环、推导式、迭代器及其变体进行迭代。让我们看看我们可以在Python中迭代集合的所有不同方式。
每种方法的分析:
为了解释每种方式/技术的工作原理,每组(随机生成的集合)的时间已计算 5 次,以粗略估计每种技术在给定集合上迭代所需的时间。 random.seed(21) 已添加到每个脚本中,以固定每次执行程序时生成的随机数。使用常量种子可以帮助我们确定哪种技术最适合给定的特定随机生成集。
方法 #1:使用简单的 for 循环遍历集合。

Python3
# Creating a set using string
test_set = set("geEks")
 
# Iterating using for loop
for val in test_set:
    print(val)


Python3
# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    for val in test_set:
        _ = val
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
 
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))


Python3
# Creating a set using string
test_set = set("geEks")
 
# Iterating using enumerated for loop
for id,val in enumerate(test_set):
    print(id, val)


Python3
# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    for id, val in enumerate(test_set):
        _ = val
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))


Python3
# Creating a set using string
test_set = set("geEks")
 
test_list = list(test_set)
 
# Iterating over a set as a indexed list
for id in range(len(test_list)):
        print(test_list[id])


Python3
# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    test_list = list(test_set)
 
    for id in range(len(test_list)):
        _ = test_list[id]
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))


Python3
# Creating a set using string
test_set = set("geEks")
 
# Iterating using list-comprehension
com = list(val for val in test_set)
print(*com)


Python3
# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    list(val for val in test_set)
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))


Python3
# Creating a set using string
test_set = set("geEks")
 
# Iterating using list-comprehension
com = [print(val) for val in test_set]


Python3
# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    [val for val in test_set]
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))


Python3
# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    [map(lambda val: val, test_set)]
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))


Python3
# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    for val in iter(test_set):
        _ = val
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))


Python3
# Creating a set using string
test_set = set("geEks")
 
iter_gen = iter(test_set)
 
while True:
    try:
        # get the next item
        print(next(iter_gen))
         
        ''' do something with element '''
         
    except StopIteration:
        # if StopIteration is raised,
        # break from loop
        break


Python3
# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    iter_gen = iter(test_set)
    while True:
        try:
            # get the next item
            next(iter_gen)
            # do something with element
        except StopIteration:
            # if StopIteration is raised, break from loop
            break
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))


输出:

k
s
e
g
E

分析:

Python3

# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    for val in test_set:
        _ = val
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
 
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))

输出:

0.06303901899809716
0.06756918999963091
0.06692574200133095
0.067220498000097
0.06748137499744189


方法 #2:使用枚举 for 循环遍历集合。

Python3

# Creating a set using string
test_set = set("geEks")
 
# Iterating using enumerated for loop
for id,val in enumerate(test_set):
    print(id, val)

输出:

0 E
1 e
2 k
3 g
4 s

分析:

Python3

# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    for id, val in enumerate(test_set):
        _ = val
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))

输出

0.1306622320007591
0.13657568199778325
0.13797824799985392
0.1386374360008631
0.1424286179972114


方法#3:迭代作为索引列表的集合。

Python3

# Creating a set using string
test_set = set("geEks")
 
test_list = list(test_set)
 
# Iterating over a set as a indexed list
for id in range(len(test_list)):
        print(test_list[id])

输出:

g
k
E
s
e

分析:

Python3

# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    test_list = list(test_set)
 
    for id in range(len(test_list)):
        _ = test_list[id]
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))

输出

0.20036015100049553
0.2557020290005312
0.4601482660000329
0.2161413249996258
0.18769703499856405


方法#4:使用理解和列表构造器/初始化器迭代集合。

Python3

# Creating a set using string
test_set = set("geEks")
 
# Iterating using list-comprehension
com = list(val for val in test_set)
print(*com)

输出:

k s e g E

分析:

Python3

# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    list(val for val in test_set)
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))

输出

0.1662169310002355
0.1783527520019561
0.21661155100082397
0.19131610199838178
0.19931397800246486


方法#5:使用理解对集合进行迭代。

Python3

# Creating a set using string
test_set = set("geEks")
 
# Iterating using list-comprehension
com = [print(val) for val in test_set]

输出:

e
E
g
s
k

分析:

Python3

# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    [val for val in test_set]
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))

输出

0.11386321299869451
0.111869686999853
0.1092844699996931
0.11223735699968529
0.10928539399901638


方法 #6:使用 map、lambda 和列表推导对集合进行迭代

Python3

# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    [map(lambda val: val, test_set)]
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))

输出

1.0756000847322866e-05
1.310199877480045e-05
1.269100175704807e-05
1.1588999768719077e-05
1.2522999895736575e-05


方法#7:使用迭代器对集合进行迭代。

Python3

# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    for val in iter(test_set):
        _ = val
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))

输出

0.0676155920009478
0.07111633900058223
0.06994135700006154
0.0732101009998587
0.08668379899972933


方法 #8:使用迭代器和 while 循环对集合进行迭代。

Python3

# Creating a set using string
test_set = set("geEks")
 
iter_gen = iter(test_set)
 
while True:
    try:
        # get the next item
        print(next(iter_gen))
         
        ''' do something with element '''
         
    except StopIteration:
        # if StopIteration is raised,
        # break from loop
        break

输出:

E
s
e
k
g

分析:

Python3

# importing libraries
from timeit import default_timer as timer
import itertools
import random
 
# Function under evaluation
def test_func(test_set):
 
    iter_gen = iter(test_set)
    while True:
        try:
            # get the next item
            next(iter_gen)
            # do something with element
        except StopIteration:
            # if StopIteration is raised, break from loop
            break
 
# Driver function
if __name__ == '__main__':
 
    random.seed(21)
    for _ in range(5):
        test_set = set()
 
        # generating a set of random numbers
        for el in range(int(1e6)):
            el = random.random()
            test_set.add(el)
 
        start = timer()
        test_func(test_set)
        end = timer()
 
        print(str(end - start))

输出:

0.2136418699992646
0.1952157889973023
0.4234208280031453
0.255840524998348
0.24712910099697183

结论:
在所有循环技术中,简单的 for 循环迭代和迭代器循环效果最好,而在比较所有技术时,使用带有 lambda over set 的 map 或 set 的迭代器效果最好,在 10 毫秒内提供一百万次 set 迭代的性能。值得注意的是,上述示例每次迭代只能访问一次集合组件,而如果我们增加每次迭代访问集合组件的次数,它可能会改变每次迭代所花费的时间。
注意:上述示例输出中提到的值必然会有所不同。时间消耗变化背后的原因是个人系统处理器的处理能力的机器依赖性。