如何在一行中多次将变量设置为一个值?

时间:2014-03-01 21:45:54

标签: python

我有这段代码:

...

COMPANY = list(lines[30])
COMPANY = ''.join(COMPANY)
COMPANY = COMPANY.replace(",", "")
COMPANY = COMPANY.replace("\n", "")
COMPANY = COMPANY.split(" : ")
COMPANY[1] = COMPANY[1].lower()

...

我想知道我是否可以在一行中设置COMPANY而不是上面五行。

2 个答案:

答案 0 :(得分:2)

只需链接方法调用:

COMPANY = ''.join(list(lines[30])).replace(",", "").replace("\n", "").split(" : ")
COMPANY[1] = COMPANY[1].lower()

如果您不喜欢它的外观,可以将它分成多行:

COMPANY = ''.join(list(lines[30]))              \
            .replace(",", "").replace("\n", "") \
            .split(" : ")

答案 1 :(得分:0)

我相信这应该有效:

COMPANY = ''.join(list(lines[30])).replace(",","").replace("\n","").split(" : ")
COMPANY[1] = CAMPANY.lower()