python一行语句for if with else

时间:2016-10-06 10:35:11

标签: python

是否可以在一行语句中编写此逻辑?

B = []
for book in books:
    if book not in B:
         B.append(book)

我试过这个,但这是假的:

B = [book if book not in B for book in books] 

有没有办法在自身内部引用变量?

1 个答案:

答案 0 :(得分:-3)

"use strict";

function datasetBarChosen(group, callbackFunction) {
  //this can be refactored more
  var ds = [];
  d3.csv("https://raw.githubusercontent.com/openmundi/world.csv/master/countries(204)_olympics.csv").row(function (d) {
    return d["Code"] === group ? d : false;
  }) //get only items with code===group
  .get(callbackFunction);
}

function doThisWithResult(error, rows) {
  console.log(rows);
}

datasetBarChosen("ZIM", doThisWithResult);
//dataBarChosen takes a filter string to filter result
//and a function to be called with resulting rows

应该做的伎俩