Python – Itertools.chain()
itertools 是Python中的一个模块,具有一组用于处理迭代器的函数。它们使迭代列表和字符串等可迭代对象变得非常容易。一个这样的 itertools函数是chain() 。
注意:更多信息请参考Python Itertools
链()函数
它是一个接受一系列迭代并返回一个迭代的函数。它将所有可迭代对象组合在一起并生成一个可迭代对象作为输出。它的输出不能直接使用,因此显式转换为可迭代对象。此函数属于迭代器终止迭代器的类别。
句法 :
chain (*iterables)
链的内部工作可以实现如下:
def chain(*iterables):
for it in iterables:
for each in it:
yield each
示例 1:奇数和偶数在不同的列表中。将它们组合起来形成一个新的单一列表。
Python3
from itertools import chain
# a list of odd numbers
odd = [1, 3, 5, 7, 9]
# a list of even numbers
even = [2, 4, 6, 8, 10]
# chaining odd and even numbers
numbers = list(chain(odd, even))
print(numbers)
Python3
from itertools import chain
# some consonants
consonants = ['d', 'f', 'k', 'l', 'n', 'p']
# some vowels
vowels = ['a', 'e', 'i', 'o', 'u']
# resultatnt list
res = list(chain(consonants, vowels))
# sorting the list
res.sort()
print(res)
Python3
from itertools import chain
res = list(chain('ABC', 'DEF', 'GHI', 'JKL'))
print(res)
Python3
from itertools import chain
st1 = "Geeks"
st2 = "for"
st3 = "Geeks"
res = list(chain(st1, st2, st3))
print("before joining :", res)
ans = ''.join(res)
print("After joining :", ans)
Python3
from itertools import chain
li = ['ABC', 'DEF', 'GHI', 'JKL']
# using chain-single iterable.
res1 = list(chain(li))
res2 = list(chain.from_iterable(li))
print("using chain :", res1, end ="\n\n")
print("using chain.from_iterable :", res2)
Python3
from itertools import chain
li = ['123', '456', '789']
res = list(chain.from_iterable(li))
print("res =", res, end ="\n\n")
new_res = list(map(int, res))
print("new_res =", new_res)
sum_of_li = sum(new_res)
print("\nsum =", sum_of_li)
Python3
from itertools import chain
li = ['123', '456', '789']
res = list(map(int, list(chain.from_iterable(li))))
sum_of_li = sum(res)
print("res =", res, end ="\n\n")
print("sum =", sum_of_li)
输出:
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
示例 2:一些辅音在列表中。元音在列表中给出。将它们组合起来并对其进行排序。
Python3
from itertools import chain
# some consonants
consonants = ['d', 'f', 'k', 'l', 'n', 'p']
# some vowels
vowels = ['a', 'e', 'i', 'o', 'u']
# resultatnt list
res = list(chain(consonants, vowels))
# sorting the list
res.sort()
print(res)
输出:
['a', 'd', 'e', 'f', 'i', 'k', 'l', 'n', 'o', 'p', 'u']
示例 3:在下面的示例中,每个 String 都被认为是一个可迭代对象,其中的每个字符都被认为是迭代器中的一个元素。因此每个字符都产生
Python3
from itertools import chain
res = list(chain('ABC', 'DEF', 'GHI', 'JKL'))
print(res)
输出:
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
示例 4:
Python3
from itertools import chain
st1 = "Geeks"
st2 = "for"
st3 = "Geeks"
res = list(chain(st1, st2, st3))
print("before joining :", res)
ans = ''.join(res)
print("After joining :", ans)
输出:
加入前:['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k ', '']
加入后:GeeksforGeeks
加入前:['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k ', '']
加入后:GeeksforGeeks
chain.from_iterable()函数
它类似于链,但它可用于从单个迭代中链接项目。差异在下面给出的示例中得到了证明:
示例 5:
Python3
from itertools import chain
li = ['ABC', 'DEF', 'GHI', 'JKL']
# using chain-single iterable.
res1 = list(chain(li))
res2 = list(chain.from_iterable(li))
print("using chain :", res1, end ="\n\n")
print("using chain.from_iterable :", res2)
输出:
使用链:['ABC','DEF','GHI','JKL']
使用chain.from_iterable:['A','B','C','D','E','F','G','H','I','J','K', 'L']
使用链:['ABC','DEF','GHI','JKL']
使用chain.from_iterable:['A','B','C','D','E','F','G','H','I','J','K', 'L']
示例 6:现在考虑如下所示的列表:
li=['123', '456', '789']
您应该考虑每个数字来计算列表的总和。所以答案应该是:
1+2+3+5+6+7+8+9 = 45
这可以使用下面的代码轻松实现:
Python3
from itertools import chain
li = ['123', '456', '789']
res = list(chain.from_iterable(li))
print("res =", res, end ="\n\n")
new_res = list(map(int, res))
print("new_res =", new_res)
sum_of_li = sum(new_res)
print("\nsum =", sum_of_li)
输出:
res = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
new_res = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sum = 45
为了简化它,我们结合了这些步骤。下面给出一个优化的方法:
Python3
from itertools import chain
li = ['123', '456', '789']
res = list(map(int, list(chain.from_iterable(li))))
sum_of_li = sum(res)
print("res =", res, end ="\n\n")
print("sum =", sum_of_li)
输出:
res = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sum = 45