在Python中,我有一个列名称列表,其格式如下:
private static async Task DoesItExist(IMongoCollection<MyDocument> collection, MyDocument document, DateTime oldDateModified)
{
Thread.Sleep(500);
var fromDatabaseCursor = await collection.FindAsync(d => d.Id == document.Id && d.DateModified == oldDateModified);
var fromDatabaseDoc = await fromDatabaseCursor.FirstOrDefaultAsync();
if (fromDatabaseDoc != null)
{
Console.WriteLine("But it was found!");
}
else
{
Console.WriteLine("And wasn't found!");
}
}
和一个数组,它描述了应该使用哪些列并具有以下形式:
columns_names = ['example1', 'example2', 'example3', 'example4', 'example5']
我想创建一个新列表,其中只包含我想要使用的列(数组值为True)。
columns_used = [False False True False True]
答案 0 :(得分:2)
简单地说:
[v for i,v in enumerate(column_names) if columns_used[i] == True] # ['example3', 'example5']