简体   繁体   中英

Determining sets of substrings in a list of strings

Okay, please forgive me for my question, it's probably not asked in the best way possible!!

I have a set of 56 entries in a dictionary. The keys indicate the combinations of features that are represented by the strings in the dictionaries. I am subdividing the dictionary of strings into sets based on features and putting the values into lists. My code works up until this point.

I am now having difficulties working out how I compare each of the strings in the list to each other to find out what the maximum shared string is. So my lists look like:

[ngali, ngali, ngalina, ngalina, ngina, ngina, nginana]

So 'ng' is what I'm after in this case. So far I've only been able to find reference to finding string distances with things like Levinshtein, and fuzz.

Are there any 'neat' ways of coding this or am I destined to write nested loops nested in loops to work through all of the possible combinations?

I'm using Python 3.

from os.path import commonprefix
l = ['ngali','ngali','ngalina','ngalina','ngina','ngina','nginana']
commonprefix(l)

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