50日目の質問


問題を解く
最初の質問です.
namelist([ {'name': 'Bart'}, {'name': 'Lisa'}, {'name': 'Maggie'} ])
#returns 'Bart, Lisa & Maggie'
namelist([ {'name': 'Bart'}, {'name': 'Lisa'} ])
#returns 'Bart & Lisa'
namelist([ {'name': 'Bart'} ])
#returns 'Bart'
namelist([])
#returns ''
def namelist(names):
  el=[]
  fi = []
  for i in names:
    el.append(i['name'])
  if len(el)<=1:
    return "".join(el)
  else:
    two = ' & '.join(el[-2:])
    for i in el[:-2]:
      fi.append(i + ',')
    fi.append(two)
    return " ".join(fi)
2つの空のリストを作成した後、最初のリストで入力値のbellクラス値を受け入れ、長さが1の場合は直接戻り、そうでない場合は名前の入力が2つの場合、fiは空のリストであり、2つの戻りを加え、名前が強い場合はfiに2つを加え、その後に戻る
2番目です.
delete_nth ([1,1,1,1],2) # return [1,1]
delete_nth ([20,37,20,21],1) # return [20,37,21]
  def delete_nth(order,max_e):
    count = {}
    res = []
    for i in order:
      if i not in count :
        count[i] = 0
      else:
        count[i] += 1
      if count[i] <max_e:
        res.append(i)
    return res
繰り返し文でorderの値をdict形式に変換します.次にcount[i]の値がmax eより小さい場合、結果リストにiを追加します.