假设我有许多值:(左栏是值计数:1、2、3等;右栏是实际值)
1 5.2
2 1.43
3 3.54
4 887
5 0.35
我要做的是将值从递减重新排序为递增(自上而下),然后我希望python遍历这些值,并继续挑选这些值(稍后用作输出),直到遇到一个值达到或高于某个阈值为止。例如:
^{pr2}$假设我的阈值是5.0,所以这里我希望程序放弃1和4(高值),并给出5、2和3以及它们对应的值作为输出。我希望这有道理。如果我的阈值只允许2个值,那么作为一个更棘手的技巧,我希望它忽略所有的东西,并给出类似“找不到值”之类的值。在
它们所在的文件(值和计数)大致如下:
ID some: value another: value another: value another: value another: value another: value 1: 5.2
等等,上面提到的每个值都对应于文件中的一个新行。所以我感兴趣的东西分别位于x行,14列和15列。在
实际线路如下:
Mod# 2 11494 Chi^2: 1.19608371367 Scale: 0.567691651772 Tin: 1499 Teff: 3400 Luminosity: 568.0 L M-dot: 4.3497e-08 Tau: 2.44E-01 Dust composition: Fe IRx1: 0.540471121182
我对IRx1及其后面的值感兴趣。在
下面的解决方案假设数据是作为元组列表读入的。在
例如:
将是问题中样本数据的列表。在
^{pr2}$函数的第一行按元组第二位的值对列表进行排序。在
然后,函数的第二行过滤得到的列表,以便只保留值低于阈值的元素。在
如果第三行包含两个以上的元素,则第三行返回结果排序列表,否则返回“找不到值”,这样可以完成您正在尝试的操作,减少文件输入。在
假设文件每行有一个数字:
如果文件如下所示:
^{pr2}$您希望您的输出是
set([2,3,5])
,然后:给定一个如下所示的文件:
如果有一个制表符(
\t
)分隔11494
和Chi^2
,则以下脚本应该可以工作:使用该函数,您应该能够指定要检查哪些列的值低于阈值。尽管此算法确实具有更高的空间复杂性(使用的RAM比绝对需要的多),但您应该能够在读取文件后marshall/pickle
lines
,并从那里继续进行后续的运行。如果你有一个巨大的输入文件需要一段时间来处理(我想你可能已经有了),这一点特别有用相关问题 更多 >
编程相关推荐