逐步浏览python

时间:2015-08-19 18:17:51

标签: python dictionary

我有这样的字典:

{'AAAGGG': ['AGAGAGAA', 'AGAGAGAG'], 'AAAGGC': ['AGAGAGAG']})

我想逐步浏览每个键,然后逐步查看其相应值的字符。所以像这样:

for key in myDict:
    for eachValue in key:
        for char in eachValue:
            do something

希望有一种简单的方法可以做到这一点。

3 个答案:

答案 0 :(得分:5)

Python Tutorial: Looping Techniques

获得密钥后,使用密钥查找值。不要遍历密钥。

for key in myDict:
    for eachValue in myDict[key]:
        for char in eachValue:
            do something

更有效率,遍历项目并避免额外查找:

for key, value in myDict.items():
    for eachValue in value:
        for char in eachValue:
            do something

答案 1 :(得分:1)

这应该这样做:

File files = new File(name1+"\"+name2);
if (!files.exists()) {
    if (files.mkdirs()) {
        System.out.println("sub directories created successfully");
    } else {
        System.out.println("failed to create sub directories");
    }
}

答案 2 :(得分:0)

您正在寻找chain.from_iterable

from itertools import chain
print list(chain.from_iterable({'long_key': ["abc", "cdef"], 'another_key':['ZYC', 'hgt']}.keys()))
#  ['a', 'n', 'o', 't', 'h', 'e', 'r', '_', 'k', 'e', 'y', 'l', 'o', 'n', 'g', '_', 'k', 'e', 'y']