Home > Net >  Inline for-loop python return
Inline for-loop python return

Time:11-18

def example():
   word_ids = [2,3,4,5,6]
   boundary = 2
   return [(u, v, v*2) for i, u in enumerate(word_ids) for j, v in
                        enumerate(word_ids[max(i - boundary, 0):i   boundary   1]) if u != v]

a = example()
print(a)

This return:

[(2, 3, 6), (2, 4, 8), (3, 2, 4), (3, 4, 8), (3, 5, 10), (4, 2, 4), (4, 3, 6), (4, 5, 10), (4, 6, 12), (5, 3, 6), (5, 4, 8), (5, 6, 12), (6, 4, 8), (6, 5, 10)]

Can someone help me to get this using an inline function like above:

[(2, [3,4], [6,8]), (3, [2,4,5], [4,8,10]), (4, [2,3,5,6] [4,6,10,12]), (5, [3,4,6], [6,8,12]), (6, [4,5], [8,10])]

CodePudding user response:

def sample():
  values = []
  possible = [2,3,4,5,6]
  popping = [2,3,4,5,6]
  boundary = 2
  for index, num in enumerate(possible):
    popping.pop(index)
    values.append((num,popping[max(index-2,0):index boundary 1],[x*num for x in popping[max(index-2,0):index boundary 1]]))
    popping = [2,3,4,5,6]
  return values

I’m working on a simpler version right now

  • Related