久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

python:遍歷具有列表值的字典

python: iterating through a dictionary with list values(python:遍歷具有列表值的字典)
本文介紹了python:遍歷具有列表值的字典的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

給定一個列表字典,例如

Given a dictionary of lists, such as

d = {'1':[11,12], '2':[21,21]}

哪個更pythonic或更可取:

Which is more pythonic or otherwise preferable:

for k in d:
    for x in d[k]:
        # whatever with k, x

for k, dk in d.iteritems():
    for x in dk:
        # whatever with k, x

或者還有什么需要考慮的?

or is there something else to consider?

編輯,如果列表可能有用(例如,標準字典不保留順序),這可能是合適的,盡管它要慢得多.

EDIT, in case a list might be useful (e.g., standard dicts don't preserve order), this might be appropriate, although it's much slower.

d2 = d.items()
for k in d2:
        for x in d2[1]:
            # whatever with k, x

推薦答案

這是一個速度測試,為什么不呢:

Here's a speed test, why not:

import random
numEntries = 1000000
d = dict(zip(range(numEntries), [random.sample(range(0, 100), 2) for x in range(numEntries)]))

def m1(d):
    for k in d:
        for x in d[k]:
            pass

def m2(d):
    for k, dk in d.iteritems():
        for x in dk:
            pass

import cProfile

cProfile.run('m1(d)')

print

cProfile.run('m2(d)')

# Ran 3 trials:
# m1: 0.205, 0.194, 0.193: average 0.197 s
# m2: 0.176, 0.166, 0.173: average 0.172 s

# Method 1 takes 15% more time than method 2

cProfile 示例輸出:

cProfile example output:

         3 function calls in 0.194 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.194    0.194 <string>:1(<module>)
        1    0.194    0.194    0.194    0.194 stackoverflow.py:7(m1)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}



         4 function calls in 0.179 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.179    0.179 <string>:1(<module>)
        1    0.179    0.179    0.179    0.179 stackoverflow.py:12(m2)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {method 'iteritems' of 'dict' objects}

這篇關(guān)于python:遍歷具有列表值的字典的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产成人+综合亚洲+天堂 | 四虎永久在线视频 | 色综合天天综合网天天狠天天 | 草草福利影院 | 亚洲在线免费观看 | 一二三区视频 | 一道本在线 | 久久噜噜 | 亚洲一级黄色片 | 国产日韩精品一区二区 | 久久91精品 | 欧美中文字幕 | 岛国av在线播放 | 高潮毛片无遮挡免费看 | 亚洲综合视频在线观看 | 热久久久久 | 深夜福利视频网站 | 亚洲成人av在线播放 | 久久久蜜桃 | 亚洲精品91 | 在线免费黄色网址 | 亚洲国产精品久久久久 | 中文字幕av网站 | 欧美自拍视频 | 男女啪啪网站 | 天天干夜夜欢 | 精品伊人久久 | 日韩精品视频在线免费观看 | 国产精品国产三级国产专区52 | 午夜影院福利 | 蜜桃综合网 | 亚洲视频在线免费观看 | 男女啪啪免费 | 五月婷婷影院 | 色播五月婷婷 | 欧美大片18| 成人在线国产 | 亚洲资源在线 | 欧美激情三区 | 国产精品久久一区二区三区 | 在线免费播放av |