所以我从MySQL数据库中获取了一个项目列表,使用了Python,Flask,Flask-SQLAlchemy。我已经像这样增加了这个列表的行:
for file in files:
file.last_viewed_time = datetime.utcnow()
db.session.commit()
但是我想从这个列表(文件)中提取另外一个东西,一个另一个属性的列表。 如何将这些添加到新列表中,例如:
mylist = []
for file in files:
new = file.attribute
add new to mylist
或者这是一种令人费解的方式,我可以更好地复制这些值吗?
答案 0 :(得分:4)
使用list.append
:
mylist = []
for f in files:
new = f.attribute
mylist.append(new)
mylist = [f.attribute for f in files]
答案 1 :(得分:1)
mylist = [f.attribute for f in files] # don't use file, it's the name of a builtin-function
答案 2 :(得分:0)
试试这个
mylist = []
for file in files:
new = file.attribute
mylist.append(new)