Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用列表理解的itertools.product_Python_List Comprehension_Itertools - Fatal编程技术网

Python 使用列表理解的itertools.product

Python 使用列表理解的itertools.product,python,list-comprehension,itertools,Python,List Comprehension,Itertools,我想使用列表理解,而不是itertools.product def pr(l): return [''.join(i) for i in [(x,y) for x in l for y in l]] operators = ['/','*','+','-'] pr(operators) ['//', '/*', '/+', '/-', '*/', '**', '*+', '*-', '+/', '+*', '++', '+-', '-/', '-*', '-+', '--'] 这是

我想使用列表理解,而不是itertools.product

def pr(l):
    return [''.join(i) for i in [(x,y) for x in l for y in l]]

operators = ['/','*','+','-']
pr(operators)

['//', '/*', '/+', '/-', '*/', '**', '*+', '*-', '+/', '+*', '++', '+-', '-/', '-*', '-+', '--']
这是可行的,但我想修改我的函数,使其返回成对重复的组合:

def pr(l, repeat=1):
   # list comprehension code here



o = ['/','*','+','-']

pr(operators, repeat=4)
pr(operators, repeat=3)

['////', '///*', '///+', '///-', '//*/', '//**', '//*+', '//*-', '//+/', '//+*', '//++', '//+-', '//-/', '//-*', '//-+', '//--', '/*//', '/*/*', '/*/+', '/*/-', '/**/', '/***', '/**+', '/**-', '/*+/', '/*+*', '/*++', '/*+-', '/*-/', '/*-*', '/*-+', '/*--', '/+//', '/+/*', '/+/+', '/+/-', '/+*/', '/+**', '/+*+', '/+*-', '/++/', '/++*', '/+++', '/++-', '/+-/', '/+-*', '/+-+', '/+--', '/-//', '/-/*', '/-/+', '/-/-', '/-*/', '/-**', '/-*+', '/-*-', '/-+/', '/-+*', '/-++', '/-+-', '/--/', '/--*', '/--+', '/---', '*///', '*//*', '*//+', '*//-', '*/*/', '*/**', '*/*+', '*/*-', '*/+/', '*/+*', '*/++', '*/+-', '*/-/', '*/-*', '*/-+', '*/--', '**//', '**/*', '**/+', '**/-', '***/', '****', '***+', '***-', '**+/', '**+*', '**++', '**+-', '**-/', '**-*', '**-+', '**--', '*+//', '*+/*', '*+/+', '*+/-', '*+*/', '*+**', '*+*+', '*+*-', '*++/', '*++*', '*+++', '*++-', '*+-/', '*+-*', '*+-+', '*+--', '*-//', '*-/*', '*-/+', '*-/-', '*-*/', '*-**', '*-*+', '*-*-', '*-+/', '*-+*', '*-++', '*-+-', '*--/', '*--*', '*--+', '*---', '+///', '+//*', '+//+', '+//-', '+/*/', '+/**', '+/*+', '+/*-', '+/+/', '+/+*', '+/++', '+/+-', '+/-/', '+/-*', '+/-+', '+/--', '+*//', '+*/*', '+*/+', '+*/-', '+**/', '+***', '+**+', '+**-', '+*+/', '+*+*', '+*++', '+*+-', '+*-/', '+*-*', '+*-+', '+*--', '++//', '++/*', '++/+', '++/-', '++*/', '++**', '++*+', '++*-', '+++/', '+++*', '++++', '+++-', '++-/', '++-*', '++-+', '++--', '+-//', '+-/*', '+-/+', '+-/-', '+-*/', '+-**', '+-*+', '+-*-', '+-+/', '+-+*', '+-++', '+-+-', '+--/', '+--*', '+--+', '+---', '-///', '-//*', '-//+', '-//-', '-/*/', '-/**', '-/*+', '-/*-', '-/+/', '-/+*', '-/++', '-/+-', '-/-/', '-/-*', '-/-+', '-/--', '-*//', '-*/*', '-*/+', '-*/-', '-**/', '-***', '-**+', '-**-', '-*+/', '-*+*', '-*++', '-*+-', '-*-/', '-*-*', '-*-+', '-*--', '-+//', '-+/*', '-+/+', '-+/-', '-+*/', '-+**', '-+*+', '-+*-', '-++/', '-++*', '-+++', '-++-', '-+-/', '-+-*', '-+-+', '-+--', '--//', '--/*', '--/+', '--/-', '--*/', '--**', '--*+', '--*-', '--+/', '--+*', '--++', '--+-', '---/', '---*', '---+', '----']
['///', '//*', '//+', '//-', '/*/', '/**', '/*+', '/*-', '/+/', '/+*', '/++', '/+-', '/-/', '/-*', '/-+', '/--', '*//', '*/*', '*/+', '*/-', '**/', '***', '**+', '**-', '*+/', '*+*', '*++', '*+-', '*-/', '*-*', '*-+', '*--', '+//', '+/*', '+/+', '+/-', '+*/', '+**', '+*+', '+*-', '++/', '++*', '+++', '++-', '+-/', '+-*', '+-+', '+--', '-//', '-/*', '-/+', '-/-', '-*/', '-**', '-*+', '-*-', '-+/', '-+*', '-++', '-+-', '--/', '--*', '--+', '---']

我该怎么做呢?

只需将您的命令包装到函数中即可

def f(l):
    return [(x, y) for x in l for y in l]
或者,如果您想要与
itertools
具有相同的生成器行为:

def f(l):
    return ((x, y) for x in l for y in l)
或更详细的生成器语法:

def f(l):
    for x in l:
        for y in l:
            yield x, y

对于“repeat”的任何值,这里都有一个通用的解决方案

def pr(l, repeat=1):
    if repeat == 1:
        return [[x] for x in l]
    sub_prod = pr(l, repeat-1)
    return [ [x] + y for x in l for y in sub_prod ]

o = ['/','*','+','-']

pr(o, 3)
结果:

[['/', '/', '/'],
 ['/', '/', '*'],
 ['/', '/', '+'],
 ['/', '/', '-'],
 ['/', '*', '/'],
 ['/', '*', '*'],
 ['/', '*', '+'],
 ['/', '*', '-'],
 ['/', '+', '/'],
 ['/', '+', '*'],
 ['/', '+', '+'],
 ...
如果要将每个子列表转换为字符串,请使用:

["".join(x) for x in pr(o, 3)]

你能澄清一下吗?样本输入和样本输出你现有的有什么问题?
列表(产品(“/*+-”,“/*+-”)
@Roy2012如果我不得不猜测的话,它包括同一对的对立和不同顺序。根据定义,产品包含同一对的不同顺序。所以我不确定这里有什么问题。这能回答你的问题吗?很好用!谢谢你