Ensure that same name files will always match?
up vote
1
down vote
favorite
I have two list:
this:
list1
['C:\Users\user\Desktop\programs1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs1\merge\ASTO\ASTO.shp']
and this:
list2
['C:\Users\user\Desktop\programs\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\AWE\AWE.shp', #THIS IS EXTRA
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp']
Then zip:
final = [(f,s) for f,s in zip(list1,list2)]
final
How to ensure that the pairs will match with the corresponding same name on the other list after the zip?
Maybe we match with their previous folder?
Like:
if list1[0].split('\')[-2] == list2[0].split('\')[-2]:
final = [(f,s) for f,s in zip(list1,list2)]
final
python list loops
New contributor
|
show 3 more comments
up vote
1
down vote
favorite
I have two list:
this:
list1
['C:\Users\user\Desktop\programs1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs1\merge\ASTO\ASTO.shp']
and this:
list2
['C:\Users\user\Desktop\programs\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\AWE\AWE.shp', #THIS IS EXTRA
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp']
Then zip:
final = [(f,s) for f,s in zip(list1,list2)]
final
How to ensure that the pairs will match with the corresponding same name on the other list after the zip?
Maybe we match with their previous folder?
Like:
if list1[0].split('\')[-2] == list2[0].split('\')[-2]:
final = [(f,s) for f,s in zip(list1,list2)]
final
python list loops
New contributor
zip
preserves the ordering. It's not clear to me what you are asking.
– roganjosh
Nov 19 at 14:03
in the second list the third item doesn't exist in the first. it will be paired wrong
– user10671234
Nov 19 at 14:06
And so will all subsequent items, then?
– roganjosh
Nov 19 at 14:07
Yes, this is why it needs to do some conditions to ensure that it pairs well.
– user10671234
Nov 19 at 14:09
1
why not[x for x in list1 if x in list2]
? What do you need thezip
for?
– Ev. Kounis
Nov 19 at 14:11
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have two list:
this:
list1
['C:\Users\user\Desktop\programs1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs1\merge\ASTO\ASTO.shp']
and this:
list2
['C:\Users\user\Desktop\programs\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\AWE\AWE.shp', #THIS IS EXTRA
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp']
Then zip:
final = [(f,s) for f,s in zip(list1,list2)]
final
How to ensure that the pairs will match with the corresponding same name on the other list after the zip?
Maybe we match with their previous folder?
Like:
if list1[0].split('\')[-2] == list2[0].split('\')[-2]:
final = [(f,s) for f,s in zip(list1,list2)]
final
python list loops
New contributor
I have two list:
this:
list1
['C:\Users\user\Desktop\programs1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs1\merge\ASTO\ASTO.shp']
and this:
list2
['C:\Users\user\Desktop\programs\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\AWE\AWE.shp', #THIS IS EXTRA
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp']
Then zip:
final = [(f,s) for f,s in zip(list1,list2)]
final
How to ensure that the pairs will match with the corresponding same name on the other list after the zip?
Maybe we match with their previous folder?
Like:
if list1[0].split('\')[-2] == list2[0].split('\')[-2]:
final = [(f,s) for f,s in zip(list1,list2)]
final
python list loops
python list loops
New contributor
New contributor
edited Nov 19 at 14:39
New contributor
asked Nov 19 at 14:02
user10671234
376
376
New contributor
New contributor
zip
preserves the ordering. It's not clear to me what you are asking.
– roganjosh
Nov 19 at 14:03
in the second list the third item doesn't exist in the first. it will be paired wrong
– user10671234
Nov 19 at 14:06
And so will all subsequent items, then?
– roganjosh
Nov 19 at 14:07
Yes, this is why it needs to do some conditions to ensure that it pairs well.
– user10671234
Nov 19 at 14:09
1
why not[x for x in list1 if x in list2]
? What do you need thezip
for?
– Ev. Kounis
Nov 19 at 14:11
|
show 3 more comments
zip
preserves the ordering. It's not clear to me what you are asking.
– roganjosh
Nov 19 at 14:03
in the second list the third item doesn't exist in the first. it will be paired wrong
– user10671234
Nov 19 at 14:06
And so will all subsequent items, then?
– roganjosh
Nov 19 at 14:07
Yes, this is why it needs to do some conditions to ensure that it pairs well.
– user10671234
Nov 19 at 14:09
1
why not[x for x in list1 if x in list2]
? What do you need thezip
for?
– Ev. Kounis
Nov 19 at 14:11
zip
preserves the ordering. It's not clear to me what you are asking.– roganjosh
Nov 19 at 14:03
zip
preserves the ordering. It's not clear to me what you are asking.– roganjosh
Nov 19 at 14:03
in the second list the third item doesn't exist in the first. it will be paired wrong
– user10671234
Nov 19 at 14:06
in the second list the third item doesn't exist in the first. it will be paired wrong
– user10671234
Nov 19 at 14:06
And so will all subsequent items, then?
– roganjosh
Nov 19 at 14:07
And so will all subsequent items, then?
– roganjosh
Nov 19 at 14:07
Yes, this is why it needs to do some conditions to ensure that it pairs well.
– user10671234
Nov 19 at 14:09
Yes, this is why it needs to do some conditions to ensure that it pairs well.
– user10671234
Nov 19 at 14:09
1
1
why not
[x for x in list1 if x in list2]
? What do you need the zip
for?– Ev. Kounis
Nov 19 at 14:11
why not
[x for x in list1 if x in list2]
? What do you need the zip
for?– Ev. Kounis
Nov 19 at 14:11
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
I hope I understand correctly that you want to get the pairs which match (for example based on the penultimate folder as in your example - you can customize on the condition you would like). What I would do then would be something like this:
dict1 = {path.split('\')[-2]: path for path in list1}
dict2 = {path.split('\')[-2]: path for path in list2}
common = set(dict1) & set(dict2)
pairs = [(dict1[element], dict2[element]) for element in common]
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I hope I understand correctly that you want to get the pairs which match (for example based on the penultimate folder as in your example - you can customize on the condition you would like). What I would do then would be something like this:
dict1 = {path.split('\')[-2]: path for path in list1}
dict2 = {path.split('\')[-2]: path for path in list2}
common = set(dict1) & set(dict2)
pairs = [(dict1[element], dict2[element]) for element in common]
add a comment |
up vote
0
down vote
I hope I understand correctly that you want to get the pairs which match (for example based on the penultimate folder as in your example - you can customize on the condition you would like). What I would do then would be something like this:
dict1 = {path.split('\')[-2]: path for path in list1}
dict2 = {path.split('\')[-2]: path for path in list2}
common = set(dict1) & set(dict2)
pairs = [(dict1[element], dict2[element]) for element in common]
add a comment |
up vote
0
down vote
up vote
0
down vote
I hope I understand correctly that you want to get the pairs which match (for example based on the penultimate folder as in your example - you can customize on the condition you would like). What I would do then would be something like this:
dict1 = {path.split('\')[-2]: path for path in list1}
dict2 = {path.split('\')[-2]: path for path in list2}
common = set(dict1) & set(dict2)
pairs = [(dict1[element], dict2[element]) for element in common]
I hope I understand correctly that you want to get the pairs which match (for example based on the penultimate folder as in your example - you can customize on the condition you would like). What I would do then would be something like this:
dict1 = {path.split('\')[-2]: path for path in list1}
dict2 = {path.split('\')[-2]: path for path in list2}
common = set(dict1) & set(dict2)
pairs = [(dict1[element], dict2[element]) for element in common]
answered Nov 20 at 22:55
zsomko
3616
3616
add a comment |
add a comment |
user10671234 is a new contributor. Be nice, and check out our Code of Conduct.
user10671234 is a new contributor. Be nice, and check out our Code of Conduct.
user10671234 is a new contributor. Be nice, and check out our Code of Conduct.
user10671234 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376311%2fensure-that-same-name-files-will-always-match%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
zip
preserves the ordering. It's not clear to me what you are asking.– roganjosh
Nov 19 at 14:03
in the second list the third item doesn't exist in the first. it will be paired wrong
– user10671234
Nov 19 at 14:06
And so will all subsequent items, then?
– roganjosh
Nov 19 at 14:07
Yes, this is why it needs to do some conditions to ensure that it pairs well.
– user10671234
Nov 19 at 14:09
1
why not
[x for x in list1 if x in list2]
? What do you need thezip
for?– Ev. Kounis
Nov 19 at 14:11