Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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 解包和组合嵌套元组的一般方法_Python - Fatal编程技术网

Python 解包和组合嵌套元组的一般方法

Python 解包和组合嵌套元组的一般方法,python,Python,我已经生成了三个dict的笛卡尔积,因此: import itertools combined = list(itertools.product(foo, bar, baz)) 因此,我的列表现在如下所示: [(('text a', 'value a'), ('text b', 'value b'), ('text c', 'value c')), … ] 我想做的是解压这个列表,这样我就可以得到一个列表,其中包含扁平嵌套元组的文本和值: [['text a text b text c',

我已经生成了三个dict的笛卡尔积,因此:

import itertools
combined = list(itertools.product(foo, bar, baz))
因此,我的列表现在如下所示:

[(('text a', 'value a'), ('text b', 'value b'), ('text c', 'value c')), … ]
我想做的是解压这个列表,这样我就可以得到一个列表,其中包含扁平嵌套元组的文本和值:

[['text a text b text c', 'value a value b value c'], … ]
有没有一个有效的通用方法来实现这一点

在看到Dan D.的答案后,继续跟进:

如果我的元组看起来像什么呢

(('text a', float a), ('text b', float b), ('text c', float c ))

我想添加浮点数,而不是串联数值?

您不必将所有内容都塞进一行:

map(lambda v: map(' '.join, zip(*v)), combined)
def combine(items):
    for tuples in items:
        text, numbers = zip(*tuples)
        yield ' '.join(text), sum(numbers)

print list(combine(product( ... )))

您不必将所有内容都塞进一行:

def combine(items):
    for tuples in items:
        text, numbers = zip(*tuples)
        yield ' '.join(text), sum(numbers)

print list(combine(product( ... )))
如果我的元组看起来像什么呢

(('text a', float a), ('text b', float b), ('text c', float c ))
组合='文本a',浮点a',文本b',浮点b',文本c',浮点c

我想添加浮点数,而不是串联数值

map(lambda v: map(' '.join, zip(*v)), combined)
尝试使用内置的sum函数:

sum(x for (_,x) in combined)
如果我的元组看起来像什么呢

(('text a', float a), ('text b', float b), ('text c', float c ))
组合='文本a',浮点a',文本b',浮点b',文本c',浮点c

我想添加浮点数,而不是串联数值

map(lambda v: map(' '.join, zip(*v)), combined)
尝试使用内置的sum函数:

sum(x for (_,x) in combined)

这里有一个概括:

def combine(functions, data):
    for elements in data:
        yield [f(e) for f, e in zip(functions, zip(*elements))]

# A simple function for demonstration.
def pipe_join(args):
    return '|'.join(args)

some_data = [
    (('A', 10), ('B', 20), ('C', 30)),
    (('D', 40), ('E', 50), ('F', 60)),
    (('G', 70), ('H', 80), ('I', 90)),
]

for c in combine( [pipe_join, sum], some_data ):
    print c

这里有一个概括:

def combine(functions, data):
    for elements in data:
        yield [f(e) for f, e in zip(functions, zip(*elements))]

# A simple function for demonstration.
def pipe_join(args):
    return '|'.join(args)

some_data = [
    (('A', 10), ('B', 20), ('C', 30)),
    (('D', 40), ('E', 50), ('F', 60)),
    (('G', 70), ('H', 80), ('I', 90)),
]

for c in combine( [pipe_join, sum], some_data ):
    print c

这正是我想要的。这正是我想要的。同意。Dan的回答对于具体案例更为简洁,但这更为灵活,更易于适应。同意。Dan的回答对于具体案例来说更简洁,但这更灵活,也更容易适应。这是联合收割机中令人惊讶和可怕的复杂收益声明,也就是说,一次完成。这是联合收割机中令人惊讶和可怕的复杂收益声明,也就是说,一次完成。