I have a list say
word = ["engine","ant","aeiou"]
I need to sort the list according to the number of vowels in the list elements. And this sort must change the input as well.
I tried writing the code, by using zip() and a few ops i'm getting the required answer but the input still remains same.

count = 0
    vow_count = []
    for chars in words:
        for letter in chars:
            if letter in 'aeiou':
                count += 1
        vow_count.append(count)
        count = 0

Now i have the number of vowels for each element in a separate list called vow_count. How to sort the word list according to the vow_list so that i have an inplace sort??

Append the count + word to the new list.

words = ["engine","ant","aeiou"]
count = 0
vow_count = []
for chars in words:
    for letter in chars.lower():
        if letter in 'aeiou':
            count += 1
    vow_count.append([count, chars])
    count = 0
vow_count.sort()
print vow_count
print [word for count, word in vow_count]

Edited 2 Years Ago by woooee

Append the count + word to the new list.

words = ["engine","ant","aeiou"]
count = 0
vow_count = []
for chars in words:
for letter in chars.lower():
if letter in 'aeiou':
count += 1
vow_count.append([count, chars])
count = 0
vow_count.sort()
print vow_count
print [word for count, word in vow_count]

But the words is not changed to to the sorted list right? I'll give you the entire scene here, This is the test function

def test_sort_by_vowel_count():
    single_sort_by_vc_test(["engine", "ant", "aeiou"], ["aeiou", "engine", "ant"])
    single_sort_by_vc_test(["engine", "ant", "aeroplane", "key", "bcdgcdbcd"], ["aeroplane", "engine", "ant", "key", "bcdgcdbcd"])
    single_sort_by_vc_test([], [])
    single_sort_by_vc_test(None, None)

def single_sort_by_vc_test(input, result):
    sort_by_vowel_count(input)
    assert result == input          // Assertion error here!!!!!! 

def sort_by_vowel_count(words):
    count = 0
    vow_count = []
    for chars in words:
        for letter in chars.lower():
            if letter in 'aeiou':
                count += 1
        vow_count.append([count, chars])
        count = 0
    vow_count.sort(reverse = True)
    return [word for count, word in vow_count]

I've marked the point where i'm getting the AssertionError

Replace last line with

words[:] = [word for count, word in vow_count]

This will replace the contents of list 'words' by your result. Also the line count = 0 should be inside the for loop. Print the result at the end of your function to see what happens.

Edited 2 Years Ago by Gribouillis

Replace last line with

words[:] = [word for count, word in vow_count]

This will replace the contents of list 'words' by your result. Also the line count = 0 should be inside the for loop. Print the result at the end of your function to see what happens.

The result printed is correct but still the input list remains same :( and i get the assertion error again

You can use the Schwartzian transform algorithm ...

''' sort_Schwartzian.py
example of the Schwartzian transform algorithm  
'''

def get_vowel_count(word, count=0):
    for c in word:
        if c in 'aeiou':
            count += 1
    return count

mylist = ["engine", "ant", "aeiou"]

# use the Schwartzian transform algorithm
# temporarily put count into (count, word) tuples
# then sort the list of tuples
templist = [(get_vowel_count(word), word) for word in mylist]

print(templist)  # test

templist.sort()

print(templist)  # test

# remove temporary count item after sorting
newlist = [word for (count, word) in templist]

print(newlist)

''' result ...
[(3, 'engine'), (1, 'ant'), (5, 'aeiou')]
[(1, 'ant'), (3, 'engine'), (5, 'aeiou')]
['ant', 'engine', 'aeiou']
'''
This question has already been answered. Start a new discussion instead.