PratapSingh
PratapSingh

Reputation: 257

Match and insert in a python sorted list

I have a sorted list and I want to insert a string if it matches the pattern in list.

Example :

Sorted List
['Amy Dave', 'Dee Waugh', 'Eva A', 'Gin', 'Joy Kola', 'Kay Min', 'Mae', 'Pam Deing']

Above list is in sorted order. I need to insert a name in sorted order and also if the name already exist then it should be inserted before the existing name.

Example 
Name  'Eva Henry'

As Eva is already in the list then after matching the pattern it should be inserted before "Eva A". If name does not match then it should be inserted in sorted order in the list. The output should be like :

 Sorted List
    ['Amy Dave', 'Dee Waugh', 'Eva Henry', 'Eva A', 'Gin', 'Joy Kola', 'Kay Min', 'Mae', 'Pam Deing']

Any help will be appreciated.

Thank you

Upvotes: 2

Views: 964

Answers (4)

PratapSingh
PratapSingh

Reputation: 257

Here is my solution I think its easy

:

#spliting against whitespace
first_name = name.split()

#Stroting the first name of the user
first_name = first_name[0]

#Matching the pattern 
match = re.compile(first_name,re.IGNORECASE)
ind = ''

for i in sort_names:
        if re.match(match, i):
                ind = sort_names.index(i)
                break
                #If name matches for the first time end the loop and do insert name in the sorted list

if ind != '':
        sort_names.insert(ind, val)
        print ""
        print sort_names
else:
        bisect.insort(sort_names, val)
        print sort_names

Upvotes: 0

user774340
user774340

Reputation:

OK I will get down-voted for this but I can't let this stand. This is a terrible design pattern and you should complain strongly if this is homework.

I would store the name as a tuple with a frequency ('Fred Bloggs', 2), or use a dict() or something: just anything but please not this. Google 'python dict ()'.

edit: Actually a dict() isn't ordered is it? Oh well, I fail at life. Shrug.

edit: Also I meant list of tuples.

Upvotes: 0

pepr
pepr

Reputation: 20762

In my opinion, there are no stupid questions. If the names are meant as full names and only the first names are the key for sorting, there always may be some funny idea and the need to solve the problem. You can use bisect this way:

>>> fullnames = ['Amy Dave', 'Dee Waugh', 'Eva A', 'Gin', 'Joy Kola', 'Kay Min', 'Mae', 'Pam Deing']
>>> names = [full.split()[0] for full in fullnames]
>>> names
['Amy', 'Dee', 'Eva', 'Gin', 'Joy', 'Kay', 'Mae', 'Pam']

So, we have parallel list of first names that will be used to find the position of another full name xx (the first name extracted to x the same way as in the previous case):

>>> xx = 'Eva Henry'
>>> x = xx.split()[0]
>>> x
'Eva'

Now, use bisect to find the wanted position in the first-name list:

>>> import bisect
>>> pos = bisect.bisect_left(names, x)

Then update both lists:

>>> fullnames.insert(pos, xx)
>>> names.insert(pos, x)

Here is the result:

>>> fullnames
['Amy Dave', 'Dee Waugh', 'Eva Henry', 'Eva A', 'Gin', 'Joy Kola', 'Kay Min', 'Mae', 'Pam Deing']
>>> names
['Amy', 'Dee', 'Eva', 'Eva', 'Gin', 'Joy', 'Kay', 'Mae', 'Pam']

Upvotes: 3

jgritty
jgritty

Reputation: 11915

Here's a complete answer that does what you want, however ridiculous. I didn't test any edge cases.

sorta_sorted_list = ['Amy Dave', 'Dee Waugh', 'Eva A', 'Gin', 'Joy Kola', 'Kay Min', 'Mae', 'Pam Deing']

print sorta_sorted_list

def insert_kinda_sorted(name, sorta_sorted_list):
    new_list = []
    fname = name.split()[0]
    inserted = False
    for index in range(len(sorta_sorted_list)):
        if not inserted:
            if sorta_sorted_list[index].split()[0] == fname:
                new_list.append(name)
                inserted = True
            if sorta_sorted_list[index] > name:
                new_list.append(name)
                inserted = True
        new_list.append(sorta_sorted_list[index])

    return new_list

sorta_sorted_list = insert_kinda_sorted('Eva Henry', sorta_sorted_list)
print sorta_sorted_list

sorta_sorted_list = insert_kinda_sorted('Joe Blow', sorta_sorted_list)
print sorta_sorted_list

output is:

['Amy Dave', 'Dee Waugh', 'Eva A', 'Gin', 'Joy Kola', 'Kay Min', 'Mae', 'Pam Deing']
['Amy Dave', 'Dee Waugh', 'Eva Henry', 'Eva A', 'Gin', 'Joy Kola', 'Kay Min', 'Mae', 'Pam Deing']
['Amy Dave', 'Dee Waugh', 'Eva Henry', 'Eva A', 'Gin', 'Joe Blow', 'Joy Kola', 'Kay Min', 'Mae', 'Pam Deing']

Upvotes: 0

Related Questions