【问题标题】:Why does this auto array adjuster not work?为什么这个自动阵列调整器不起作用?
【发布时间】:2021-04-04 20:58:06
【问题描述】:

我正在做一个新项目,基本上你添加两个列表,你有一个目标,我只想添加列表; list1 & list2 在一起,我有一个目标; target 所以我想预测/配置list2 直到我得到我想要的结果,也就是target

我的代码:

def random_funct(list1,list2,target):
  def __logic__():
    lists_of_lists = [list1, list2]
    out_sum = [sum(x) for x in zip(*lists_of_lists)]
    out = 0
    
    for i in out_sum:
      out+=int(i)
    
    return(out)
  
  while True:
    my_logic = __logic__()
    if my_logic > target:
      for i in range(len(list2)):
        list2[int(i)]+= 1

    elif my_logic < target:
      for i in range(len(list2)):
        list2[int(i)]-= 1
    
    elif my_logic == target:
      print('algorithm settled on '+str(list2))
      break


if __name__ == '__main__':
  random_funct([2,1],[2,1],5)

【问题讨论】:

  • 所需的输出配置为list2 或新的list2,如果您添加list1list2,您将获得目标。
  • 调用random_funct([2,1],[2,1],5)的预期输出是什么?
  • @trincot 预期的输出将是一个配置的 list2,配置的意义在于它将给目标又名5
  • 是的,但请具体说明一下,这个特定的电话会是什么?
  • 它会给出一个数组

标签: python list algorithm


【解决方案1】:

我认为您混淆了 &lt;&gt; 标志...

改用这个:

def random_funct(list1,list2,target):
  def __logic__():
    lists_of_lists = [list1, list2]
    out_sum = [sum(x) for x in zip(*lists_of_lists)]
    out = 0

    for i in out_sum:
      out+=int(i)

    return out

while True:
  my_logic = __logic__()
  if my_logic > target:
      list2[0]-= 1

  elif my_logic < target:
      list2[0]+= 1

  elif my_logic == target:
      print('algorithm settled on '+ str(list2))
      break

现在您只需修改列表中的第一项2。 我认为它可以完成工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多