Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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 如何仅为id和product的不同组合调用函数_Python - Fatal编程技术网

Python 如何仅为id和product的不同组合调用函数

Python 如何仅为id和product的不同组合调用函数,python,Python,我想为id和product的不同组合调用函数“func1”,有人能建议怎么做吗 if integrated_flag != True: response = func1(id, product, target, Status) 假设响应是一个字符串,您希望它们连接在一起 或一次性: knownPairs = [] while(morePairsAvailable): #Get a new Id/Product response = '' pair = (id

我想为id和product的不同组合调用函数“func1”,有人能建议怎么做吗

if integrated_flag != True:
    response = func1(id, product, target, Status)

假设响应是一个字符串,您希望它们连接在一起

或一次性:

knownPairs = []

while(morePairsAvailable):
    #Get a new Id/Product

    response = ''
    pair = (id, product)
        if not pair in knownPairs:
            response += func1(id, product, target, Status)
        else:
            knownPairs += pair

将使用Id和Product的每个组合调用函数。至于如何获得id/产品列表,这取决于您的系统是否有id+产品组合列表:

id_prod_lst = [
        [1, "a"],
        [2, "b"],
        [3, "c"],
        [2, "b"],
        [3, "c"]
        ]
使用set仅查找唯一实例。Set需要不可变项,为此,我们将列表转换为元组:

uniq_id_prod_set = set(tuple(itm) for itm in id_prod_lst)
print uniq_id_prod_set
具有调用的功能:

def longfunc(pr_id, product, target, stat):
    print pr_id, product, target, stat
    return
最后,在循环中调用它:

for pr_id, product in uniq_id_prod_set:
    longfunc(pr_id, product, "T->bum", "Status")
输出应为:

set([(1, 'a'), (2, 'b'), (3, 'c')])
for loop
1 a T->bum Status
2 b T->bum Status
3 c T->bum Status

我想到了一个
for
循环。如何存储
id
product
?它们在一个列表、一本字典、一个数据库中吗?你那里的东西有什么问题?我想你不是想把括号放在那里。@basic-不是这样,我有没有办法把id和prod输入到某个哈希表中,并检查哈希表中是否没有该组合以供后续运行?@DSM你是对的。。。我用它们来暗示列表语法,给OP一个提示,但我同意它是相反的-intuitive@user3654069您计划如何跨运行保留哈希表?你说的“插拔”是什么意思?在控制台/代码中的某个地方键入?更妙的是,退一步。。。你想完成什么?某种缓存,这样如果已经计算过,你就可以查找以前的结果,或者???你目前的要求没有多大意义,这意味着你试图解决错误problem@basic-目标是为id和prod的唯一组合调用func1,因此如果已经为(id和prod)的组合运行func1我们不想再次调用func1…哈希表可以是一个全局表,以便在运行期间保留数据…脚本没有现成的id和prod列表,因此,有没有办法输入(id和prod)组合到某个哈希表中,并检查该组合是否不存在于后续运行的hastable中,然后才调用该函数?
set([(1, 'a'), (2, 'b'), (3, 'c')])
for loop
1 a T->bum Status
2 b T->bum Status
3 c T->bum Status