python - 哪一个更喜欢 - line.lower() 与 word.lower()

标签 python coding-style

当我需要拆分一行,并将列表中的所有单词都设为小写时,首选方式是:

1.)

    list = []
    for word in line.split():
        word = word.lower()
        list.append(word)

2.)

    list = []
    for word in line.lower().split():
        list.append(word)

对性能有影响吗?它真的有什么不同吗(除了第二个更短)。

编辑 添加了缺失的 split()

最佳答案

也许更紧凑的东西:

>>> a = "I AM A DEVELOPER"

>>> a.lower().split()
['i', 'am', 'a', 'developer']

关于python - 哪一个更喜欢 - line.lower() 与 word.lower(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4546879/

相关文章:

c - K&R 中的练习 1-24 - 基本语法检查

c# - boolean getter/setter 方法和属性的语法

c# - 使用空合并运算符的独特方式

python - 列表成员的细节

python - reshape Pandas Dataframe 的最佳方式

python - 我可以在分区的配置单元表上使用 mrjob python 库吗?

c# - 使用初始化 block 不好吗

python - python 中的 sudo renice

python - 为什么底部打印顺序不正确?

c - 为什么 Kernighan 和 Ritchie 包含看似不必要的类型转换?