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

Python 从子列表中提取具有最大项的元组(在目录中查找最新报告)

Python 从子列表中提取具有最大项的元组(在目录中查找最新报告),python,tuples,Python,Tuples,我有一个脚本,它创建了一系列子目录中所有PDF的目录列表。输出是元组,其中包括保存为字符串的文件年份以及生成报告的单位的id,该报告如下所示: unit1, 2010 unit2, 2002 unit2, 2005 unit2, 2010 unit3, 2003 unit1, '2010' unit2, '2010' unit3, '2003' [[(unit1, '2010')],[(unit12, '2010'), (unit2, '2010'), (unit2, '2005'),

我有一个脚本,它创建了一系列子目录中所有PDF的目录列表。输出是元组,其中包括保存为字符串的文件年份以及生成报告的单位的id,该报告如下所示:

unit1, 2010
unit2, 2002
unit2, 2005
unit2, 2010 
unit3, 2003 
unit1, '2010'
unit2, '2010'
unit3, '2003'
[[(unit1, '2010')],[(unit12, '2010'), (unit2, '2010'), (unit2, '2005'), (unit2, '2002')],[(unit3, '2003']]
import glob, os, itertools, operator  
dirtup = []
for f in glob.glob('P:\Office*\Technical*\Bureau*\T*\*\YR2*\R*\*\*.pdf'):
    fpath, fname = os.path.split(f)
    fyr = fpath[91:95]
    vcs = 'Volume'
    rname, extname = os.path.splitext(fname)
    rcid = fname[0:7]
    dirtup.append ((f, fyr, rcid, vcs))

dirtup2 = sorted(dirtup, key=operator.itemgetter(2))

for key, group in itertools.groupby(dirtup2, operator.itemgetter(2)):
    maxval = max(x[1] for x in dirtup2)

print [x for x in dirtup2 if x[1] == maxval] 
我现在要做的是创建一个报告,根据第二项中包含最大值的元组查找最近的报告。通常,我会在Access中使用MAX查询来完成这项工作,但是,我正在尝试省略这一步骤,并一次性编写摘录。使用我的原始代码,我的输出将包括以下内容:

unit1, 2010
unit2, 2002
unit2, 2005
unit2, 2010 
unit3, 2003 
unit1, '2010'
unit2, '2010'
unit3, '2003'
[[(unit1, '2010')],[(unit12, '2010'), (unit2, '2010'), (unit2, '2005'), (unit2, '2002')],[(unit3, '2003']]
import glob, os, itertools, operator  
dirtup = []
for f in glob.glob('P:\Office*\Technical*\Bureau*\T*\*\YR2*\R*\*\*.pdf'):
    fpath, fname = os.path.split(f)
    fyr = fpath[91:95]
    vcs = 'Volume'
    rname, extname = os.path.splitext(fname)
    rcid = fname[0:7]
    dirtup.append ((f, fyr, rcid, vcs))

dirtup2 = sorted(dirtup, key=operator.itemgetter(2))

for key, group in itertools.groupby(dirtup2, operator.itemgetter(2)):
    maxval = max(x[1] for x in dirtup2)

print [x for x in dirtup2 if x[1] == maxval] 
我环顾四周,意识到我需要更改脚本,以便它生成匹配每个唯一id的元组列表。使用从中找到的伟大答案,我能够将结果拆分为一组子列表。这意味着我的输出现在如下所示:

unit1, 2010
unit2, 2002
unit2, 2005
unit2, 2010 
unit3, 2003 
unit1, '2010'
unit2, '2010'
unit3, '2003'
[[(unit1, '2010')],[(unit12, '2010'), (unit2, '2010'), (unit2, '2005'), (unit2, '2002')],[(unit3, '2003']]
import glob, os, itertools, operator  
dirtup = []
for f in glob.glob('P:\Office*\Technical*\Bureau*\T*\*\YR2*\R*\*\*.pdf'):
    fpath, fname = os.path.split(f)
    fyr = fpath[91:95]
    vcs = 'Volume'
    rname, extname = os.path.splitext(fname)
    rcid = fname[0:7]
    dirtup.append ((f, fyr, rcid, vcs))

dirtup2 = sorted(dirtup, key=operator.itemgetter(2))

for key, group in itertools.groupby(dirtup2, operator.itemgetter(2)):
    maxval = max(x[1] for x in dirtup2)

print [x for x in dirtup2 if x[1] == maxval] 
我现在的困难是试图从每个包含最高值项的子列表中提取元组。我尝试了以下方法:

unit1, 2010
unit2, 2002
unit2, 2005
unit2, 2010 
unit3, 2003 
unit1, '2010'
unit2, '2010'
unit3, '2003'
[[(unit1, '2010')],[(unit12, '2010'), (unit2, '2010'), (unit2, '2005'), (unit2, '2002')],[(unit3, '2003']]
import glob, os, itertools, operator  
dirtup = []
for f in glob.glob('P:\Office*\Technical*\Bureau*\T*\*\YR2*\R*\*\*.pdf'):
    fpath, fname = os.path.split(f)
    fyr = fpath[91:95]
    vcs = 'Volume'
    rname, extname = os.path.splitext(fname)
    rcid = fname[0:7]
    dirtup.append ((f, fyr, rcid, vcs))

dirtup2 = sorted(dirtup, key=operator.itemgetter(2))

for key, group in itertools.groupby(dirtup2, operator.itemgetter(2)):
    maxval = max(x[1] for x in dirtup2)

print [x for x in dirtup2 if x[1] == maxval] 
这只返回与每个子列表的最大fyr匹配的元组,而不是与最大fyr匹配的元组

编辑


使用mglison的第一个答案,我能够得到输出(包含第二个最大值项的元组)。

您可以根据特定字段对每个子列表进行排序,并获取排序子列表的第一个元素

for key,group in itertools.groupby(dirtup2,operator.itemgetter(2)):
    newlist=sorted(group,key=operator.itemgetter(1),reverse=True)
    tuple_with_max=newlist[0]
    print tuple_with_max

@selllikesybok谢谢你清理代码我再次编辑了这个,试图让问题更清楚一点:我喜欢在担心将结果写入文件之前使用打印来验证事情,但这意味着有时我可能会有点迷路。如果我在两个语句之间传递结果,只关注其中一个是不好的。我想我不明白第二行在子列表中的迭代是如何工作的。@mburkenysdot--对不起。我不明白你的问题。你能再试一次吗?@mburkenysdot--我已经更新了我的答案。我不太清楚你想做什么,但我猜到了。让我知道这是否有效。我只是想说,我非常感谢你给我的帮助(这是你提供的第二个有用答案)。--我这边的问题属于我。希望我能在对原始问题的编辑中更清楚地说明我的意图。