Skip to content

Instantly share code, notes, and snippets.

View piyonishi's full-sized avatar
On target

Yusuke Nakanishi piyonishi

On target
View GitHub Profile
# 整数を割り算すると、デフォルトで小数点以下が切り捨てになる
12 / 26
#0
float(12) / 26
#0.46153846153846156
12 * 1.0 / 26
#0.46153846153846156
// 1つの場合
function preloader() {
heavyImage = new Image();
heavyImage.src = "sample.jpg";
}
// 複数ある場合
function preloader() {
// counter
var i = 0;
l = [1, 1, 2, 2, 3]
distinct_result = list()
map(lambda x: not x in distinct_result and distinct_result.append(x), l)
print(distinct_result)
#[1, 2, 3]
distinct_result = list(set(l))
print(distinct_result)
#[1, 2, 3]
import sys
argvs = sys.argv
print(argvs)
# ターミナルで以下のように打つと
#python comand-line.py あ a 1
# 以下の内容が返ってくる
#['comand-line.py', 'あ', 'a', '1']
Array.prototype.distinct = function() {
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
# Pythonっぽい書き方
a = '1,2,3,4,5,'
# その1
p1 = [item for item in a.split(',') if not item == '']
print(p1)
#['1', '2', '3', '4', '5']
# その2
import markdown
md = markdown.Markdown(extensions=['codehilite'])
sample = """
sample text
:::python
import sys
def sample():
print 'sample'
"""
@piyonishi
piyonishi / regexp.$n.js
Created June 27, 2013 09:30
知らんかったわ…/(^o^)\
var str = 'iPhone';
str.replace(/(iPhone)/g); // 括弧でくくった部分が格納される
console.log(RegExp.$1);
# インデックスつきループ
# listの場合enumerateを使う
sample_list = ['i', 'ro', 'ha', 'ni', 'ho', 'he', 'to']
for i, v in enumerate(sample_list):
print(i, v)
# dictionaryで3.xの場合itemsを使う
# iteritemsは廃止
sample_dictionary = {0:'i', 1:'ro', 2:'ha', 3:'ni', 4:'ho', 5:'he', 6:'to'}
# jinjaでenumerate()
<ul>
{%- for item in items %}
# loop.indexだと1から始まる
<li>{{ item }} - {{ loop.index0 }}</li>
{%- endfor %}
</ul>