Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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_Dictionary - Fatal编程技术网

Python 排序时如何获取字典值的长度

Python 排序时如何获取字典值的长度,python,dictionary,Python,Dictionary,所以我在控制台上打印了这本词典 source-document01321.txt 1 {'startPos': '216', 'endPos': '225'} 2 {'startPos': '3678', 'endPos': '3687'} this is the length 2 source-document01323.txt 1 {'startPos': '2880', 'endPos': '2889'} 2 {'startPos': '6042', 'endPos': '6351'}

所以我在控制台上打印了这本词典

source-document01321.txt
1 {'startPos': '216', 'endPos': '225'}
2 {'startPos': '3678', 'endPos': '3687'}
this is the length  2
source-document01323.txt
1 {'startPos': '2880', 'endPos': '2889'}
2 {'startPos': '6042', 'endPos': '6351'}
3 {'startPos': '14232', 'endPos': '14241'}
4 {'startPos': '16956', 'endPos': '16965'}
5 {'startPos': '22626', 'endPos': '22635'}
6 {'startPos': '24708', 'endPos': '24717'}
7 {'startPos': '34824', 'endPos': '34833'}
8 {'startPos': '36444', 'endPos': '36453'}
9 {'startPos': '38064', 'endPos': '38073'}
this is the length  9
source-document01259.txt
1 {'startPos': '3528', 'endPos': '3537'}
2 {'startPos': '10428', 'endPos': '10437'}
3 {'startPos': '12426', 'endPos': '12435'}
4 {'startPos': '18450', 'endPos': '18459'}
5 {'startPos': '24864', 'endPos': '24873'}
6 {'startPos': '27036', 'endPos': '27213'}
7 {'startPos': '30588', 'endPos': '31167'}
8 {'startPos': '34824', 'endPos': '34833'}
9 {'startPos': '44466', 'endPos': '44475'}
10 {'startPos': '45492', 'endPos': '45501'}
11 {'startPos': '46644', 'endPos': '46653'}
12 {'startPos': '51732', 'endPos': '51741'}
使用此代码

for key, value in doc_dict.iteritems():
    print key
    for k, v in value.iteritems():
        print k,v
    print "this is the length ", len(value)
我想根据其中每个键的值的长度按相反顺序对其进行排序,只得到前5个键——我使用下面的代码完成了这项工作

li = []
for k in sorted(doc_dict, key=lambda k: len(doc_dict[k]), reverse=True)[:5]:
    li.append(k)
pprint(li)
但该列表仅包含密钥

输出:

['source-document01348.txt',
 'source-document01389.txt',
 'source-document01253.txt',
 'source-document01306.txt',
 'source-document01255.txt']
我还希望每个键旁边都有值的长度,这样我就可以计算出之后的百分比

i、 e

我需要做什么样的改变才能做到这一点

for k in sorted(doc_dict, key=lambda k: len(doc_dict[k]), reverse=True)[:5]:
    li.append((k,len(doc_dict[k])))
只需再次进行计算,添加一个元组
(key,len(value))

只需再次进行计算,添加一个元组
(key,len(value))
使用
排序(doc_dict.items(),…

for k, v in sorted(doc_dict.items(), key=lambda x: len(x[0]), reverse=True)[:5]:
    li.append([k, len(v)]) 
使用排序的(文档目录项(),…

for k, v in sorted(doc_dict.items(), key=lambda x: len(x[0]), reverse=True)[:5]:
    li.append([k, len(v)]) 

就快到了。更改
li。将(k)
附加到:


li.append([k,len(doc_dict[k]))
你就快到了。将
li.append(k)
更改为:


li.append([k,len(doc_dict[k]))

我很好奇我的代码是否有效。你能测试一下并给我反馈吗?thxHey@sparkandshine会让你知道一段时间Thaksu可能会忘记测试我的代码。我在等待你的回复。我很好奇我的代码是否有效。你能测试一下并给我反馈吗?thxHey@sparkandshine会让你知道Thaksu可能会伪造代码t测试我的代码。我正在等待你的回复。