Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 访问列表的值,其中列表是字典的值_Python_List_Dictionary - Fatal编程技术网

Python 访问列表的值,其中列表是字典的值

Python 访问列表的值,其中列表是字典的值,python,list,dictionary,Python,List,Dictionary,我的标题不是很有描述性,但很难用一行字来解释。希望你能明白我下面的意思: 这是字典: d = {"A": [33, 333, 11, 111, 27, 272,], "B": [44, 444, 23, 233]} #every two elements in the list are considered as a pair which should be later 'retrieved' as pair. 我想处理字典中的每个键,查看列表(字典中该键的值)并进行一些测试,如果测试通过

我的标题不是很有描述性,但很难用一行字来解释。希望你能明白我下面的意思:

这是字典:

d = {"A": [33, 333, 11, 111, 27, 272,], "B": [44, 444, 23, 233]}   #every two elements in the list are considered as a pair which should be later 'retrieved' as pair.
我想处理字典中的每个键,查看列表(字典中该键的值)并进行一些测试,如果测试通过,那么我想恢复通过的元素及其对应的对。同样,下面是一个例子来解释我的意思(我很抱歉没有说得很清楚,请耐心听我说):

但是我没有正确地计算出来,我得到了taxid[I]的索引错误: 我实现的是阈值检查工作正常,但我认为我在索引方面出了问题,可能是我做I=+1的方式,而不是打印对的对应id,它不能正确地对应它们,并且会给我错误


请评论我需要进一步澄清的地方,非常感谢您的帮助。我已经试着解决它一段时间了。多谢各位

您正在使用
i
并在每次看到分数时增加它,因此它是您迄今为止查看的分数总数(不考虑键),而不是对应于分数的id的位置。您可以解决这个问题,但是@MorganThrapp改变数据结构的想法是一个好主意

使用字典或命名元组是一个更好的主意,因为这样您就不必记住元组中的每个元素对应于什么(即分数是第一位还是id?),但您可以使用
zip
来配对值:

>>> vals = d["A"]
>>> vals[::2]
[33, 11, 27]
>>> vals[1::2]
[333, 111, 272]
>>> list(zip(vals[::2], vals[1::2]))
[(33, 333), (11, 111), (27, 272)]
诸如此类

d = {"A": [33, 333, 11, 111, 27, 272,], "B": [44, 444, 23, 233]}
for key, value in sorted(d.items()):
    print(key)
    pairs = list(zip(value[::2], value[1::2]))
    threshold = 0.8 * max(score for score, id in pairs)
    for score, id in pairs:
        if score >= threshold:
            print(score, id)
给予


我们根本不需要使用索引。

变量
i
的初始化和增量在错误的位置完成

请参阅更正的版本:

for key, value in d.items():
    i = 0
    print key
    score_list = value[0::2]
    highest_score_in_list = score_list[0]
    threshold = 0.8 * float(highest_score_in_list)
    for index, items in enumerate(score_list):
        id = value[1::2]
        if float(items) <=float(threshold):
            pass
        else:
            print index, items, id[i]
        i += 1

您能更改输入dict的格式吗?如果是这样,您可能需要一个元组列表的dict。例如,
{'a':[(33333),(11111)]}
什么是
taxid
?在给定的代码中没有定义它。@Morgan Thrapp:我想我可以,我还不知道怎么做。这本词典是我自己编的,我觉得这样最好。我将研究如何制作元组列表词典。有好的链接吗?你可能需要使用@FgS2链接中的解决方案对这些列表进行分区,关于什么?元组?名单?字典?对于他们中的任何一个,我都会推荐.Works!!我错过了你救我的那件小事!谢谢大家!@FgS2很高兴能为您提供帮助。你可以通过投票和接受答案来回报你的支持!我只能在一个答案上打勾,atm没有足够的分数给你投票。当我这样做的时候,我会回来投票。第一个答案也是很有用的,所以这次我将使用它。再次感谢!是的,我同意,如果我要像摩根建议的那样改变数据结构,那将是最理想的。我也会努力的。
d = {"A": [33, 333, 11, 111, 27, 272,], "B": [44, 444, 23, 233]}
for key, value in sorted(d.items()):
    print(key)
    pairs = list(zip(value[::2], value[1::2]))
    threshold = 0.8 * max(score for score, id in pairs)
    for score, id in pairs:
        if score >= threshold:
            print(score, id)
A
33 333
27 272
B
44 444
for key, value in d.items():
    i = 0
    print key
    score_list = value[0::2]
    highest_score_in_list = score_list[0]
    threshold = 0.8 * float(highest_score_in_list)
    for index, items in enumerate(score_list):
        id = value[1::2]
        if float(items) <=float(threshold):
            pass
        else:
            print index, items, id[i]
        i += 1
A
0 33 333
2 27 272
B
0 44 444