简体   繁体   中英

Comparing the elements of a list with themselves

I have lists of items:

['MRS_103_005_010_BG_001_v001',
'MRS_103_005_010_BG_001_v002',
'MRS_103_005_010_FG_001_v001',
'MRS_103_005_010_FG_001_v002',
'MRS_103_005_010_FG_001_v003',
'MRS_103_005_020_BG_001_v001',
'MRS_103_005_020_BG_001_v002',
'MRS_103_005_020_BG_001_v003']

I need to identify the latest version of each item and store it to a new list. Having trouble with my logic.

Based on how this has been built I believe I need to first compare the indices to each other. If I find a match I then check to see which number is greater.

I figured I first needed to do a check to see if the folder names matched between the current index and the next index. I did this by making two variables, 0 and 1, to represent the index so I could do a staggered incremental comparison of the list on itself. If the two indices matched I then needed to check the vXXX number on the end. whichever one was the highest would be appended to the new list.

I suspect that the problem lies in one copy of the list getting to an empty index before the other one does but I'm unsure of how to compensate for that.

Again, I am not a programmer by trade. Any help would be appreciated! Thank you.

# Preparing variables for filtering the folders
versions = foundVerList
verAmountTotal = len(foundVerList) 
verIndex = 0
verNextIndex = 1
highestVerCount = 1
filteredVersions = []


# Filtering, this will find the latest version of each folder and store to a list

while verIndex < verAmountTotal:
    try:
        nextVer = (versions[verIndex]) 
        nextVerCompare = (versions[verNextIndex])
    except IndexError:
        verNextIndex -= 1

    if nextVer[0:24] == nextVerCompare[0:24]:
        if nextVer[-3:] < nextVerCompare [-3:]:
            filteredVersions.append(nextVerCompare)
        else:
            filteredVersions.append(nextVer)  



    verIndex += 1
    verNextIndex += 1

My expected output is:

print filteredVersions
['MRS_103_005_010_BG_001_v002', 'MRS_103_005_010_FG_001_v003']
['MRS_103_005_020_BG_001_v003']

The actual output is:

print filteredVersions
['MRS_103_005_010_BG_001_v002', 'MRS_103_005_010_FG_001_v002', 
'MRS_103_005_010_FG_001_v003']

['MRS_103_005_020_BG_001_v002', 'MRS_103_005_020_BG_001_v003']

During the with loop I am using os.list on each folder referenced via verIndex. I believe the problem is that a list is being generated for every folder that is searched but I want all the searches to be combined in a single list which will THEN go through the groupby and sorted actions.

Seems like a case for itertools.groupby :

from itertools import groupby

grouped = groupby(data, key=lambda version: version.rsplit('_', 1)[0])
result = [sorted(group, reverse=True)[0] for key, group in grouped]

print(result)

Output:

['MRS_103_005_010_BG_001_v002',
 'MRS_103_005_010_FG_001_v003',
 'MRS_103_005_020_BG_001_v003']

This groups the entries by everything before the last underscore, which I understand to be the "item code".

Then, it sorts each group in reverse order. The elements of each group differ only by the version, so the entry with the highest version number will be first.

Lastly, it extracts the first entry from each group, and puts it back into a result list .

Try this:

text = """MRS_103_005_010_BG_001_v001
MRS_103_005_010_BG_001_v002
MRS_103_005_010_FG_001_v001
MRS_103_005_010_FG_001_v002
MRS_103_005_010_FG_001_v003
MRS_103_005_020_BG_001_v001
MRS_103_005_020_BG_001_v002
MRS_103_005_020_BG_001_v003
"""

result = {}

versions = text.splitlines()

for item in versions:
    v = item.split('_')
    num = int(v.pop()[1:])
    name = item[:-3]


    if result.get(name, 0) < num:
        result[name] = num 

filteredVersions = [k + str(v) for k, v in result.items()]

print(filteredVersions)

output:

['MRS_103_005_010_BG_001_v2', 'MRS_103_005_010_FG_001_v3', 'MRS_103_005_020_BG_001_v3']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM