l1 #list to evaluate
[100, 11, 12, 3, 4, 12, 3, 10, 100, 11, 100, 2, 200]
// list with numbers even postion on the list l1
[l1.index(c) for c in [a for a in l1 if a % 2==0]]
[0, 2, 4, 2, 7, 0, 0, 11, 12]
// list list with the value
[l1[e] for e in [l1.index(c) for c in [a for a in l1 if a % 2==0]]]
[100, 12, 4, 12, 10, 100, 100, 2, 200]
a dict with index and value
>>> {e:l1[e] for e in [l1.index(c) for c in [a for a in l1 if a % 2==0]]}
{0: 100, 2: 12, 4: 4, 7: 10, 11: 2, 12: 200}
Or other dict with value and index
>>> {l1[e]:l1.index(l1[e]) for e in [l1.index(c) for c in [a for a in l1 if a % 2==0]]}
{100: 0, 12: 2, 4: 4, 10: 7, 2: 11, 200: 12}
using lambda
>>> test= lambda l1:{e:l1[e] for e in [l1.index(c) for c in [a for a in l1 if a % 2==0]]}
>>> l2=[100,32,34,13,3]
>>> test(l2)
{0: 100, 1: 32, 2: 34}
>>> test([100,33,53,18,98,67])
{0: 100, 3: 18, 4: 98}
>>>
No hay comentarios:
Publicar un comentario