3

I am having a list of file names created using dirs = os.listdir(dir/path).

Also I am exporting a data set from a csv file. stocks = csv.reader(open('config.csv', 'rb'))

Sample stock data:

fileName1,url1

fileName2,url2

What I need to do is get the each url for each file in the dirs list in a python script. Thanks in advance.

2
  • What do you mean by get each url? Are you going to make a web request? Commented Jan 24, 2014 at 9:47
  • Ok, so you want all entries from 'stocks' where the fileName appears in 'dirs'? Commented Jan 24, 2014 at 9:48

2 Answers 2

6

Pytho provides a built-in function for this, zip:

for dir, stock, in zip(dirs, stocks)

Demo:

>>> a = ["cat", "dogs"]
>>> b = ["http://cat-service.com", "http://dogs-care.com"]
>>> for animal, site in zip(a, b):
    print(animal, site)


cat http://cat-service.com
dogs http://dogs-care.com

See, It's not hard, right? Python makes our life easier! :)

Alternative: If performance really matters, use itertools.izip.

Update

Turns out the answer above is not what the OP was asking. Well, same as Steve, I'd use a dictionary for this:

stock_dict = dict(stock)
urls = [stock_dict.get(file, "Not found") for file in dirs]

Demo:

We assume there are 3 files in the directory, two of them are listed.

>>> stock = [("fileName1","url1"), ("fileName2","url2")]
>>> stock_dict = dict(stock)
>>> dirs = ["fileName1", "fileName2", "fileName3"]
>>> urls = [stock_dict.get(file, "Not found") for file in dirs]
>>> urls
['url1', 'url2', 'Not found']

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think it's what @Yasitha wanted. At least your code doesn't work right if theres fileName1, fileName3 but not fileName2 in the path.
Thanks for the answer. What I am looking for is get the url when the file name match with name in the stock data. Eg: Lets assume there is a record called "file1" in the dirs and I want to Iterate through the stocks and if there is a record for file1 get the url. Need to do this for all elements in dirs.
2

Assuming everything fits in memory:

stockslookup = dict(stocks)
urls = [stockslookup[filename] for filename in dirs]

Note that the filename has to be exactly the same string, so you might want to use os.path.abspath on both (or os.path.normpath).

4 Comments

That's a nice way to do it. I was thinking about something with list comprehension, but this is way smarter!
@EarlGrey: if both lists were sorted then you could do a merge-like operation that's O(n+m) time and O(1)-ish memory (ish becomes actually if you assume a fixed upper limit on filename length in the file...). But assuming you have the memory budget, dict is the "obvious" way to look up anything :-)
I'm actually a bit confused about what the OP is asking.
@alKid: I think the "sample stock data" in the question is the contents of config.csv, and the request is to look up each filename in the file to get its corresponding url. But obviously if I'm wrong then my code is wrong :-)

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.