Skip to content

Instantly share code, notes, and snippets.

View kerenskybr's full-sized avatar
🤖
___

Roger Monteiro kerenskybr

🤖
___
View GitHub Profile
@kerenskybr
kerenskybr / populate.py
Created October 6, 2022 13:49
Another way to populate a dict when using enumerate
# Another way to populate a dict when using enumerate
my_dict = {}
my_string = 'Something'
for i, my_dict[i] in enumerate(my_string):
pass
# Output:
# {0: 'S', 1: 'o', 2: 'm', 3: 'e', 4: 't', 5: 'h', 6: 'i', 7: 'n', 8: 'g'}
@kerenskybr
kerenskybr / group_substrings.py
Created April 20, 2021 17:13
Grouping similar substrings in list
from itertools import groupby
test_list = ['geek_1', 'coder_2', 'geek_4', 'coder_3', 'pro_3']
test_list.sort()
res = [list(i) for j, i in groupby(test_list,
lambda a: a.split('_')[0])]
#The original list is : [‘coder_2’, ‘coder_3’, ‘geek_1’, ‘geek_4’, ‘pro_3’]
test_list = [{'gfg' : [1, 5, 6, 7], 'good' : [9, 6, 2, 10], 'CS' : [4, 5, 6]},
{'gfg' : [5, 6, 7, 8], 'CS' : [5, 7, 10]},
{'gfg' : [7, 5], 'best' : [5, 7]}]
# Concatenate Similar Key values
# Using loop
res = dict()
for dict in test_list:
for list in dict:
if list in res:
@kerenskybr
kerenskybr / sub_key.py
Created April 20, 2021 17:09
Get first element of sublist as dictionary key
# Given a list [[1,2,3], [4,5,6], [7,8,9]]
{words[0]:words[1:] for words in lst}
#result
#{1: [2, 3], 4: [5, 6], 7: [8, 9]}
@kerenskybr
kerenskybr / flat.py
Created April 20, 2021 17:08
Flattening a list of lists
#Given a list of lists t
flat_list = [item for sublist in t for item in sublist]
flat_list = []
for sublist in t:
for item in sublist:
flat_list.append(item)
# OR
flatten = lambda t: [item for sublist in t for item in sublist]
@kerenskybr
kerenskybr / websocket_client.py
Last active April 5, 2021 13:26
Script to test websockets with/out luminati proxy
import websocket
try:
import thread
except ImportError:
import _thread as thread
import time
import argparse
import socks
parser = argparse.ArgumentParser()
@kerenskybr
kerenskybr / intersection.py
Last active November 22, 2022 19:53
Function to find intersection between two lines
def intersection(horizontal, vertical):
# https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
# Horizontal x y x y
# Vertical x y x y
# x1, y1, x2, y2, x3, y3, x4, y4
# 0 1 2 3 0 1 2 3
result = []
x1, x2, x3, x4, y1, y2, y3, y4 = int(horizontal[0]), int(horizontal[2]), int(vertical[0]), int(vertical[2]), int(horizontal[1]), int(horizontal[3]), int(vertical[1]), int(vertical[3])
@kerenskybr
kerenskybr / clean_blocks.py
Created December 8, 2020 14:36
Function to clean all data created in runtime to free memory (blender script)
def clean_blocks():
for block in bpy.data.meshes:
if block.users == 0:
bpy.data.meshes.remove(block)
for block in bpy.data.materials:
if block.users == 0:
bpy.data.materials.remove(block)
for block in bpy.data.textures:
@kerenskybr
kerenskybr / load_img_opencv.py
Created December 8, 2020 11:50
Load images from a directory using opencv
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename))
if img is not None:
images.append(img)
return images
@kerenskybr
kerenskybr / merge_list.py
Last active September 22, 2020 01:54
Merge together items from list of lists sequentially
def merge_list(a_list):
"""Merge together items from list of lists sequentially
"""
return [list(i) for i in list(zip(*a_list))]
"""
i. e.
[[1,2,3], [4,5,6]]
result:
[[1,4], [2,5], [3,6]]