coder13
coder13

Reputation: 45

Rearranging list in python

I need to define a function named "dutch_flag" which takes a list of colors (red, green and blue) and:

Returns color_list rearranged so that 'red' strings come first, 'green' second and 'blue' third.

>>> color_list = ['red', 'green', 'blue', 'red', 'red', 'blue', 'red', 'green']
>>> dutch_flag(['red', 'green', 'blue', 'red', 'red', 'blue', 'red', 'green'])
>>> color_list
['red', 'red', 'red', 'red', 'green', 'green', 'blue', 'blue']

So far I have come up with this:

color_list = ['red','red','red','red','green','green','red','blue','green','red','blue','red','blue','blue']

def dutch_flag (ls):

#create a list of colors according to their priority
colors = ['red', 'green', 'blue']

#create a list that keeps track of the number color counts while looping
colorCounts = []

#create a temporary list which will be used to re-create color_list
temp_ls = []

#loop over each color to process color_list
for color in colors:                    #for each colors
    colorCounts.append(ls.count(color))     #remember the color count

    #now add the color (color-count) times to the temp list

    #for each count, append color to the temp_list
    for count in range(ls.count(color)):
        temp_ls.append( color )

#re-define list
ls = temp_ls

#free memory
del temp_ls, colors, colorCounts

#return result
return ls

Problem is it does not "rearrange" the input list. I cannot find a way to rearrange in such manner.

Please suggest me a Simple solution (or an algorithm) so that an Absolute Beginner like me can understand.

Upvotes: 0

Views: 309

Answers (3)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250891

Simply use sorted with list.index as key:

>>> color_list = ['red','red','red','red','green','green','red','blue','green','red','blue','red','blue','blue']

>>> colors = ['red', 'green', 'blue']
>>> sorted(color_list, key= colors.index)
['red', 'red', 'red', 'red', 'red', 'red', 'red', 'green', 'green', 'green', 'blue', 'blue', 'blue', 'blue']

>>> colors = ['green', 'blue', 'red']
>>> sorted(color_list, key= colors.index)
['green', 'green', 'green', 'blue', 'blue', 'blue', 'blue', 'red', 'red', 'red', 'red', 'red', 'red', 'red']

Using a function:

def dutch_flag(seq, order=('red', 'green', 'blue')):
    return sorted(seq, key= order.index)

#Re-assign the returned value to a variable
new_list = dutch_flag(['red', 'green', 'blue', 'red', 'red', 'blue', 'red', 'green'])

Upvotes: 2

Christian Tapia
Christian Tapia

Reputation: 34146

Here is a simple solution: You can sort the list using sort() (which will sort in-place, in other words, it will rearrenge the list) and passing True to the parameter reverse. You will get the expected result because this will sort the strings alphabetically in the reverse order:

def dutch_flag(L):
    L.sort(reverse = True)

Demo:

>>> color_list = ['red', 'green', 'blue', 'red', 'red', 'blue', 'red', 'green']
>>> dutch_flag(color_list)
>>> color_list
['red', 'red', 'red', 'red', 'green', 'green', 'blue', 'blue']

Upvotes: 1

Kannan Mohan
Kannan Mohan

Reputation: 1840

A simple sort() function should work.

color_list = ['red', 'green', 'blue', 'red', 'red', 'blue', 'red', 'green']
color_list.sort(reverse=True)
print(color_list)
['red', 'red', 'red', 'red', 'green', 'green', 'blue', 'blue']

Upvotes: 0

Related Questions