python中while的用法_Python Itertools.takewhile()用法及代码示例

itertools是Python中的一个模块,具有用于处理迭代器的功能集合。它们使遍历列表和字符串之类的可迭代对象变得非常容易。这样的itertools函数之一是takewhile()。

注意:有关更多信息,请参阅Python Itertools。

takewhile()

这允许从迭代开始考虑一个项目,直到指定的谓词首次变为假。在大多数情况下,迭代器是列表或字符串。顾名思义,“take”是序列“while”中的元素,谓词是“true”。该功能属于“terminating iterators”类别。输出不能直接使用,而必须转换为另一种可迭代的形式。通常,它们会转换为列表。

用法:

takewhile(predicate, iterable)

的predicate是内置函数或用户定义的函数。它甚至可以是lambda函数。

下面给出了使用简单if-else对该功能的一般实现。

def takewhile(predicate, iterable):

for i in iterable:

if predicate(i):

return(i)

else:

break

功能takewhile()以谓词和可迭代作为参数。迭代iterable以检查其每个元素。如果指定谓词上的元素的值为true,则将其返回。否则,循环终止。

示例1:列表和takewhile()

考虑一个整数列表。我们只需要输出中的偶数。查看下面的代码,看看如果我们使用takewhile()。

from itertools import takewhile

# function to check whether

# number is even

def even_nos(x):

return(x % 2 == 0)

# iterable (list)

li =[0, 2, 4, 8, 22, 34, 6, 67]

# output list

new_li = list(takewhile(even_nos, li))

print(new_li)

输出:

[0, 2, 4, 8, 22, 34, 6]

示例2:字符串和takewhile()

考虑一个alpha-numerical字符串。现在,我们需要将元素取为数字。

from itertools import takewhile

# function to test the elements

def test_func(x):

print("Testing:", x)

return(x.isdigit())

# using takewhile  with for-loop

for i in takewhile(test_func, "11234erdg456"):

print("Output:", i)

print()

输出:

Testing:1

Output:1

Testing:1

Output:1

Testing:2

Output:2

Testing:3

Output:3

Testing:4

Output:4

Testing:e

可迭代对象也可以直接传递。将它们传递给某些变量不是强制性的takewhile()功能。

示例3:takewhile()中的lambda函数

考虑输入字符串的元素,直到遇到“ s”为止。

from itertools import takewhile

# input string

st ="GeeksforGeeks"

# consider elements until

# 's' is encountered

li = list(takewhile(lambda x:x !='s', st))

print(li)

输出:

['G', 'e', 'e', 'k']

范例4:

按随机顺序列出字母,并考虑元素,直到遇到“ e”,“ i”或“ u”。

import random

from itertools import takewhile

# generating alphabets in random order

li = random.sample(range(97, 123), 26)

li = list(map(chr, li))

print("The original list list is:")

print(li)

# consider the element until

# 'e' or 'i' or 'o' is encountered

new_li = list(takewhile(lambda x:x not in ['e', 'i', 'o'],

li))

print("\nThe new list is:")

print(new_li)

输出:

The original list list is:

[‘v’, ‘u’, ‘k’, ‘j’, ‘r’, ‘q’, ‘n’, ‘y’, ‘a’, ‘x’, ‘i’, ‘p’, ‘e’, ‘w’, ‘b’, ‘t’, ‘s’, ‘l’, ‘z’, ‘m’, ‘f’, ‘c’, ‘g’, ‘d’, ‘o’, ‘h’]

The new list is:

[‘v’, ‘u’, ‘k’, ‘j’, ‘r’, ‘q’, ‘n’, ‘y’, ‘a’, ‘x’]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`matrix_permutations = itertools.product(*row_permutations)` 会对 `row_permutations` 的每个迭代器进行排列,然后将它们的笛卡尔积作为最终结果。因为 `row_permutations` 的每个迭代器都是对应矩阵 `mp` 的一行进行排列的,所以 `matrix_permutations` 的每个元素都是一个排列好的矩阵。 以下是一个示例: 假设有一个矩阵 `mp`: ``` mp = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] ``` 使用 `row_permutations = [itertools.permutations(row) for row in mp]` 对每一行进行排列操作,并使用 `matrix_permutations = itertools.product(*row_permutations)` 对整个矩阵进行排列操作后,将得到以下 `matrix_permutations`: ``` [ ((1, 2, 3), (4, 5, 6), (7, 8, 9)), ((1, 2, 3), (4, 5, 6), (7, 9, 8)), ((1, 2, 3), (4, 6, 5), (7, 8, 9)), ((1, 2, 3), (4, 6, 5), (7, 9, 8)), ((1, 3, 2), (4, 5, 6), (7, 8, 9)), ((1, 3, 2), (4, 5, 6), (7, 9, 8)), ((1, 3, 2), (4, 6, 5), (7, 8, 9)), ((1, 3, 2), (4, 6, 5), (7, 9, 8)), ((2, 1, 3), (4, 5, 6), (7, 8, 9)), ((2, 1, 3), (4, 5, 6), (7, 9, 8)), ((2, 1, 3), (4, 6, 5), (7, 8, 9)), ((2, 1, 3), (4, 6, 5), (7, 9, 8)), ((2, 3, 1), (4, 5, 6), (7, 8, 9)), ((2, 3, 1), (4, 5, 6), (7, 9, 8)), ((2, 3, 1), (4, 6, 5), (7, 8, 9)), ((2, 3, 1), (4, 6, 5), (7, 9, 8)), ((3, 1, 2), (4, 5, 6), (7, 8, 9)), ((3, 1, 2), (4, 5, 6), (7, 9, 8)), ((3, 1, 2), (4, 6, 5), (7, 8, 9)), ((3, 1, 2), (4, 6, 5), (7, 9, 8)), ((3, 2, 1), (4, 5, 6), (7, 8, 9)), ((3, 2, 1), (4, 5, 6), (7, 9, 8)), ((3, 2, 1), (4, 6, 5), (7, 8, 9)), ((3, 2, 1), (4, 6, 5), (7, 9, 8)) ] ``` 可以看到,这里的每个元素都是一个排列好的矩阵,其第一个元素 `((1, 2, 3), (4, 5, 6), (7, 8, 9))` 就是原始矩阵 `mp`,而其他元素则是对 `mp` 进行不同排列得到的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值