0

I am learning python for customizing an application which uses Ironpython 2.7.4. My inputs are as follows:

nids = [1,2,3,4,5,6]
enids = [1,2,2,8,3,4,5,5,6,7,7]
s = [44, 33, 68, 76, 43, 43, 53, 55, 69, 45, 31]
t = [50, 40, 30, 10, 10, 60, 50, 80, 90, 10, 30]

I want elements from enids which are in nids. Also I want elements from s and t at the index of elements from enids which are in nids. I came up with following list comprehensions:

enids = [enid for enid in enids if enid in nids]
s = [s[i] for i, enid in enumerate(enids) if enid in nids]
t = [t[i] for i, enid in enumerate(enids) if enid in nids]

Since all the above list comprehension uses same for and if conditions I wonder if there is any way to simplify the code and save computation time.

1 Answers1

0

Examine:

all = [(enid, s[i], t[i]) for i,enid in enumerate(enids) if enid in nids]

it gives similar results, but in tuple:

(1, 44, 50),
 (2, 33, 40),
 (2, 68, 30),
 (3, 43, 10),
 (4, 43, 60),
 (5, 53, 50),
 (5, 55, 80),
 (6, 69, 90)]

Nour-Allah Hussein
  • 1,439
  • 1
  • 8
  • 17