无法获得重新创建的范围函数以否定步骤打印列表。它还会打印一个额外的数字

时间:2019-10-11 01:54:07

标签: python python-3.x list function

已指示我创建一个与范围函数具有相同结果的函数,而无需实际使用范围函数。我有它,所以它打印升序列表,但是当我尝试使其以-1的步长打印降序列表时,它会打印一个空列表。最后我还会得到一个额外的数字,请检查下面的测试行。

var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Raw Data");
var wsFeedback = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Drop Downs - Feedback");
var feedback = wsFeedback.getRange(2, 1,wsFeedback.getLastRow()-1,2).getValues();
var wsTechnical = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Drop Downs - Technical");
var technical = wsTechnical.getRange(2, 1,wsTechnical.getLastRow()-1,2).getValues();

function onEdit(e){
  var activeCell = e.range;
  var val = activeCell.getValue();
  var r = activeCell.getRow();
  var c = activeCel1.getColumn();
  var wsName = activeCell.getSheet().getName();
  if(wsName == "Raw Data" && c === 4 &&  r > 2){

    if(val === ""){
      ws.getRange(r, 5).clearContent();
      ws.getRange(r, 5).clearDataValidations();
    } else {
      ws.getRange(r, 5).clearContent();
      var filteredFeedback = feedback.filter(function(f){ return f[0] === val });
      var listToApply = filteredFeedback.map(function(f){ return f[1] });
      var cell = ws.getRange(r, 5);
      applyValidationToCell(listToApply,cell);
    }
  }
}

function onEdit(e){
  var activeCell = e.range;
  var val = activeCell.getValue();
  var r = activeCell.getRow();
  var c = activeCell.getColumn();
  var wsName = activeCell.getSheet().getName();
  if(wsName == "Raw Data" && c === 6 &&  r > 2){

    if(val === ""){
      ws.getRange(r, 7).clearContent();
      ws.getRange(r, 7).clearDataValidations();
    } else {
      ws.getRange(r, 7).clearContent();
      var filteredTechnical = technical.filter(function(t){ return t[0] === val });
      var listToApply = filteredTechnical.map(function(t){ return t[1] });
      var cell = ws.getRange(r, 7);
      applyValidationToCell(listToApply,cell);
    }
  }
}


function applyValidationToCell(list,cell){

  var rule = SpreadsheetApp
  .newDataValidation()
  .requireValueInList(list)
  .setAllowInvalid(false)
  .build();

  cell.setDataValidation(rule)
}

这些是功能的测试行。除了最后一个作品,所有作品。

def myRange(start = 0, stop = 0, step = 1):
    number = start
    number_list = []
    if start == stop:
        return []
    while number <= stop:
        number_list.append(number)
        number = number + step
    return number_list

myRange(4,1,-1)打印一个空列表,而不是[4,3,2]

这是当前输出

print(myRange(2,4, 6))
print(myRange(2,4, 1))
print(myRange(0,5,1))
print(myRange(4,1,-1))

这是正确的输出

[2]
[2, 3, 4]
[0, 1, 2, 3, 4, 5]
[]

1 个答案:

答案 0 :(得分:0)

使用 if 语句

def myRange(start = 0, stop = 0, step = 1):
    number = start
    number_list = []
    if step > 0 :
        while number <= stop:
            number_list.append(number)
            number = number + step
    elif step < 0:
        while number >= stop:
            number_list.append(number)
            number = number + step
    return number_list