将输出循环到列表或numpy数组,以及一个NoneTypeError

时间:2018-11-16 13:27:22

标签: python loops numpy

这是我的代码:

{{#if renderList}}
    <ol>
  {{#each languages as |language|}}
    <li>{{language}}</li>
  {{/each}}
  </ol>
{{else}}
  <table>
    <th>Language</th>
      {{#each languages as |language|}}
      <tr>{{language}}</tr>
    {{/each}}
  </table>
{{/if}}

我花了很长时间才达到循环的这个阶段,而这仍然不是我想要的!

我想为变量rt和ht的每次循环获取一个列表或值数组。目前,这可以正常工作并为循环打印正确的值,但也会导致NoneTypeError。如果我添加# Libs import numpy as np # Isostatic model: def airy(t,tref=35.,rhoM=3.2,rhoC=2.7,mode=None,rate=None,hlimit=None): # Equilibrium ratios: er = rhoC / (rhoM-rhoC) di = rhoC/rhoM # Static buoyancy equation: excess = t-tref hi = excess * (1-di) ri = excess - hi # Mode results: if mode == 'Initial': print("Model parameters: Crustal density =",rhoC,"Mantle density =",rhoM) print("Mountain height (Km) =",np.round(hi, 3)) print("Root thickness (Km) =",np.round(ri, 3)) print("Ratio of height to root =",er) return ri, hi elif mode=='Erosive': # Initialise loop counter = 0 ht = hi while ht >= hlimit: counter = counter+1 excess = t-tref ht = excess * (1-di) rt = excess - ht ht = ht*np.exp(rate*counter) t = ht+rt+tref print(rt, ht, counter) elif mode==None: return ri, hi tref = 35. it = tref*1.5 print("Initial thickness =",it) ir, ih = airy(it, mode='Initial') rt, ht, tstep = airy(it, mode='Erosive', rate=-0.025,hlimit=0.5) ,则结果是单个值,而不是列表/数组。

所以我看到了this的答案,但理想情况是希望在一个函数中使用它,而我看不到在循环中应该在哪里使用列表。

任何帮助将不胜感激!谢谢

2 个答案:

答案 0 :(得分:2)

您忘了返回三值元组:

def airy(t,tref=35.,rhoM=3.2,rhoC=2.7,mode=None,rate=None,hlimit=None):
  # Equilibrium ratios:
  er = rhoC / (rhoM-rhoC)
  di = rhoC/rhoM
  # Static buoyancy equation:
  excess = t-tref
  hi = excess * (1-di)
  ri = excess - hi  
  # Mode results:
  if mode == 'Initial':
    print("Model parameters: Crustal density =",rhoC,"Mantle density =",rhoM)
    print("Mountain height (Km) =",np.round(hi, 3))
    print("Root thickness (Km) =",np.round(ri, 3))
    print("Ratio of height to root =",er)
    return ri, hi
  elif mode=='Erosive':
    # Initialise loop
    counter = 0
    ht = hi
    l_ht = []
    l_rt_ = []
    l_counter = []
    while ht >= hlimit:
      counter = counter+1
      excess = t-tref
      ht = excess * (1-di)
      rt = excess - ht
      ht = ht*np.exp(rate*counter)
      t = ht+rt+tref
      print(rt, ht, counter)
      l_rt.append(rt)
      l_ht.append(ht)
      l_counter .append(counter)
    return l_rt, l_ht, l_counter # HERE
  elif mode==None:
    return ri, hi

否则将返回None,否则将无法打开包装。

答案 1 :(得分:1)

$keys = array_column($arr2, null, 'id');

$newArr = array_map(function($item) use($keys){
    $item['attr1'] = $keys[$item['id']]['attr1'];
    $item['attr2'] = $keys[$item['id']]['attr2'];
    // and so on
    return $item;
}, $arr1);