因為我改的 有點亂 我拿一個類似乾淨的 當範例
import pandas as pd
import numpy
collected_list = []
target_list = []
target_item_list = []
with open('19666.txt') as reader, open('new1966tt.txt', 'w') as writer:
for index, line in enumerate(reader):
inner_idx = line.split()[0]
item = line.split()[1]
target_list.append((inner_idx, item))
target_item_list.append(item)
if index % 50 == 0:
max_item = max(target_item_list)
max_item_idx = target_item_list.index(max_item)
collected_list.append(target_list[max_item_idx-1:max_item_idx+3])
target_list = []
target_item_list = []
writer.write(str(collected_list))
存入txt的結果:
[[('time', 'number')]][[('time', 'number')], [('0.017', '0.107'), ('0.018', '0.115'), ('0.019', '0.113'), ('0.02', '0.109')]][[('time', 'number')], [('0.017', '0.107'), ('0.018', '0.115'), ('0.019', '0.113'), ('0.02', '0.109')], [('0.067', '0.218'), ('0.068', '0.253'), ('0.069', '0.234'), ('0.07', '0.182')]]
我想得到的是 每50找一次 後換行在寫入另外50行得到的結果
[('0.017', '0.107'), ('0.018', '0.115'), ('0.019', '0.113'), ('0.02', '0.109')]
[('0.067', '0.218'), ('0.068', '0.253'), ('0.069', '0.234'), ('0.07', '0.182')]
我嘗試過使用 類似這樣跟set(但他無法處理多維) 還有其他等等的方法但是沒用,感覺是if index % 50 == 0:那邊常識就可以得到我要的結果 但一子無法
也嘗試把它寫入下面
c = []
for i in collected_list:
if i not in c:
c.append(i)
collected_list = c