- Goal
リスト型の要素の中の重複排除とNone(空要素)を排除する。
集合型(set)なら、重複排除は自動でできて、.discardでNoneは排除可能だが、リスト型で行いたい。理由は、リスト内の順番をキープしたいから。 - How
if で要素があるかどうか?と長さが0より大きいかを確認する。
※filterを使うとか、内包表記を使うとか色々載っていたが、これが一番分かりやすかった。。。This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters# -*- coding: utf-8 -*- import unicodedata inputtext = u'Aaa\nbBbb\n\nCCddff\n\nAaa\n\n' inputlist3 = [] # 全角を半角へ、小文字を大文字へ変換 itemcode = unicodedata.normalize('NFKC', inputtext).upper() # 改行区切りでリストに代入 inputlist = list(itemcode.replace(' ','\n').replace(' ','\n').split('\n')) # 改行区切りでリストに代入 でも、最後の改行は排除 inputlist2 = list(itemcode.replace(' ','\n').replace(',','\n').splitlines()) # 重複排除と長さの確認 for x in inputlist: if x not in inputlist3 and len(x) > 0 : inputlist3.append(x) print "=== inputtext" print inputtext print "=== after normalize" print itemcode print "=== normal list splitted by \\n" print inputlist print "=== normal list with splitlines" print inputlist2 print "=== eliminating duplication and None" print inputlist3 - Result
~$ python eliminating_duplication.py=== inputtextAaabBbbCCddffAaa=== after normalizeAAABBBBCCDDFFAAA=== normal list splitted by \n[u'AAA', u'BBBB', u'', u'CCDDFF', u'', u'AAA', u'', u'']=== normal list with splitlines[u'AAA', u'BBBB', u'', u'CCDDFF', u'', u'AAA', u'']=== eliminating duplication and None[u'AAA', u'BBBB', u'CCDDFF']
Oracle Application Express Notes | Apps development Notes | Google Cloud Platform | Python | apps test | Cool Beans | English | Books
2018/07/29
リストの要素の重複排除とNone(空要素)の排除 − Python
2018/07/01
GCP: gcloud コマンドで GAEのログを取る方法
備忘録
GAE アプリのログ解析をする場合、Stackdriver logging からログを取り出す必要あり。gcloud コマンドで簡単にできる。
GAE アプリのログ解析をする場合、Stackdriver logging からログを取り出す必要あり。gcloud コマンドで簡単にできる。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# log のリストを取得 | |
$ gcloud logging logs list | |
NAME | |
projects/<project名>/logs/appengine.googleapis.com%2Frequest_log | |
# GAEのlogを取得する; 取得して見やすくする | |
$ gcloud logging read 'resource.type=gae_app AND \ | |
logName=projects/<プロジェクト名>/logs/appengine.googleapis.com%2Frequest_log AND \ | |
severity>=INFO' --limit 1 --format json | python -m json.tool | |
[ | |
{ | |
"httpRequest": { | |
"status": 200 | |
# GAEのlogを取得する; フィルター付き | |
# 結果の加工まで含めた処理 | |
# timestamp の文字列を変数にするには、dt=`date --rfc-3339=s --date="1 days ago"|tr ' ' 'T'` | |
# で、変数にして timestamp>="'"${dt}"'" で指定する | |
$ gcloud logging read 'resource.type=gae_app AND \ | |
logName=projects/<project名>/logs/appengine.googleapis.com%2Frequest_log AND \ | |
timestamp>="2018-06-29T23:39:34+00:00" AND \ #logの期間を設定 | |
severity>=INFO AND \ | |
"品番が見つかりません"' \ #ログ内の日本語や文字列でフィルターしたければ、””で囲むだけ | |
--limit 10 \ # 検索結果の表示上限を指定 | |
--format json | \ | |
jq ".[].protoPayload.<parse tag>" | \ # 結果をjq に渡して、パース | |
sed -e '/<行頭文字>/d' | \ # 不要な行を削除 | |
awk '{print $4}' | \ # スペース区切りの 4列目を取得 | |
sed -e s/,//g > test.log # 不要文字(この場合はコンマ)を削除して、test.logへ出力 |
ラベル:
GCP,
Google App Engine,
Google Cloud Platform,
仕事
登録:
投稿 (Atom)