Reputation: 31
The following is the code I wrote:
def comb(self, rows, cols):
return [s+t for s in a for t in b]
If the values of rows
and cols
are
rows = ['abc','efg']
cols = ['123','456']
Expected output: ['abc123','abc456,'efg123','efg456']
Program output: ['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']
I am new to Python programming. Could you help me understand what is happening? I already fixed the output but I would like to understand why this happened.
Upvotes: 2
Views: 161
Reputation: 31
It was an error in my code because instead of taking the string as it is I was using each string as an input for the comb function
def comb(self, rows, cols): return [s+t for s in a for t in b]
rows = ['abc','efg'] cols = ['123','456']
print [comb(rs, cs) for rs in rows for cs in cols]
so the output was ['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']
in this function, it would take each character of the string and the output will be a combination of the characters instead of the string.
But thank you for trying to help. I really appreciate it.
Upvotes: 0
Reputation: 1004
The itertools
library is a popular Python standard library containing many tools that help you iterate over different ways of combining itrators (such as your given rows
and cols
lists).
import itertools
def comb(rows, cols):
return map(lambda t: t[0] + t[1], itertools.product(rows, cols))
itertools.product
will give you an iterator full of tuples which give you all the possible combinations of the entries in rows
as the first element, and the entries in cols
as the second element.
map
uses the lambda
function given to join the two strings in the tuple produced by itertools.product
into one string, which is what you want.
To get the list that you mentioned you want, you can wrap the call to map
in the function list()
to evaluate the map
-produced iterator into a list. Fyi, for large lists, this will be inefficient, as the result of combining the substrings will all live in memory, whereas if you use the iterator provided by the first def comb
I gave you, when you iterate over those iterators, you'll use rules to generate each entry as you go.
Upvotes: 0
Reputation: 446
change it to this:
rows = ['abc','efg']
cols = ['123','456']
def comb(rows, cols):
return [s+t for s in rows for t in cols]
print(comb(rows,cols))
output:
['abc123', 'abc456', 'efg123', 'efg456']
Upvotes: 0
Reputation: 4690
Try using the zip()
function:
>>> rows = ['abc','efg']
>>> cols = ['123','456']
>>> def comb(rows, cols):
return [r+c for r, c in zip(rows, cols)]
>>> comb(rows, cols)
['abc123', 'efg456']
The zip()
function, in essence, pairs each value in rows
with each value in cols
.
>>> list(zip(rows, cols))
[('abc', '123'), ('efg', '456')]
On the other hand, [s+t for s in a for t in b]
is a nested for
loop, with iterations of a
nested in iterations of b
.
Upvotes: 0
Reputation: 1845
To understand what your list comprehension is doing, you can rewrite it like this:
results = []
for s in a:
for t in b:
results.append(s+t)
Presumably that's not what you want.
Upvotes: 2