好的,问题是,我有一个包含 N 行的列表,如下所示:
4.96035894 2.94014535 9.71651378 On
8.37470259 9.08139103 10.23145322 Off
5.73085411 4.21656546 9.98718707 On
6.40892867 9.44195654 8.83707549 On
4.26065784 3.74966832 7.89520829 On
8.89601431 9.84208918 9.63054539 On
9.10538764 8.58408119 10.87454882 On
6.21494725 4.61164407 9.08378204 Off
7.62256424 9.59449339 10.84506558 Off
6.49210768 4.03768151 10.75221925 Off
5.04079861 4.99362253 10.34349177 Off
...
目标是在第三个字段中找到具有最低值的 X (X < N) 行(它可以轻松扩展到任何给定字段,但让我们关注第三个字段)并更改第四个字段(始终是一个字符串)根据用户调用的参数切换为 On/Off,即,如果参数为 On,则更改为 On,如果为 Off,则更改为 Off。
在上面的示例中,例如,如果我想将第三个值最低的 3 行更改为关闭,则输出将是:
4.96035894 2.94014535 9.71651378 On
8.37470259 9.08139103 10.23145322 Off
5.73085411 4.21656546 9.98718707 On
6.40892867 9.44195654 8.83707549 Off // this row is changed
4.26065784 3.74966832 7.89520829 Off // this row is changed
8.89601431 9.84208918 9.63054539 On
9.10538764 8.58408119 10.87454882 On
6.21494725 4.61164407 9.08378204 Off // this row is changed
7.62256424 9.59449339 10.84506558 Off
6.49210768 4.03768151 10.75221925 Off
5.04079861 4.99362253 10.34349177 Off
...
我想我可以为 X=1 的特定情况做,最低值行,但我不知道如何扩展到任意 X。也许一个 X 大小的数组填充并在遍历列表时被编辑?