篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 随机par ou impar(功能示例)相关的知识,希望对你有一定的参考价值。
"""
Exemplo para ajudar a responder o problema de aninhamento em https://gist.github.com/AndersonFirmino/b0300923094a5a8450018c5bd32c9de8
"""
from itertools import filterfalse
from functools import partial
from random import choice, random
def pipe(*funcs):
def inner(data, funcs=funcs):
result = data
for f in funcs:
result = f(result)
return result
return inner
is_par = lambda x: x % 2 == 0
filter_par = partial(filter, is_par)
filter_impar = partial(filterfalse, is_par)
random_sorted = partial(sorted, key=lambda x: random())
flaged = pipe(range, tuple, filter_par, random_sorted, choice)
non_flaged = pipe(range, tuple, filter_impar, random_sorted, choice)
def odd_or_even(flag, limit):
"""Retorna um numero aleatorio pelo limite
:param flag:
:param limit:
:return:
"""
return flaged(limit) if flag else non_flaged(limit)
以上是关于python 随机par ou impar(功能示例)的主要内容,如果未能解决你的问题,请参考以下文章