<edit> Thanks to everyone who has answered so far. The zip and os.path.join are really helpful. Any suggestions on ways to list the counter in front, without doing something like this:
zip(range(len(files)), files, directories)
</edit>
Hi,
I'm in the process of learning Python, but I come from a background where the following pseudocode is typical:
directories = ['directory_0', 'directory_1', 'directory_2']
files = ['file_a', 'file_b', 'file_c']
for(i = 0; i < directories.length; i++) {
print (i + 1) + '. ' + directories[i] + '/' + files[i] + '\n'
}
# Output:
# 1. directory_0/file_a
# 2. directory_1/file_b
# 3. directory_2/file_c
In Python, the way I would write the above right now, would be like this:
directories = ['directory_0', 'directory_1', 'directory_2']
files = ['file_a', 'file_b', 'file_c']
for i in range(len(directories)):
print '%s. %s/%s' % ((i + 1), directories[i], files[i]
# Output:
# 1. directory_0/file_a
# 2. directory_1/file_b
# 3. directory_2/file_c
While reading Dive into Python, Mark Pilgrim says that using for loops for counters is "Visual Basic-style thinking" (Simple Counters). He goes on to show how to use loops with dictionaries, but never really addresses a python solution in regards to how for loop counters are typically used in other languages.
I was hoping somebody could show me how to properly write the above scenario in Python. Is it possible to do it a different way?
If I took out the incrementing line count, is it possible to just match the two lists together using some kind of list comprehension?
For example, if all I wanted from the output was this (no counters, is that possible with list comprehension):
# Output:
# directory_0/file_a
# directory_1/file_b
# directory_2/file_c
Thanks in advance for any help.