Python - 迭代 list.append 的结果

标签 python list

为什么我不能做这样的事情:

files = [file for file in ['default.txt'].append(sys.argv[1:]) if os.path.exists(file)]

最佳答案

list.append 在 Python 中不返回任何内容:

>>> l = [1, 2, 3]
>>> k = l.append(5)
>>> k
>>> k is None
True

你可能想要这个:

>>> k = [1, 2, 3] + [5]
>>> k
[1, 2, 3, 5]
>>> 

或者,在您的代码中:

files = [file for file in ['default.txt'] + sys.argv[1:] if os.path.exists(file)]

关于Python - 迭代 list.append 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5390352/

相关文章:

python - 如何检查一个元素是否等于多维列表的特定索引

来自矩阵的选定行的 R_List

python - 使用 Python 比较两个不同的 csv 列时无法获取缺失的元素

python - 如何生成Taurusdesigner或Qt下创建的GUI的Python代码?

r - 检查列表是否包含 R 中的另一个列表

python - 删除列表中的重复项,同时保持其顺序(Python)

python - MySQL 和 web2py

python多线程无法输出完整结果

python - 异常值: 'User' object has no attribute 'update' thrown in DJANGO ORM

list - 使用 AutoMapper 对特定属性进行自定义转换