Zip items from two lists that have the same file name?












2















I have two list: this:



list1(has way more items)



['C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp']


and this:



list2(has way more items)



['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']


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


wanted final output :



[('C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\AST\AST.shp'),etc..]









share|improve this question























  • Do the two lists have the same length?

    – Martin Thoma
    Nov 20 '18 at 7:51











  • No they don't. it needs also condition for that.

    – user10671234
    Nov 20 '18 at 7:57
















2















I have two list: this:



list1(has way more items)



['C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp']


and this:



list2(has way more items)



['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']


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


wanted final output :



[('C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\AST\AST.shp'),etc..]









share|improve this question























  • Do the two lists have the same length?

    – Martin Thoma
    Nov 20 '18 at 7:51











  • No they don't. it needs also condition for that.

    – user10671234
    Nov 20 '18 at 7:57














2












2








2








I have two list: this:



list1(has way more items)



['C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp']


and this:



list2(has way more items)



['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']


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


wanted final output :



[('C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\AST\AST.shp'),etc..]









share|improve this question














I have two list: this:



list1(has way more items)



['C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp']


and this:



list2(has way more items)



['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']


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


wanted final output :



[('C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\AST\AST.shp'),etc..]






python pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 7:36









user10671234user10671234

376




376













  • Do the two lists have the same length?

    – Martin Thoma
    Nov 20 '18 at 7:51











  • No they don't. it needs also condition for that.

    – user10671234
    Nov 20 '18 at 7:57



















  • Do the two lists have the same length?

    – Martin Thoma
    Nov 20 '18 at 7:51











  • No they don't. it needs also condition for that.

    – user10671234
    Nov 20 '18 at 7:57

















Do the two lists have the same length?

– Martin Thoma
Nov 20 '18 at 7:51





Do the two lists have the same length?

– Martin Thoma
Nov 20 '18 at 7:51













No they don't. it needs also condition for that.

– user10671234
Nov 20 '18 at 7:57





No they don't. it needs also condition for that.

– user10671234
Nov 20 '18 at 7:57












1 Answer
1






active

oldest

votes


















2














I would just group the files with a collections.defaultdict(), then output the pairs of length 2 in a separate list.



Demo:



from os.path import basename
from collections import defaultdict
from pprint import pprint

f1 = [
"C:\Users\user\Desktop\prog1\merge\AST\AST.shp",
"C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp",
"C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp",
]

f2 = [
"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",
]

files = defaultdict(list)
for path in f1 + f2:
filename = path.split('\')[-1]
files[filename].append(path)

pairs = [tuple(v) for k, v in files.items() if len(v) == 2]
pprint(pairs)


Output:



[('C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\AST\AST.shp'),
('C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp'),
('C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp',
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp')]


Note: Using os.path.basename() to extract the filename from Windows paths will only work on Windows. It will simply do nothing on Unix enviorments.






share|improve this answer


























  • your code returns .

    – Ev. Kounis
    Nov 20 '18 at 7:53











  • in files = defaultdict(list). What is the list variable?

    – user10671234
    Nov 20 '18 at 7:54






  • 1





    @RoadRunner That is what I get. The online compiler might be running a different OS though. Hmm

    – Ev. Kounis
    Nov 20 '18 at 7:56








  • 1





    I think it is great.

    – user10671234
    Nov 20 '18 at 8:25






  • 1





    @RoadRunner On retrospect, it does make sense. +1

    – Ev. Kounis
    Nov 20 '18 at 8:45











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388243%2fzip-items-from-two-lists-that-have-the-same-file-name%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I would just group the files with a collections.defaultdict(), then output the pairs of length 2 in a separate list.



Demo:



from os.path import basename
from collections import defaultdict
from pprint import pprint

f1 = [
"C:\Users\user\Desktop\prog1\merge\AST\AST.shp",
"C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp",
"C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp",
]

f2 = [
"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",
]

files = defaultdict(list)
for path in f1 + f2:
filename = path.split('\')[-1]
files[filename].append(path)

pairs = [tuple(v) for k, v in files.items() if len(v) == 2]
pprint(pairs)


Output:



[('C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\AST\AST.shp'),
('C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp'),
('C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp',
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp')]


Note: Using os.path.basename() to extract the filename from Windows paths will only work on Windows. It will simply do nothing on Unix enviorments.






share|improve this answer


























  • your code returns .

    – Ev. Kounis
    Nov 20 '18 at 7:53











  • in files = defaultdict(list). What is the list variable?

    – user10671234
    Nov 20 '18 at 7:54






  • 1





    @RoadRunner That is what I get. The online compiler might be running a different OS though. Hmm

    – Ev. Kounis
    Nov 20 '18 at 7:56








  • 1





    I think it is great.

    – user10671234
    Nov 20 '18 at 8:25






  • 1





    @RoadRunner On retrospect, it does make sense. +1

    – Ev. Kounis
    Nov 20 '18 at 8:45
















2














I would just group the files with a collections.defaultdict(), then output the pairs of length 2 in a separate list.



Demo:



from os.path import basename
from collections import defaultdict
from pprint import pprint

f1 = [
"C:\Users\user\Desktop\prog1\merge\AST\AST.shp",
"C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp",
"C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp",
]

f2 = [
"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",
]

files = defaultdict(list)
for path in f1 + f2:
filename = path.split('\')[-1]
files[filename].append(path)

pairs = [tuple(v) for k, v in files.items() if len(v) == 2]
pprint(pairs)


Output:



[('C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\AST\AST.shp'),
('C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp'),
('C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp',
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp')]


Note: Using os.path.basename() to extract the filename from Windows paths will only work on Windows. It will simply do nothing on Unix enviorments.






share|improve this answer


























  • your code returns .

    – Ev. Kounis
    Nov 20 '18 at 7:53











  • in files = defaultdict(list). What is the list variable?

    – user10671234
    Nov 20 '18 at 7:54






  • 1





    @RoadRunner That is what I get. The online compiler might be running a different OS though. Hmm

    – Ev. Kounis
    Nov 20 '18 at 7:56








  • 1





    I think it is great.

    – user10671234
    Nov 20 '18 at 8:25






  • 1





    @RoadRunner On retrospect, it does make sense. +1

    – Ev. Kounis
    Nov 20 '18 at 8:45














2












2








2







I would just group the files with a collections.defaultdict(), then output the pairs of length 2 in a separate list.



Demo:



from os.path import basename
from collections import defaultdict
from pprint import pprint

f1 = [
"C:\Users\user\Desktop\prog1\merge\AST\AST.shp",
"C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp",
"C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp",
]

f2 = [
"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",
]

files = defaultdict(list)
for path in f1 + f2:
filename = path.split('\')[-1]
files[filename].append(path)

pairs = [tuple(v) for k, v in files.items() if len(v) == 2]
pprint(pairs)


Output:



[('C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\AST\AST.shp'),
('C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp'),
('C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp',
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp')]


Note: Using os.path.basename() to extract the filename from Windows paths will only work on Windows. It will simply do nothing on Unix enviorments.






share|improve this answer















I would just group the files with a collections.defaultdict(), then output the pairs of length 2 in a separate list.



Demo:



from os.path import basename
from collections import defaultdict
from pprint import pprint

f1 = [
"C:\Users\user\Desktop\prog1\merge\AST\AST.shp",
"C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp",
"C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp",
]

f2 = [
"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",
]

files = defaultdict(list)
for path in f1 + f2:
filename = path.split('\')[-1]
files[filename].append(path)

pairs = [tuple(v) for k, v in files.items() if len(v) == 2]
pprint(pairs)


Output:



[('C:\Users\user\Desktop\prog1\merge\AST\AST.shp',
'C:\Users\user\Desktop\programs\merge\AST\AST.shp'),
('C:\Users\user\Desktop\prog1\merge\ASTI\ASTI.shp',
'C:\Users\user\Desktop\programs\merge\ASTI\ASTI.shp'),
('C:\Users\user\Desktop\prog1\merge\ASTO\ASTO.shp',
'C:\Users\user\Desktop\programs\merge\ASTO\ASTO.shp')]


Note: Using os.path.basename() to extract the filename from Windows paths will only work on Windows. It will simply do nothing on Unix enviorments.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 8:21

























answered Nov 20 '18 at 7:49









RoadRunnerRoadRunner

11.2k31340




11.2k31340













  • your code returns .

    – Ev. Kounis
    Nov 20 '18 at 7:53











  • in files = defaultdict(list). What is the list variable?

    – user10671234
    Nov 20 '18 at 7:54






  • 1





    @RoadRunner That is what I get. The online compiler might be running a different OS though. Hmm

    – Ev. Kounis
    Nov 20 '18 at 7:56








  • 1





    I think it is great.

    – user10671234
    Nov 20 '18 at 8:25






  • 1





    @RoadRunner On retrospect, it does make sense. +1

    – Ev. Kounis
    Nov 20 '18 at 8:45



















  • your code returns .

    – Ev. Kounis
    Nov 20 '18 at 7:53











  • in files = defaultdict(list). What is the list variable?

    – user10671234
    Nov 20 '18 at 7:54






  • 1





    @RoadRunner That is what I get. The online compiler might be running a different OS though. Hmm

    – Ev. Kounis
    Nov 20 '18 at 7:56








  • 1





    I think it is great.

    – user10671234
    Nov 20 '18 at 8:25






  • 1





    @RoadRunner On retrospect, it does make sense. +1

    – Ev. Kounis
    Nov 20 '18 at 8:45

















your code returns .

– Ev. Kounis
Nov 20 '18 at 7:53





your code returns .

– Ev. Kounis
Nov 20 '18 at 7:53













in files = defaultdict(list). What is the list variable?

– user10671234
Nov 20 '18 at 7:54





in files = defaultdict(list). What is the list variable?

– user10671234
Nov 20 '18 at 7:54




1




1





@RoadRunner That is what I get. The online compiler might be running a different OS though. Hmm

– Ev. Kounis
Nov 20 '18 at 7:56







@RoadRunner That is what I get. The online compiler might be running a different OS though. Hmm

– Ev. Kounis
Nov 20 '18 at 7:56






1




1





I think it is great.

– user10671234
Nov 20 '18 at 8:25





I think it is great.

– user10671234
Nov 20 '18 at 8:25




1




1





@RoadRunner On retrospect, it does make sense. +1

– Ev. Kounis
Nov 20 '18 at 8:45





@RoadRunner On retrospect, it does make sense. +1

– Ev. Kounis
Nov 20 '18 at 8:45


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388243%2fzip-items-from-two-lists-that-have-the-same-file-name%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$