Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Conditional list comprehension

Sorry to ask so much question at the border between Sage and Python, but I try many attempt before asking.

The following code use list comprehension

A=[[3, 2, 1, 1, 0, 0, 0], [4, 3, -1, 0, -1, 1, 0]]
B=[3,5]
AA=[v[:i] for i in B  for v in A]
AA

the result is

[[3, 2, 1], [4, 3, -1], [3, 2, 1, 1, 0], [4, 3, -1, 0, -1]]

but the result I want is [[3, 2, 1],[4, 3, -1, 0, -1]] that is B[0] is applied to A[0] and B[1] is applied to A[1] and so on for larger exemple. Is there a way to give some conditions in list comprehension (I would like not to use loop)

Conditional list comprehension

Sorry to ask so much question questions at the border between Sage and Python, but I try many attempt before asking.

The following code use list comprehension

A=[[3, 2, 1, 1, 0, 0, 0], [4, 3, -1, 0, -1, 1, 0]]
B=[3,5]
AA=[v[:i] for i in B  for v in A]
AA

the result is

[[3, 2, 1], [4, 3, -1], [3, 2, 1, 1, 0], [4, 3, -1, 0, -1]]

but the result I want expect is [[3, 2, 1],[4, 3, -1, 0, -1]] that is B[0] is applied to A[0] and B[1] is applied to A[1] and so on for larger exemple. Is there a way to give some conditions in list comprehension (I would like not to use loop)

Conditional list comprehension

Sorry to ask so much questions at the border between Sage and Python, but I try many attempt before asking.

The following code use list comprehension

A=[[3, 2, 1, 1, 0, 0, 0], [4, 3, -1, 0, -1, 1, 0]]
B=[3,5]
AA=[v[:i] for i in B  for v in A]
AA

the result is

[[3, 2, 1], [4, 3, -1], [3, 2, 1, 1, 0], [4, 3, -1, 0, -1]]

but the result I expect is [[3, 2, 1],[4, 3, -1, 0, -1]] that is B[0] is applied to A[0] and B[1] is applied to A[1] and so on for larger exemple. exemples. Is there a way to give some conditions in list comprehension (I would like not to use loop)