TOOLSP
"
WELCOME !

Please ! Use [CODE] tags for your LINKS and CODE.
Favor usar balisas [CODE] para sus vínculos y código.
Merci d'utiliser les balises [CODE] pour vos liens et code.

[code]http://Thank.you[/code]
"
*** GITHUB ***
.
.
URL Resolver [GIT]
.
.
.
.
Jx Update [REDIRECT]
.
.
*** Associated ***
.
Pastebin Your list online
.
mediafire Upload Files
.
imgur Upload Pics
lyngsat TV logos collection
transparent .png
.
http://hola.org free? VPN
Hola.apk free? VPN App.
.
hidester- free Proxy
.
webgrabplus EPG - Eng
xmltv EPG - Fr
kazer EPG - Fr
.
.wordreference Traduction
.
mail.com (fast sign-in)
.
.
Latest topics
» Regex for my favori serie
Sun 24 Sep - 21:12 by beezlo

» help regex
Fri 22 Sep - 21:08 by mickydoo

» I need i little help please!
Wed 20 Sep - 18:24 by jujuuj

» some help with vaughn or ustream .TV
Wed 20 Sep - 0:56 by adrianhn

» Xbmc tool box / Xbmc gui (Kodi)
Mon 18 Sep - 18:16 by oxus

» how to play this in kodi
Mon 11 Sep - 8:25 by retrorat1

» Where to put your list
Sun 10 Sep - 14:29 by oxus

FRIENDS / PARTNERS

forum

créer un forum


Dealing with list. Sorting, Removing duplicates.

View previous topic View next topic Go down

Dealing with list. Sorting, Removing duplicates.

Post by twogun on Tue 18 Apr - 15:49

*Put lists in Alphanumerical order:

Let's assume we have a regex that returns a list of genres. The list is in random order, but we want them to be in Alphanumerical order.  All we have to do is use sorted() like this:

Code:
genre = ['Drama', 'Anime', 'Comedy', 'Action', 'Religious', 'Sci-Fi', 'Horror', 'Romance']
genre = sorted(genre)
# now  genre = ['Action', 'Anime', 'Comedy', 'Drama', 'Horror', 'Religious', 'Romance', 'Sci-Fi']


*Remove duplicates from list:

if the regex returns the list genre with duplicates we need to remove them so we don't list a genre more then once.

One simple way of doing this is to convert the list into a set.  Since a set can not have duplicates this will eliminate the duplicates.  Then we convert the set back to a list.  The good thing about this method is you do not have to import any libraries.  The downside of this method is that it destroys the original order. It works fine if you don't care about the order or wish to use sorted() for alphanumeric order.

Code:
genre = ['Drama', 'Anime', 'Comedy', 'Anime', 'Action', 'Religious', 'Drama','Comedy', 'Sci-Fi', 'Horror', 'Romance']

genre = list(set(genre))
# now  genre = ['Romance', 'Sci-Fi', 'Horror', 'Drama', 'Anime', 'Action', 'Comedy', 'Religious']
# no duplicates but in random order
# if we want alphnumerical order we need to use sorted()
genre = sorted(genre)
# now  genre = ['Action', 'Anime', 'Comedy', 'Drama', 'Horror', 'Religious', 'Romance', 'Sci-Fi']
# Extra: We can combine both commands in one one line and receive the same results
genre = sorted(list(set(genre)))

*Removing duplicates while keeping the original order:

If for some reason you need to remove duplicates but keep the original order you can do so like this:

Code:
# first import the library:
from collections import OrderedDict
# then use this command to remove duplicates
list(OrderedDict.fromkeys(genre))

*sorting tuples:

Lets assume our regex has returned a list of tuples.  We want to put them in order.  We can use sorted() just like we did with the list and it will sort the list by the first element (the titles) in the tuples.

example:

Code:
items = [('Zipper Flipper', 'Action', 'PG13'),('Shady Deals', 'Comedy', 'R'), ('Rupan III', 'Anime', 'PG13')]
items = sorted(items)
# now items = [('Rupan III', 'Anime', 'PG13'), ('Shady Deals', 'Comedy', 'R'), ('Zipper Flipper', 'Action', 'PG13')]

but what if we want to sort the list by the 2nd or 3rd element?  Here is an example:

Code:
#first import the library.  itemgetter allows you to choose an element of a tuples. Each element is numbered starting at zero.  So in this example itemgetter(0) is the 1st element (the titles) and itemgetter(1) is the 2nd element (genre), and itemgetter(2) i the 3rd element (rating).
from operator import itemgetter
items = sorted(items, key=itemgetter(0))  # ---->  Same as items = sorted(items) sorts by first element (titles)
items = sorted(items, key=itemgetter(1))  # ---->  sorts by 2nd elements (genre)
# now items = [('Zipper Flipper', 'Action', 'PG13'), ('Rupan III', 'Anime', 'PG13'), ('Shady Deals', 'Comedy', 'R')]
items = sorted(items, key=itemgetter(2))  # ---->  sorts by 3nd elements (rating)
# now items = [('Zipper Flipper', 'Action', 'PG13'), ('Rupan III', 'Anime', 'PG13'), ('Shady Deals', 'Comedy', 'R')]

Now to take this one step further.  What if we wanted to sort the above list alphanumerical by rating (3rd element), but we want all titles of the same rating in alphanumerical.  So all 'PG13' movies in order followed by all 'R' movies in order.  Here is how:  

Code:
from operator import itemgetter
items = sorted(items, key=itemgetter(2,0))  # Why 2,0?  Remember 2 is the 3rd element (rating) and 0 is the first (title)
# now items = [('Rupan III', 'Anime', 'PG13'), ('Zipper Flipper', 'Action', 'PG13'), ('Shady Deals', 'Comedy', 'R')]
avatar
twogun

Messages : 33
Date d'inscription : 2017-04-03

View user profile

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum