使用python在一行中转换许多过程代码行

时间:2017-02-18 15:12:12

标签: python python-2.7 python-3.x

这是一个程序,我想把它转换成一行:

def fix_machine(debris, product):
    i=0
    while(i<len(product)):
        if(debris.find(product[i]) == -1):
            return "Give me something that's not useless next time."
            break
        i = i + 1
    return product

2 个答案:

答案 0 :(得分:0)

debris = 'widget_z'
product = ['widget_a', 'widget_b', 'widget_c', 'widget_d']

if debris not in product: print("Give me something that's not useless next time.")

答案 1 :(得分:0)

更好地解释下次debrisproduct的内容,但我认为你想要这个:

def fix_machine(debris, product):    
    if set(product) <= set(debris):
        return product
    else:
        return "Give me something that's not useless next time."

我认为debrisproduct现在都是字符串。