【问题标题】:How can I generalize this code in Python?如何在 Python 中概括此代码?
【发布时间】:2021-12-27 09:35:24
【问题描述】:

我有一个代码,我想用 Python 概括,我想要的只是分离这个输出,以便在一个唯一的邮件中发送每一行。

提前谢谢你,

这是我的代码。

    art = [[600, 'pet', 'small'], [300, 'pet', 'medium'], [600, 'pet', 'medium'], [200, 'pet', 'big'], [300, 'pet', 'big'], [200, 'pet', 'small'], [600, 'pet', 'big']]

    art_600 =[]
    art_300 =[]
    art_200 =[]

    for i in range(len(art)):
        if art[0][0] == art[i][0]:
            art_600.append(art[i])
        if art[1][0] == art[i][0]:
            art_300.append(art[i])
        if art[-2][0] == art[i][0]:
            art_200.append(art[i])

    print(art_600)
    print(art_300)
    print(art_200)

【问题讨论】:

    标签: python for-loop if-statement


    【解决方案1】:

    尝试制作dict并遍历

    for key in dict_:
        # do your code
        pass
    

    【讨论】:

    • 谢谢@Andrey,我会测试这个想法。
    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 2014-07-20
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多