Chapter 2 Introductory Examples

In [1]: path = 'usagov_bitly_data2012-03-16-1331923249.txt'
open(path).readline()
# file contains a web data known as JSON-JavaScript Object Notation
# output JSON string
out[1]: '{ "a": "Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/535.11 (KHTML, like Gecko) Chrome\/17.0.963.78 Safari\/535.11", "c": "US", "nk": 1, "tz": "America\/New_York", "gr": "MA", "g": "A6qOVH", "h": "wfLQtf", "l": "orofrog", "al": "en-US,en;q=0.8", "hh": "1.usa.gov", "r": "http:\/\/www.facebook.com\/l\/7AQEFzjSi\/1.usa.gov\/wfLQtf", "u": "http:\/\/www.ncbi.nlm.nih.gov\/pubmed\/22415991", "t": 1331923247, "hc": 1331822918, "cy": "Danvers", "ll": [ 42.576698, -70.954903 ] }\n'


In [2]: import json
        # use loads function in json module to convert a JSON string into a Python dictionary object
        path = 'usagov_bitly_data2012-03-16-1331923249.txt'
        records = [json.loads(line) for line in open(path)]
        # list comprehension, returns a list of dictionaries
        print(records[0])
        print(records[0]['tz'])
Out[2]:{'h': 'wfLQtf', 'gr': 'MA', 'u': 'http://www.ncbi.nlm.nih.gov/pubmed/22415991', 'l': 'orofrog', 'tz': 'America/New_York', 'c': 'US', 'al': 'en-US,en;q=0.8', 'hc': 1331822918, 'hh': '1.usa.gov', 'cy': 'Danvers', 'nk': 1, 'a': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 Safari/535.11', 'r': 'http://www.facebook.com/l/7AQEFzjSi/1.usa.gov/wfLQtf', 't': 1331923247, 'g': 'A6qOVH', 'll': [42.576698, -70.954903]}
        America/New_York

In [3]: time_zones = [tz['tz']for tz in records if 'tz' in tz]
        time_zones_withoutms = [tz['tz']for tz in records if 'tz' in tz and tz['tz'] is not '']
        # the time zone list without missing data
        print(time_zones[:11])
        print(time_zones_withoutms[:11])
Out[4]: ['America/New_York', 'America/Denver', 'America/New_York', 'America/Sao_Paulo', 'America/New_York', 'America/New_York', 'Europe/Warsaw', '', '', '', 'America/Los_Angeles']
        ['America/New_York', 'America/Denver', 'America/New_York', 'America/Sao_Paulo', 'America/New_York', 'America/New_York', 'Europe/Warsaw', 'America/Los_Angeles', 'America/New_York', 'America/New_York', 'America/New_York']

In [4]: from collections import defaultdict

        def counts(s):
        """produce how many times a certain kinda variable occures.
        s: a sequence that can be iterated
        returns: counting numbers
        """
            countings = defaultdict(int) 
            # the values are initialized to 0
            for x in s:
                countings[x] += 1
            return countings

        countings = counts(time_zones)
        print(countings['America/New_York'])
        print(len(time_zones))
Out[4]: 1251
        3440

In [5]: print(countings)
Out[5]: defaultdict(<class 'int'>, {'': 521, 'America/Halifax': 4, 'America/Recife': 2, 'Asia/Novosibirsk': 1, 'America/Montreal': 9, 'Europe/Sofia': 1, 'America/New_York': 1251, 'Europe/Riga': 2, 'America/Argentina/Buenos_Aires': 1, 'Europe/Moscow': 10, 'America/Guayaquil': 2, 'America/Winnipeg': 4, 'America/La_Paz': 1, 'Asia/Karachi': 3, 'America/Santo_Domingo': 1, 'Europe/Uzhgorod': 1, 'America/Sao_Paulo': 33, 'Europe/Madrid': 35, 'Africa/Johannesburg': 1, 'Asia/Pontianak': 1, 'Chile/Continental': 6, 'America/Argentina/Mendoza': 1, 'Europe/Prague': 10, 'America/Tegucigalpa': 1, 'America/Phoenix': 20, 'Asia/Dubai': 4, 'America/Rainy_River': 25, 'Asia/Bangkok': 6, 'Asia/Yekaterinburg': 1, 'America/Vancouver': 12, 'America/Montevideo': 1, 'America/Edmonton': 6, 'America/Argentina/Cordoba': 1, 'Australia/NSW': 6, 'Asia/Jakarta': 3, 'America/Costa_Rica': 1, 'America/Denver': 191, 'Europe/Berlin': 28, 'America/Lima': 1, 'Pacific/Honolulu': 36, 'Europe/Ljubljana': 1, 'Europe/Paris': 14, 'Europe/Athens': 6, 'Europe/Belgrade': 2, 'Pacific/Auckland': 11, 'Australia/Queensland': 1, 'Europe/Skopje': 1, 'Europe/Bratislava': 3, 'America/Anchorage': 5, 'Europe/Lisbon': 8, 'Europe/Amsterdam': 22, 'Europe/Vilnius': 2, 'Asia/Amman': 2, 'Europe/Bucharest': 4, 'America/Puerto_Rico': 10, 'Europe/Stockholm': 14, 'America/Managua': 3, 'America/Caracas': 1, 'Europe/Oslo': 10, 'Asia/Manila': 1, 'America/Chicago': 400, 'Europe/London': 74, 'Africa/Lusaka': 1, 'Europe/Brussels': 4, 'Europe/Volgograd': 1, 'Europe/Copenhagen': 5, 'Asia/Harbin': 3, 'America/Bogota': 3, 'Europe/Malta': 2, 'Europe/Rome': 27, 'Asia/Tokyo': 37, 'Europe/Vienna': 6, 'Africa/Ceuta': 2, 'Africa/Cairo': 3, 'Asia/Jerusalem': 3, 'Europe/Budapest': 5, 'America/Mexico_City': 15, 'Asia/Kuching': 1, 'Asia/Beirut': 4, 'Asia/Seoul': 5, 'Africa/Casablanca': 1, 'America/Monterrey': 1, 'Asia/Kuala_Lumpur': 3, 'Asia/Istanbul': 9, 'Asia/Calcutta': 9, 'America/Indianapolis': 20, 'America/St_Kitts': 1, 'America/Los_Angeles': 382, 'Asia/Riyadh': 1, 'Europe/Warsaw': 16, 'America/Chihuahua': 2, 'Europe/Dublin': 3, 'Europe/Helsinki': 10, 'Asia/Nicosia': 1, 'America/Mazatlan': 1, 'Asia/Hong_Kong': 10, 'Europe/Zurich': 4})

dictionary is an object without order, so if we wanna top 10 time zones and their countings we have to use another data structure


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容

  • 年龄从来不是衡量一个人是否成熟的标志,一个人是否成熟来源于他是否了解自己所作所为的目的,来源于他是否敢承担所做决定...
    未时l阅读 188评论 0 0
  • html 是描述网页的一种标签 例如 与 之间描述关于文档的信息。 yu 之间定于文档样式信息。 javascr...
    墨耕堂堂主阅读 211评论 0 0
  • 栖月阅读 129评论 0 0