On 11/16/2014 2:57 AM, Garrett Berg wrote:
(how often have you iterated over a string?)
Often enough, but perhaps more often have written functions for which a
string is as valid an input as many other iterables.
def cross(iterable, reiterable):
for a in iterable:
for b in reiterable:
yield a, b
print(list(cross('ab', 'cde')))
# [('a', 'c'), ('a', 'd'), ('a', 'e'), ('b', 'c'), ('b', 'd'), ('b', 'e')]
For me, there is no point in excluding such input and output.
(And yes, the code above could be enhanced by at least checking that
reiterable is *not* an iterator, or by replacing the second arg, renamed
iterable2, with list(iterable2), but this is besides the point.)
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list