「python入門」sorted()、min()とmax()でリストを並べ替えるサンプル

1.sorted()
>>> l = [{‘x’:1, ‘y’:2}, {‘x’:2, ‘y’:3}, {‘x’:3, ‘y’:4}]
>>> l1 = sorted(l, key=lambda d:d[‘x’], reverse=True)
>>> l1
[{‘y’: 4, ‘x’: 3}, {‘y’: 3, ‘x’: 2}, {‘y’: 2, ‘x’: 1}]
>>>

2.itemgetter()
>>> from operator import itemgetter
>>> l = [{‘x’:1, ‘y’:2}, {‘x’:2, ‘y’:3}, {‘x’:3, ‘y’:4}]
>>> l2 = sorted(l, key=itemgetter(‘x’), reverse=True)
>>> l2
[{‘y’: 4, ‘x’: 3}, {‘y’: 3, ‘x’: 2}, {‘y’: 2, ‘x’: 1}]
>>>

3.min()とmax()
>>> min(l, key=itemgetter(‘x’))
{‘y’: 2, ‘x’: 1}
>>> max(l, key=itemgetter(‘x’, ‘y’))
{‘y’: 4, ‘x’: 3}
>>> max(l, key=itemgetter(‘x’))
{‘y’: 4, ‘x’: 3}
>>>

「Linux入門」プロセスを表示、強制終了するメモ

1.プロセスを表示
ps aux
ps aux | grep  nginx

2.プロセスを終了
kill pid
ps -ef | grep processname| grep -v grep | awk ‘{print $2}’ | xargs kill -9

「django入門」静的ファイル(css、js、images)を読み込むメモ

1.静的ファイルのフォルダ
STATICFILES_DIRS = (
(“images”,os.path.join(STATIC_URL, ‘images’).replace(‘\\’, ‘/’)),
(“css”, os.path.join(STATIC_URL, ‘css’).replace(‘\\’, ‘/’)),
(“js”, os.path.join(STATIC_URL, ‘js’).replace(‘\\’, ‘/’)),
)

2.ディフォルトの設定
STATICFILES_FINDERS = (
“django.contrib.staticfiles.finders.FileSystemFinder”,
“django.contrib.staticfiles.finders.AppDirectoriesFinder”

)

3.使用例
{% load static from staticfiles %}
{% static “css/sample.css” assample %}
<link href=”{{ sample }}” rel=”external nofollow” rel=”stylesheet”>

「JavaScript入門」requestFullScreen()でページのフルスクリーンを実行するサンプル

JSコード
function FullScreenEnterFunc() {
document.getElementById(‘cft’).style.height = screenHeight+”px”;
var d= document.getElementById(‘cft’);
if (d.requestFullscreen) {
d.requestFullScreen();
} else if (d.webkitRequestFullScreen) {
d.webkitRequestFullScreen();
} else if (d.mozRequestFullScreen) {
d.mozRequestFullScreen();
} else {
d.msRequestFullscreen();
}
}

「PostgreSQL入門」重複データと重複件数を取得する方法

1.列column_testが重複データ
SELECT
column_test
FROM
table_AA
GROUP BY
column_test
HAVING
COUNT(column_test) > 1

2.列column_testが重複データと重複件数
SELECT
COUNT(column_test) recordCnt
, column_test
FROM
table_AA
GROUP BY
column_test
HAVING
COUNT(column_test) > 1

「iOS入門」enum変数を出力するサンプル

サンプルコード
#import <Foundation/Foundation.h>

int main(int argc, charchar * argv[])
{
@autoreleasepool {
/* enum定義 */
enum day {Sunday = 6, Monday = 2, Tuesday, Wednesday, Thursday, Friday, Saturday};

/* enumのパラメータ */
enum day today, tomorrow;

today = Wednesday;
tomorrow = Thursday;

/* enumの値を出力 */
NSLog(@”today = %u”, today);
NSLog(@”tomorrow = %u”, tomorrow);

enum {alive, die} state;
state = alive;

NSLog(@”state = %d”, state);

}
}

jQueryMobileのページ間のパラメーターを渡すサンプル

サンプルコード:
$(document).bind(‘pagebeforechange’,function(e,data){
if (typeof data.toPage !== “string”){
return;
}
var paramUrl = $.mobile.path.parseUrl(data.toPage);
var params = getUrlParamPage(paramUrl.search);
});

function getUrlParamPage(string) {
var obj = {};
if (string.indexOf(“?”) != -1) {
var string = string.substr(string.indexOf(“?”) + 1);
var strs = string.split(“&”);
for(var i = 0; i < strs.length; i ++) {
var tempArr = strs[i].split(“=”);
obj[tempArr[0]] = unescape(tempArr[1]);
}
}
return obj;
}

「Java入門」Mapのリストをソートするサンプル

Javaコード:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListSortDemo {

public static void main(String[] args) {

List<Map<Object, Object>> lst = new ArrayList<Map<Object, Object>>();

Map<Object, Object> mapAA = new HashMap<Object, Object>();
Map<Object, Object> mapBB = new HashMap<Object, Object>();
Map<Object, Object> mapCC = new HashMap<Object, Object>(); ·

mapAA.put(“number”, “4”);
mapBB.put(“number”, “4.15”);
mapCC.put(“number”, “5.67”);
lst.add(mapAA);
lst.add(mapBB);
lst.add(mapCC);

Collections.sort(lst, new Comparator<Map<Object, Object>>() {

public int compare(Map<Object, Object> o1, Map<Object, Object> o2) {

int mapAAVal = (int)Double.parseDouble((String) o1.get(“number”))*100 ;
int mapBBVal = (int)Double.parseDouble((String) o2.get(“number”))*100;
return mapAAVal – mapBBVal;
}
});
System.out.println(lst);
}
}

「Java入門」配列の繰り返し処理サンプル

方法1
String[] empArr = { “山田”, “杉山”, “data003″ ,”data004”};
for(int i=0; i<empArr.length; i++) {
System.out.println(empArr[i]);
}
方法2
String[] empArr = { “山田”, “杉山”, “data003″ ,”data004”};
for(String cc : empArr) {
System.out.println(cc);
}

「php入門」array_multisortで多次元の配列をソートする

//配列
$students = array(
array(‘name’ => ‘山田’, ‘age’ => 30)
, array(‘name’ => ‘杉村’, ‘age’ =>26)
, array(‘name’ => ‘山城’, ‘age’ =>23)
, array(‘name’ => ‘上村’, ‘age’ =>22)
);

$ages = array();
foreach ($students as $user) {
$ages[] = $user[‘age’];
}
array_multisort($ages, SORT_ASC, $students);