久久久久久久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}

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

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
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(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产免费又黄又爽又刺激蜜月al | 精国产品一区二区三区 | 精品成人一区二区 | 中文字字幕一区二区三区四区五区 | 精品一区二区三区四区在线 | 黄色一级毛片 | 成人一级视频在线观看 | 久久成人免费视频 | 成人国产精品久久久 | 国产情侣在线看 | 免费一级黄色录像 | 日韩一区二区三区在线观看 | 中文字幕亚洲免费 | 国产免费一区二区三区 | 日本成人综合 | 成人a在线观看 | 91精品国产91久久久久青草 | 91精品久久久久久久久 | 91国在线观看| 99久久精品一区二区毛片吞精 | 麻豆av电影网| 天天爱爱网| 久久久久久网站 | 精品一二区 | 国产一区二区三区在线 | 一级片网址 | 欧美日韩精品一区二区三区四区 | 浮生影院免费观看中文版 | 亚洲国产精品久久久久 | 婷婷91 | 国产成人精品一区二 | av在线免费网站 | 日本在线中文 | 九九久久精品 | 精品欧美一区二区三区久久久 | 亚洲性网| 久免费视频| 美女黄频 | 久久久久亚洲精品 | 国产精品久久久久久影视 | 亚洲欧美日韩中文在线 |