使函数仅在python中运行某些条件 [英] Making a function only run for certain conditions in python

查看:136
本文介绍了使函数仅在python中运行某些条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

G'day!

所以我有一个函数从两个列表中取出元素,第一个是标准列表格式,第二个是列表列表,内部列表包含3元组形式的元素。我的输出是第二个列表格式的新列表,在相同数量的内部列表中包含相同数量的元素,其中一些值由于通过函数传递而略微调整。

So I have a function which is taking the elements from two lists, the first of which is in a standard list format, the second being a list of lists, the inner lists containing elements in the form of 3-tuples. My output is a new list in the format of the the second list, containing the same number of elements in the same number of inner lists, with some of the values slightly adjusted as a result of being passed through the function.

这是一个示例代码和一个示例函数,其中从itertools导入链。首先是一些列表,如 [0,1,2,3,1,5,6,7,1,2,3,5,1,1,2,3,5,6]
秒是一些列表,例如 [[(13,12,32),(11,444,25)],[(312,443,12),(123, 4,123)],[(545,541,1),(561,112,560)]]

Here is an example code, and an example function, where chain is being imported from itertools. first is some list such as [0,1,2,3,1,5,6,7,1,2,3,5,1,1,2,3,5,6] whilst second is some list such as [[(13,12,32),(11,444,25)],[(312,443,12),(123,4,123)],[(545,541,1),(561,112,560)]]

def add(x, y):
    return x + y 

foo = [add(x, y) for x, y in zip(first, chain(*(chain(*second))))]
bar = [foo[i:i+3] for i in range(0, len(foo), 3)]
second = [bar[i:i+2]  for i in range(0, len(foo) / 3, 2)]

* *注意: Chain(chain())部分用于以下目的:因为处理包含3元组的列表列表通常有点困难,所以链(chain())只是扁平化(进入传统列表)个别元素)第二个列表具有上述'奇数格式'。其余代码只是从函数的输出重建新列表到原始格式,该函数已经是扁平形式。

**Note: The Chain(chain()) part is for the following purpose: Because it's generally a bit harder to handle a list of list containing 3-tuples, The chain(chain()) is just flattening (into a traditional list of individual elements) that second list with the aforementioned 'odd format'. The rest of the code is just rebuilding the new list into the original format from the output of the function, which is already in flattened form.

我遇到的问题是这样的:

The problems I'm having are as such:

我希望输出的大小和格式与原始的第二列表完全相同。如果两个列表都为空,我希望返回空列表。如果第一个列表为空,我希望返回原始的第二个列表。如果第二个列表为空,我想要返回空列表。

I want the output to be of the exact same size and format as the original 'second' list. If both lists are empty, I want the empty list returned. If the first list is empty, I want the original second list returned. If the second list is empty, I want the empty list returned.

如果第一个列表比第二个列表短,我想要运行该函数,但是可以在两个列表之间匹配元素,然后是多余列表第二个列表保持不变。

If the first list is shorter than the second list, I want the function to run for however elements can be matched between the two lists, then the 'excess' of the second list remaining unchanged.

如果第二个列表比第一个列表短,我想要在第二个列表中有多个元素运行该函数,然后只是忽略列表1中的多余元素,因此仍然输出一个与原始第二个列表具有相同尺寸和格式的新列表。

If the second list is shorter than the first list, I want the function to run for however many elements there are in the second list, then just ignore the 'excess' elements from list 1, thus still outputting a new list that has the same dimensions and formatting as the original second list.

我的问题是,我不知道如何在我的代码中实现这些小细微差别。任何帮助将不胜感激。

My problem is, that I have no idea how to implement these little nuances into my code. Any help would be appreciated.

干杯,
詹姆斯

Cheers, James

推荐答案

如果我了解了所有要求,我认为这可以做你想要的一切。与您的代码的主要区别在于它还使用来自 itertools izip_longest()并使用自定义 fillvalue ,而不是普通的 zip()。它也只是在开始时检查涉及空输入列表的特殊情况,这似乎比试图设计列表推导或任何处理它们更容易。

I think this does everything you want, if I've understood all the requirements. The major difference from your code is that it also usesizip_longest() fromitertoolswith a customfillvalue, instead of plainzip(). It also just checks for the special cases involving empty input lists at the beginning, which seemed easier than trying to devise list comprehensions or whatever to handle them.

from itertools import chain, izip_longest

def add(x, y):
    return x + y

def func(first, second):
    if not first: return second
    if not second: return []
    second = chain(*(chain(*second)))  # flatten
    foo = [add(x, y) for x, y in izip_longest(first, second, fillvalue=0)]
    bar = [tuple(foo[i:i+3]) for i in range(0, len(foo), 3)]
    return [bar[i:i+2]  for i in range(0, len(foo) / 3, 2)]

if __name__ == '__main__':
    first = [
        0, 1, 2,  3, 1, 5,
        6, 7, 1,  2, 3, 5,
        1, 1, 2,  3, 5, 6]
    second = [
        [(13, 12, 32), (11, 444, 25)],      [(312, 443, 12), (123, 4, 123)],
        [(545, 541, 1), (561, 112, 560)],   [(13, 12, 32), (11, 444, 25)],
        [(312, 443, 12), (123, 4, 123)],    [(545, 541, 1), (561, 112, 560)],
    ]

    print func(first, second)
    print
    print func(first[:-1], second) # 1st shorter, as many as poss, rest unchanged
    print
    print func(first, second[:-1]) # 2nd shorter, do only as many as in second

这篇关于使函数仅在python中运行某些条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆