Skip to content

Instantly share code, notes, and snippets.

@wwgist
wwgist / switch_like.py
Created March 7, 2013 14:07
PYTHON: switch analog...
keycode = 2
functions = {1: func1, 2: func2, 3: func3}
functions.get(keycode, default_func)()
@wwgist
wwgist / dict_transforming.py
Created March 7, 2013 13:49
PYTHON: dict comprehensions
# start_dict = {'Dick': '[email protected]', 'Jane': '[email protected]', 'Stou': '[email protected]'}
result_dict = dict( [name, '.com' in email] for name, email in start_dict.iteritems() )
# result_dict = {'Dick': True, 'Jane': True, 'Stou': False}
@wwgist
wwgist / list_unique.py
Created March 7, 2013 11:23
PYTHON: check list on unique items
start_list = [1,2,3,3,4,1]
set(start_list)
# возвращает set([1,2,3,4])
if len(start_list) == len(set(start_list)):
print 'List is unique!'
@wwgist
wwgist / list_zipping.py
Created March 7, 2013 11:18
PYTHON: zipping lists to one list of tuples
letters = ['a', 'b', 'c']
numbers = [1, 2, 3]
squares = [1, 4, 9]
zipped_list = zip(letters, numbers, squares)
# zipped_list = [('a', 1, 1), ('b', 2, 4), ('c', 3, 9)]
@wwgist
wwgist / list_all_items.py
Created March 7, 2013 11:15
PYTHON: check list items on the ALL condition
# start_list = [1,10,100,1000,10000]
if all(item < 10 for item in start_list):
print 'Success'
@wwgist
wwgist / list_any_item.py
Created March 7, 2013 11:12
PYTHON: check list items on the ANY condition
# start_list = [1,10,100,1000,10000]
if any(item < 10 for item in start_list):
print 'Success'
@wwgist
wwgist / list_index-items.py
Created March 7, 2013 11:05
PYTHON: from list > index+item pairs
# list = ['a', 'b', 'c', 'd', 'e']
for index, item in enumerate(list):
print index, item
# печатает "0 a 1 b 2 c 3 d 4 e"
@wwgist
wwgist / list_sum.py
Created March 7, 2013 11:00
PYTHON: list sum...
# start_list = [1,2,3,4,5]
result = reduce(lambda a,b: a+b, start_list)
# result = 120
@wwgist
wwgist / list_flattening.py
Created March 7, 2013 07:46
PYTHON: list flattening
flatten_list = [item for sublist in start_list for item in sublist]
@wwgist
wwgist / .gitconfig
Created February 20, 2013 17:00
GIT: .gitconfig
[user]
name = wowkalucky
email = [email protected]
[core]
whitespace = fix,-indent-with-non-tab,trailing-space,cr-at-eol
excludesfile = ~/.gitignore
editor = nano
[color]