Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Aarsh Talati aarshtalati

🎯
Focusing
View GitHub Profile
@aarshtalati
aarshtalati / compareDicts.py
Created Jun 8, 2016
Python Compare two dictionaries
View compareDicts.py
def compareDicts(d1, d2):
differences = []
# present in d1 but not in d2
for key, value in ({k: d1[k] for k in set(d1) - set(d2)}).iteritems():
temp = list[[key, value], None]
differences.append(temp)
# present in d2 but not in d1
diff2 = {k: d2[k] for k in set(d2) - set(d1)}
@aarshtalati
aarshtalati / StringHelpersAndExtensions.cs
Last active Jun 28, 2016
Some useful C# helper and extension methods, fresh from the grill
View StringHelpersAndExtensions.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
// The indentation settings in the Gist do not seem to be listening to me. size: 4, mode: tabs, (w)rap: Atlanta hip hop
@aarshtalati
aarshtalati / DictCustomSort.py
Last active Jul 2, 2016
Sort dictionary with values closest to given value
View DictCustomSort.py
>>> chooseFrom
{'1': 10.3438090737, '3': 7.73275047259, '2': 12.9046550095, '5': 10.3438090737, '4': 12.9046550095, '7': 7.88437303088, '6': 5.12169187146, '8': 0.0}
>>> gh
7.73275047259
# reference : http://stackoverflow.com/a/12141207/799593
>>> test = sorted(chooseFrom, key=lambda x:abs(chooseFrom[x]-gh))
>>> test
['3', '7', '1', '5', '6', '2', '4', '8']
@aarshtalati
aarshtalati / PythonNumericCustomSort.py
Created Jul 7, 2016
Python : sort int list based on another int list
View PythonNumericCustomSort.py
>>> a=[2, 3, 4, 5, 6, 7, 8]
>>> b=[4, 2, 3, 8, 1, 5, 7, 6]
>>> sorted(a, key = lambda x: b.index(x))
[4, 2, 3, 8, 5, 7, 6]
>>> a=[2, 3, 4, 5, 6, 7, 8, 9]
>>> sorted(a, key = lambda x: b.index(x))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
ValueError: 9 is not in list
@aarshtalati
aarshtalati / FindMostFrequents.py
Created Jul 7, 2016
Python: find most common element in corresponding two lists
View FindMostFrequents.py
import itertools
a = [7, 3]
b = [3, 1, 2]
c = [4, 3, 5]
def allEqual(t):
same = True
@aarshtalati
aarshtalati / PillowNumpyAddImages.py
Last active Jul 9, 2016
Python: PIL ( Pillow ) NumPy add images
View PillowNumpyAddImages.py
from itertools import izip
from PIL import Image
import numpy as np
def getImageDifference(x, y):
diff3 = 1.0
# http://rosettacode.org/wiki/Percentage_difference_between_images#Python
if x.size == y.size:
pairs = izip(x.getdata(), y.getdata())
View KendoUiCoreReferences.html
<link href="~/Content/kendo/kendo.common.min.css" rel="stylesheet" />
<link href="~/Content/kendo/kendo.default.min.css" rel="stylesheet" />
<script src="~/Scripts/KendoUI/jquery.min.js"></script>
<script src="~/Scripts/KendoUI/kendo.core.min.js"></script>
<script src="~/Scripts/KendoUI/kendo.ui.core.min.js"></script>
View SplitCsvString_Advanced
string str = "Tom Cruise, Scott, ,Bob | at";
IEnumerable<string> names = str
.Split(new char[]{',', '|'})
.Where(x=>x!=null && x.Trim().Length > 0)
.Select(x=>x.Trim());
@aarshtalati
aarshtalati / code.py
Created Oct 7, 2017
Reverse and splice python tuple
View code.py
""" About:
img is an OpenCV 2 object. img.shape prints a tuple as (height, width, channels) e.g. (512, 768, 3)
We need a tuple as (width, height) e.g. (768, 512)
"""
import cv2
image_path = './image.jpg'
img = cv2.imread(image_path, flags=cv2.CV_LOAD_IMAGE_COLOR)
print img.shape # (512, 768, 3)
print img.shape[::-1] # (3, 768, 512)
View np_count_elements.py
import numpy as np
x = np.arange(20).reshape(4,5)
y = x % 3 == 0
y = y.astype(int) # optional, if dealing with only 1s and 0s
# simple
np.count_nonzero(y == 1) # 7
np.count_nonzero(y == 0) # 13
(y == 1).sum() # 7
(y == 0).sum() # 13