循环中的多个列表中调用功能

发布于 2025-02-10 02:58:42 字数 407 浏览 2 评论 0 原文

我已经在Python中写了一个功能,该功能采用了三个输入变量。现在,我想在每个输入变量的项目列表中调用该功能。因此,函数调用就是这样:

 call_function('cat', 'brown', 2)

列表是:

 category_list = ['cat', 'dog', 'squirrel']
 color_list = ['brown', 'black']
 age = [1,2,3]

现在我想在每个项目上运行函数。因此,它应该返回猫,棕色和1个,然后返回猫,棕色和2个,然后猫,棕色和3,然后猫,黑色和1个,然后猫,黑色和2个,然后猫,黑色和3然后为狗做同样的事情,然后对松鼠做同样的事情。我知道我需要循环调用该功能,但是我尝试并失败了几次。有人可以帮我吗?

I have written a function in python which takes three input variables. Now I want to call the function on a list of items for each input variables. So the function call is like this:

 call_function('cat', 'brown', 2)

The lists are:

 category_list = ['cat', 'dog', 'squirrel']
 color_list = ['brown', 'black']
 age = [1,2,3]

Now I want to run function on each item. So it should return results for cat, brown and 1, and then cat, brown and 2, and then cat, brown and 3 and then cat, black and 1, and then cat, black and 2, and then cat, black and 3 and then do the same for dog and then for squirrel. I know that I will need to call the function in a loop, but I tried and failed several times. Could someone please help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹梦 2025-02-17 02:58:42

使用 itertools ,它为您完成了工作,调用 product 函数真的很容易。

将变量提供给 itertools.product(....))作为参数
然后 list()响应。

代码

import itertools
category_list = ['cat', 'dog', 'squirrel']
color_list = ['brown', 'black']
age = [1,2,3]

def call_function(category, color, age):
    print(category, color, age)


combinations = list(itertools.product(category_list, color_list, age))
for combintaion in combinations:
    call_function(combintaion[0], combintaion[1], combintaion[2])

输出

cat brown 1
cat brown 2
cat brown 3
cat black 1
cat black 2
cat black 3
dog brown 1
dog brown 2
dog brown 3
dog black 1
dog black 2
dog black 3
squirrel brown 1
squirrel brown 2
squirrel brown 3
squirrel black 1
squirrel black 2
squirrel black 3

证明

测试在这里

iTertools方法的代码

def product(*args, repeat=1):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = [tuple(pool) for pool in args] * repeat
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

use itertools, it does the job for you, really simple to call the product function.

supply the variables to itertools.product(....)) as paramaters
then list() the response.

Code

import itertools
category_list = ['cat', 'dog', 'squirrel']
color_list = ['brown', 'black']
age = [1,2,3]

def call_function(category, color, age):
    print(category, color, age)


combinations = list(itertools.product(category_list, color_list, age))
for combintaion in combinations:
    call_function(combintaion[0], combintaion[1], combintaion[2])

Output

cat brown 1
cat brown 2
cat brown 3
cat black 1
cat black 2
cat black 3
dog brown 1
dog brown 2
dog brown 3
dog black 1
dog black 2
dog black 3
squirrel brown 1
squirrel brown 2
squirrel brown 3
squirrel black 1
squirrel black 2
squirrel black 3

Proof

Test Here
enter image description here

code for method from itertools

def product(*args, repeat=1):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = [tuple(pool) for pool in args] * repeat
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)
昨迟人 2025-02-17 02:58:42

这是使用pd.apply的另一种方法,使用itertools.product

import itertools  

category_list = ['cat', 'dog', 'squirrel']
color_list = ['brown', 'black']
age = [1,2,3]

df=pd.DataFrame.from_records(
    list(itertools.product(
        category_list, color_list, age
    )), columns=['category', 'color', 'age'])

df.apply(lambda row: call_function(row['category'], row['color'], row['age']), axis=1)

结果:

cat brown 1
cat brown 2
cat brown 3
cat black 1
cat black 2
cat black 3
dog brown 1
dog brown 2
dog brown 3
dog black 1
dog black 2
dog black 3
squirrel brown 1
squirrel brown 2
squirrel brown 3
squirrel black 1
squirrel black 2
squirrel black 3

如果函数命名为my_function

df.apply(lambda row: MY_FUNCTION(row['category'], row['color'], row['age']), axis=1)

“

here is another way to do it, using pd.apply, after creating a dataframe using itertools.product

import itertools  

category_list = ['cat', 'dog', 'squirrel']
color_list = ['brown', 'black']
age = [1,2,3]

df=pd.DataFrame.from_records(
    list(itertools.product(
        category_list, color_list, age
    )), columns=['category', 'color', 'age'])

df.apply(lambda row: call_function(row['category'], row['color'], row['age']), axis=1)

RESULT:

cat brown 1
cat brown 2
cat brown 3
cat black 1
cat black 2
cat black 3
dog brown 1
dog brown 2
dog brown 3
dog black 1
dog black 2
dog black 3
squirrel brown 1
squirrel brown 2
squirrel brown 3
squirrel black 1
squirrel black 2
squirrel black 3

if function is named MY_FUNCTION

df.apply(lambda row: MY_FUNCTION(row['category'], row['color'], row['age']), axis=1)

enter image description here

我家小可爱 2025-02-17 02:58:42

我通过在循环中调用函数来解决它。我确定有一种更高效/优雅的方法可以做到这一点,但是由于我的DF和列表很小,因此效果很好。想为那些正在处理小列表并遇到类似问题的人发布我的解决方案。当然,欢迎改进建议。

for i in category_list: 
    for j in color_list: 
        for k in age: 
            call_function(i,j,k)

I solved it by calling the function in a loop. I am sure there is a more efficient/elegant way to do this, but since my df and lists are small, it works good. Wanted to post my solution for those who are working with small lists and having similar problem. Of course, improvement suggestions are welcome.

for i in category_list: 
    for j in color_list: 
        for k in age: 
            call_function(i,j,k)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文