Ad
Move To Next Item In List1 When Item From List2 Is Found, When Both Lists Are Compared
Sorry for complicated title, perhaps example'd be easier.
So I have two lists:
list1 = ['cat', 'dog', 'donkey']
list2 = ['http://cat1.com', 'http://cat2.com', 'http://dog1.com', http://dog2.com']
All I want is script, that "greps" list2 with list1 items but when it finds one ocurence, it moves to another item in list1, so it finds phrase "cat" in "http://cat1.com", then it checks list[1] item: 'dog'. When it's finish it should print all it's findings. I tried using .find() but it's not designed for list types of files.
Ad
Answer
You can try this:
list1 = ['cat', 'dog', 'donkey']
list2 = ['http://cat1.com', 'http://cat2.com', 'http://dog1.com', 'http://dog2.com']
for item in list1:
for url in list2:
if item in url:
print url
break
Output:
http://cat1.com
http://dog1.com
A more advanced approach could be:
urls = filter(lambda x: x in list2, list1)
print urls
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad