Python Forum
Python find the minimum length of string to differentiate dictionary items
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python find the minimum length of string to differentiate dictionary items
#1
dTeamNames = {'A': 'Man City', 'B': 'Man United', 'C': 'West Brom', 'D': 'West Ham' }
Hello:
I have a dictionary contains a key and a team name as the example above.
Now, I want to find a way to create another dictionary which uses the same key as dTeams, but use only the shortest length of string of the original team names as the value.
For example, I want to have the following new dictionary using the above dTeamNames:
dShortNames = {'A': 'Man Ci', 'B': 'Man Un', 'C': 'West B', 'D': 'West H' }
The rule is: for the minimum length of string which can tell the difference for each team names.
For “Man City” and “Man United”, the minimum length is 5, so I can use “Man C” and “Man U” to differentiate the both team names; however, for “West Brom” and “West Ham”, the minimum length is 6, so I can use “West B” and “West H” to differentiate the both team names.
I want to write a function to find how to find the minimum length of string to differentiate all the team names in the dictionary and create the new dictionary.
Thanks for advice.
John
Reply
#2
If you sort the names, it suffices to always differentiate two consecutive names.
Reply
#3
Hello:
I don't quite understand your meaning.
Please show me your code if you know how to do this.
As I am rather new for python programming, I can't figure this out by myself now.
Thanks,
Reply
#4
Here is a solution with module itertools
import itertools as it

def index_two(a, b):
    '''return the first index where the two words differ
    If they don't differ, return the length of the smallest'''
    return next(
        it.dropwhile(lambda t: t[0]==t[1], zip(a, b, it.count(0))),
        (None, None, min(len(a), len(b))))[2]

def index_many(words):
    '''return the first index where all the words differ
    identical words are considered to differ on the next character'''
    seq = sorted(words)
    if not seq: return 0
    a, b = it.tee(seq)
    next(b, None)
    return max((index_two(*t) for t in zip(a, b)), default=0)


def shorten_names(dic):
    n = index_many(dic.values())
    return {k: v[:1+n] for k, v in dic.items()}
    
dTeamNames = {'A': 'Man City', 'B': 'Man United', 'C': 'West Brom', 'D': 'West Ham'}
dShortNames = shorten_names(dTeamNames)
print(dShortNames)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is dictionary length not 20? Mark17 2 208 Oct-07-2021, 05:56 PM
Last Post: Mark17
  Find string between two substrings, in a stream of data xbit 1 657 May-09-2021, 03:32 PM
Last Post: bowlofred
  Parse String between 2 Delimiters and add as single list items lastyle 5 918 Apr-11-2021, 11:03 PM
Last Post: lastyle
  Beautify dictionary without converting to string. sharoon 6 969 Apr-11-2021, 08:32 AM
Last Post: buran
  Regular expression: cannot find 1st number in a string Pavel_47 2 796 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  How to get indices of minimum time difference Mekala 1 706 Nov-10-2020, 11:09 PM
Last Post: deanhystad
  How to get index of minimum element between 3 & 8 in list Mekala 2 922 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  concatenating 2 items at a time in a python list K11 3 803 Oct-21-2020, 09:34 AM
Last Post: buran
  extract a dictionary from a string berc 4 1,109 Jul-30-2020, 06:58 AM
Last Post: berc
  find a string in a field in MongoDB Leon79 2 862 Jul-19-2020, 09:20 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020