Reputation: 47
I am struck on an awkward lists comprehension problem, which I am not able to solve. So, I have two lists looking like the following:
a=[[....],[....],[....]]
b=[[....],[....],[....]]
len(a)==len(b) including sublists i.e sublists also have the same dimension.
Now I want to do a re.compile which looks something like:
[re.compile(_subelement_in_a).search(_subelement_in_b).group(1)]
and I am wondering how I can achieve the above using list compherension - something like:
[[re.compile(str(x)).search(str(y)).group(1) for x in a] for y in b]
..but obviously the above does not seem to work and I was wondering if anyone could point me in the right direction.
EDIT
I have just realized that the sublists of b have more elements than the sublists of a. So, for example:
a=[[1 items],[1 items],[1 items]]
b=[[10 item], [10 item], [10 item]]
I would still like to do the same as my above question:
[[re.compile(str(x)).search(str(y)).group(1) for x in b] for y in a]
and the output looking like:
c = [[b[0] in a[0] items],[b[1] in a[1] items],[b[2] in a[2] items]]
Example:
a=[["hgjkhukhkh"],["78hkugkgkug"],["ukkhukhylh"]]
b=[[r"""a(.*?)b""",r"""c(.*?)d""",r"""e(.*?)f""",r"""g(.*?)h""",r"""i(.*?)j"""],[r"""k(.*?)l""",r"""m(.*?)n""",r"""o(.*?)p""",r"""q(.*?)r"""],[r"""s(.*?)t""",r"""u(.*?)v""",r"""x(.*?)y""",r"""z(.*?)>"""]]
using one to one mapping. i.e check if:
elements of sublists of b[0] are present in sublist element of a[0]
elements of sublists of b[1] are present in sublist element of a[1]
elements of sublists of b[2] are presnet in sublist element of a[2]
Upvotes: 0
Views: 1204
Reputation: 69934
Sounds like you are looking for zip
? It takes a pair of lists and turns it into a list of pairs.
[
[my_operation(x,y) for x,y in zip(xs, ys)]
for xs, ys in zip(a, b)
]
-- Edit. Requirements changed:
[
[[regex(p, s) for p in patterns] for s in strings]
for strings, patterns in zip(a, b)
]
Upvotes: 3
Reputation: 179392
Use zip
liberally:
[[re.search(x, y).group(1) for x,y in zip(s,t)] for s,t in zip(a,b)]
The first zip(a,b)
produces lists of sublist pairs. The second zip
pairs the elements in parallel sublists together.
Upvotes: 2