简体   繁体   中英

Removing similar items from list based on 'version number' in Python

I've got a list, like this (but larger):

[item_101.1.txt, item_101.2.txt, item_134.1.txt, item_134.2.txt, item_134.3.txt, item_134.4.txt]

So, when there is an "item_101. 2 .txt", this here "item_101. 1 .txt" becomes redundant, and I want to remove it from the list. Similarly, "item_134. 4 .txt" should remain, but item_134. 3 .txt, item_134. 2 .txt, item_134. 1 .txt should be removed.

But I can't do this within a for loop, because that deals on a per item basis.

Any ideas? Any concepts I should be looking into?

Thanks guys!

Since this sounds like it might be homework, I'm just going to provide the structure of an algorithm:

  • Define a function that can parse the string, returning the root of the file name, and the version number. You should probably have it return the version number as an integer, instead of a string. Use would look something like this, assuming they'll always be .txt file extensions:

     > extract_version('item_101.2.txt') ('item_101', 2)
  • Use this function on all of your inputs, returning something like this:

     [('item_101', 1), ('item_101', 2), ('item_134', 1), ... ]
  • Loop through that list, keeping track of the highest version number for each in a dictionary:

     for fname, version in version_list: if fname not in highest_version: highest_version[fname] = version else: highest_version[fname] = max(highest_version[fname], version)
  • After running this loop, highest_version will contain the maximum version numbers for each file name. You can loop through the dictionary and rebuild the file names. Note that they may be in a different order than before, so you may need to sort them based on your criteria.

     for fname, version in highest_version.items(): highest_version_list.append(fname + '.' + str(version) + '.txt'

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