Skip to content

Instantly share code, notes, and snippets.

View zhoudaxia233's full-sized avatar
🎯
Focusing

Zheng Zhou zhoudaxia233

🎯
Focusing
View GitHub Profile
@zhoudaxia233
zhoudaxia233 / gist:3df472bbd8519e9e821079497db72e0c
Last active May 27, 2018 08:34
Activate local jekyll server

bundle exec jekyll serve

@zhoudaxia233
zhoudaxia233 / 原地反转Python字符串???.md
Last active May 28, 2018 21:56
???Python:reverse a string in place???

字符串不存在原地反转这种操作,因为字符串是不可变的!!! list是可变的,所以list可以原地反转。

# a is a list
a[:] = a[::-1]
@zhoudaxia233
zhoudaxia233 / Remove reversed duplicates in list.md
Created May 29, 2018 20:30
Remove reversed duplicates in list
lst = [[0, 1], [0, 4], [1, 0], [4, 0]]
data = {tuple(item) for item in map(sorted, lst)}     # method 1
data = {tuple(sorted(item)) for item in lst}          # method 2
@zhoudaxia233
zhoudaxia233 / Python:Iterating over every two elements in a list.md
Created June 1, 2018 16:28
Python: Iterating over every two elements in a list

Type 1:

from itertools import tee
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)
@zhoudaxia233
zhoudaxia233 / pip.md
Last active June 11, 2018 19:49
Install pip packages on python3.6
sudo python3.6 -m pip install --upgrade pip
sudo python3.6 -m pip install [package_name]
@zhoudaxia233
zhoudaxia233 / install_python3.6_on_WSL.md
Created June 11, 2018 19:48
install python3.6 on WSL (Ubuntu on Windows)
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt update
sudo apt install python3.6
@zhoudaxia233
zhoudaxia233 / python3_tuple_unpacking.md
Last active June 12, 2018 13:10
Removal of Tuple Parameter Unpacking

Removal of Tuple Parameter Unpacking

zs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50)]
# t1 = map(lambda (x, y): x+y, zs)  # python2 only
t1 = map(lambda tup: tup[0]+tup[1], zs)  # python2 & python3 
t2 = list(t1)
print(t2)


# def fxn(a, b_c, d): # b_c is (b,c) 2to3 refactoring tool
@zhoudaxia233
zhoudaxia233 / flatten_a_list.md
Last active June 22, 2018 19:21
Flatten a list in python
flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x]

# Below is for you to understand 
# flatten = lambda x: [item for items in itemss for item in flatten(items)] if type(itemss) is list else [itemss]

>>> a = [1, 2, [3, 4], [[5, 6], [7, 8]]]
>>> flatten(a)
[1,2,3,4,5,6,7,8]
@zhoudaxia233
zhoudaxia233 / find_difference_between_two_lists_with_repetition.md
Last active June 26, 2018 12:28
Find the difference between two lists with repetition in Python3
from collections import Counter

l1 = "I have a dog and a cat sally."
l2 = "Why the fuck I have a dog and a cat cat."

# should output "5:why the fuck cat sally"

l1 = l1.split()
l2 = l2.split()