0

I'm trying to generate some predictable URL's for a crawler.

I have a set of base urls:

base_urls = ['http://something.com/john', 
             'http://something.com/sally']

and each one has a unique url for their daily calendar:

to_append = ['-mon-calendar', 
             '-tues-calendar', 
             '-wed-calendar', 
             '-thurs-calendar', 
             '-fri-calendar']

I need to generate a new array containing a complete list of the all people's weekly calendars (e.g. 'http://something.com/john-mon-calendar',...

I could iterate through this the long way but assume it's possible to do it more efficiently with map(). I haven't used map much and doing it with two iterables is throwing me off. Could anyone point me in the right direction?

2 Answers 2

8

You can use itertools.product to create the Cartesian product between your lists, then join each item within a list comprehension.

>>> from itertools import product
>>> [''.join(i) for i in product(base_urls, to_append)]
['http://something.com/john-mon-calendar',
 'http://something.com/john-tues-calendar',
 'http://something.com/john-wed-calendar',
 'http://something.com/john-thurs-calendar',
 'http://something.com/john-fri-calendar',
 'http://something.com/sally-mon-calendar',
 'http://something.com/sally-tues-calendar',
 'http://something.com/sally-wed-calendar',
 'http://something.com/sally-thurs-calendar',
 'http://something.com/sally-fri-calendar']
Sign up to request clarification or add additional context in comments.

Comments

1

No libraries:

links = [[url + day for day in to_append] for url in base_urls]
links_flattened = sum(links, [])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.