Rob Alsod
Rob Alsod

Reputation: 2945

Python 3.3.2 - Sorting a List, While Disregarding Vowels

I am creating a program that sorts a list (Just like list.sort()) but it won't count vowels. Here is what I mean:

I have a list of... ['acd', 'ibc', 'ebb', 'zzaeib'] or something similar. A normal sort program would give this as the result: ['acd', 'ebb', 'ibc', 'zzaeib']. My sort will need to disregard the vowels, sort it, and the put the vowels back in and return the resulting list. For example, it would see the list above as ['cd', 'bc', 'bb', 'zzb']. It would then sort it (['bb', 'bc', 'cd', 'zzb']) and put the vowels back in (['ebb', 'ibc', 'acd', 'zzaeib']).

Here are the differences:

Normal sort: ['acd', 'ebb', 'ibc', 'zzaeib']

Custom sort: ['ebb', 'ibc', 'acd', 'zzaeib']

I know I can use the key feature of sort (list.sort(key=whatever_key)), but I cannot see a way to do this. Thanks in advance.

Rob.

Upvotes: 0

Views: 322

Answers (1)

fjarri
fjarri

Reputation: 9726

I know I can use the key feature of sort

Yep, you were almost there.

import re
new_list = sorted(l, key=lambda s: re.sub('[aioue]', '', s))

Upvotes: 2

Related Questions