Monday, December 2, 2019

Miscellaneous

Index page for Python:
http://mylearningcafe.blogspot.com/2015/08/python-index-page.html

Some miscellaneous stuff:

Sorting


>>> list_of_items = [1,100,200,3,100,200,5]
>>> sorted_list_of_items = sorted(list_of_items)
>>> list_of_items
[1, 100, 200, 3, 100, 200, 5]
>>> sorted_list_of_items
[1, 3, 5, 100, 100, 200, 200]

Reverse Sorting

>>> sorted_list_of_items_backwards = sorted(list_of_items,key=abs,reverse=True)

>>> sorted_list_of_items_backwards
[200, 200, 100, 100, 5, 3, 1]

Get even # list

>>> list_of_items = [1,100,200,3,100,200,5]
>>> list_of_items
[1, 100, 200, 3, 100, 200, 5]

>>> even_number_list = [x for x in list_of_items if x % 2 == 0]

>>> even_number_list
[100, 200, 100, 200]

Randomly shuffle data

>>> import random
>>> list_of_items = [1,100,200,3,100,200,5]
>>> 
>>> list_of_items
[1, 100, 200, 3, 100, 200, 5]

>>> random.shuffle(list_of_items)

>>> list_of_items
[3, 1, 5, 100, 100, 200, 200]

No comments:

Post a Comment