0
votes

I have this basic code in which I'm just trying to compare each tuple in the first list (list1) with the corresponding tuple in the second list (list 2). If the tuple in list 2 is = to the corresponding tuple in list1 minus the '.vbproj' then take both tuples and return them.

Then I need to print path + the tuple from list2 + the tuple from list1. I'm just stuck on how to do this.

path = "C:\Users\bg\Documents\Brent"

list1 = [ 'Brent.vbproj', 'Chris.vbproj', 'Nate.vbproj']
list2 = ['Brent', 'Chris', 'Nate']

def connect(list1, list2):

    for x, y in zip(string[0], string2[0]):
        if string(x) is string2(y):
            print x
            os.path.join(path, x, y)




x = connect(list1, list2)
y = connect(list1, list2)

I thought the zip() compared both tuples up to the minimal equivalence but I could be wrong?? I don't know, any help would be greatly appreciated. Thanks in advance!

2
what is purpose of print after return?oleg
Did you mean to call connect with list1 and list2 perhaps?Martijn Pieters
@oleg I just want to see if it's working properly or notbbesase
I think You should use os.path.join(path, x, y) instead of path + "\\" + x + "\\" + yoleg
in this example the code after return in function will never runoleg

2 Answers

1
votes

Use == to test for equality. is tests for identity, the two sides being the same object. Also, your inputs string and string2 are not functions, so you cannot call them. Just compare x and y directly:

if x == y:

Note that the function ends when you call return. The print statement on the next line is ignored, and the for loop ends as well.

Last but not least, you are only zipping the first elements of string and string2. I suspect you wanted to call this with list1 and list2, at which point you probably wanted to pair up first 'Brent.vbproj' and 'Brent', then 'Chris.vbproj' and 'Chris', etc. If so, just pass the lists without indexing:

for x, y in zip(string, string2):

I doubt that you'll achieve what you want though; none of the value pairs from list1 and list2 will ever be equal.

Perhaps you wanted to look at the str.startswith() method? Also, the os.path library has functions you want to be familiar with if you are manipulating and testing filenames and paths. The os.path.join(), os.path.splitext() and os.path.commonprefix() functions should be of particular interest to what I think you are trying to do here.

Note that your path variable also needs adjusting. Use a raw string, forward slashes or double the slashes:

path = r"C:\Users\bg\Documents\Brent"
path = "C:\\Users\\bg\\Documents\\Brent"
path = "C:/Users/bg/Documents/Brent"

as \b is the escape code for a backspace.

1
votes

you haven't defined string and string2 !! anyhow I understood your problem from your question text!!

comparing a tuple in list1 with corresponding tuple in list2, I myself as a beginner will do it in another way..

>>> path = r"C:\Users\bg\Documents\Brent"
>>> list1 = [ 'Brent.vbproj', 'Chris.vbproj', 'Nate.vbproj']
>>> list2 = ['Brent', 'Chris', 'Nate']
>>> for i in range(0, len(list1)):
    if i < len(list2):
        if list2[i][:] == list1[i][:len(list2[i])]:
            print(path + list2[i] + list1[i]) #Print syntax is for python 3


C:\Users\bg\Documents\BrentBrentBrent.vbproj
C:\Users\bg\Documents\BrentChrisChris.vbproj
C:\Users\bg\Documents\BrentNateNate.vbproj
>>>