Tutorial :Python List Question



Question:

i have an issue i could use some help with, i have python list that looks like this:

fail = [  ['da39a3ee5e6b4b0d3255bfef95601890afd80709', 'ron\\b\\include', 'Test.java']  ['b5cc17d3a35877ca8b76f0b2e07497039c250696', 'ron\\c', 'apa1.txt']  ['95d1543adea47e88923c3d4ad56e9f65c2b40c76', 'ron\\c', 'knark.txt']  ['da39a3ee5e6b4b0d3255bfef95601890afd80709', 'ron\\d', 'Sourcecheck.py']  ['da39a3ee5e6b4b0d3255bfef95601890afd80709', 'ron\\a\\include', 'svin.txt']  ['b5cc17d3a35877ca8b76f0b2e07497039c250696', 'ron\\a', 'apa2.txt']  ['95d1543adea47e88923c3d4ad56e9f65c2b40c76', 'ron\\c', 'apa.txt']    sha1 value, directory, filename  

What i want is to separate this content in two different lists based on the sha1 value and directory. For example.

['95d1543adea47e88923c3d4ad56e9f65c2b40c76', 'ron\\c', 'apa.txt']  ['95d1543adea47e88923c3d4ad56e9f65c2b40c76', 'ron\\c', 'knark.txt']  

i want to add to the list duplicate = [], because it's in the same directory with the same sha1 value (and only that directory). The rest of the entries i want to add to another list, say diff = [], because the sha1 value is the same but the directories differ.

I'm kinda lost with the logic here so all that help i could get would be thankful!

EDIT: Fixed a typo, last value (filename) was in some cases a 1-list element, which was 100% incorrect, thansk to SilentGhost to become aware of this issue.


Solution:1

duplicate = []  # Sort the list so we can compare adjacent values  fail.sort()  #if you didn't want to modify the list in place you can use:  #sortedFail = sorted(fail)  #      and then use sortedFail in the rest of the code instead of fail  for i, x in enumerate(fail):      if i+1 == len(fail):          





        
Previous
Next Post »