Skip to content

Instantly share code, notes, and snippets.

View Linus-Albertus's full-sized avatar

Lino A. Urdaneta F. Linus-Albertus

View GitHub Profile
@Linus-Albertus
Linus-Albertus / reverse_phrase.py
Last active May 26, 2019 10:57
[reverse phrase] Take a phrase and reverse it
original_phrase = 'foo bar'
reversed_list = [word for word in reversed(original_phrase.split())]
reversed_phrase = ' '.join(reversed_list)
@Linus-Albertus
Linus-Albertus / pymongo_regex_consult.py
Last active May 26, 2019 10:56
[pmongo regex consult] Consult a mongodb database with pymongo #pymongo #mongodb
from pymongo import MongoClient
client = MongoClient()
db = client.your_mongo_db
responses = db.your_mongodb_collection.find( { 'field_name': {'$regex' : "regular_expresion", "$options" : "i"} } )
for response in responses:
pass
@Linus-Albertus
Linus-Albertus / swapping_values.py
Created May 12, 2019 20:58
[swapping] Swapping two values in Python #python #swapping #permut
b, a = a, b
@Linus-Albertus
Linus-Albertus / join_elements.py
Last active May 26, 2019 11:58
[join elements] Join elements from a list #python #join
" ".join(a_list)
@Linus-Albertus
Linus-Albertus / count_elements.py
Last active May 26, 2019 10:57
[most frequent elements] Take a list of elements and return their frequency #python #collections #freqs
from collections import Counter
count = Counter(a_list_of_repeated_elements)
print(count.most_common(3)) # Show the 3 more frequent elements of a list
@Linus-Albertus
Linus-Albertus / reverse_string.py
Last active May 26, 2019 10:58
[reverse string] Reverse phrase with reversed #reverse #string #python
original_phrase = 'foo bar'
reversed_phrase = ''
for character in reversed(original_phrase):
reversed_phrase += character
@Linus-Albertus
Linus-Albertus / reverse list.py
Last active May 26, 2019 10:59
[reverse list] Reverse a list #reversed #list #python
original_list = ['foo', 'bar']
reversed_list = list()
for l in reversed(original_list):
reversed_list.append(l)
@Linus-Albertus
Linus-Albertus / transpose_array_2d.py
Last active May 26, 2019 10:58
[transpose array] Transpose an 2d array #array #python
original_array = [['a',1], ['b',2], ['c',3]]
transposed_array = zip(*original_array)
list(transposed_array)
@Linus-Albertus
Linus-Albertus / copy_list.py
Created May 26, 2019 11:09
[copy a list] Copy a list, not just a reference to it #python #list
new_list = old_list.copy()
@Linus-Albertus
Linus-Albertus / copy_nested_list.py
Created May 26, 2019 11:16
[copy nested lists] Copy a list of lists #python #list
from copy import deepcopy
old_list = [[1,2], [3,4]]
new_list = deepcopy(old_list)