我正在處理從csv文件收集的列表或詞典。我想這樣做是寫出來與特定的屬性相關的最大值和最小值新的CSV文件,如:具有相同屬性的Python最大和最小記錄
field1 field2 field3
1 hi 1
2 hi 5
3 bye 2
4 bye 7
應的屬性在field2
在新的CSV文件中列出如下:
f1 f2 min max
1 hi 1 5
2 bye 2 7
我的數據結構非常薄弱,但我嘗試了幾種不同的方式,包括從csv文件中讀取它。我認爲如果我將數據放入字典或列表中,只要能夠在找到最小值和最大值時將其輸出到csv文件,就可以輕鬆找到它。
這是我試過的。我認爲這是一個算法問題。底部的代碼適用於我,但我不知道什麼時候它們不再相等,所以我不知道什麼時候將它放在csv文件中,如min;清單完成時間是什麼時候?
第一次嘗試:
dict_rows = {}
frames = []
lines = (line.strip() for line in open(csvFile))
reader = csv.reader(lines, delimiter='\t', quoting=csv.QUOTE_NONE)
i = 0
for rec in reader:
#print rec
dict_rows[i] = (rec[1],rec[5])
i += 1
## for key in dict_rows[1]:
## if dict_rows[key]>max:
## max = d[key]
## if d[1] == d[1]:
## print d
## print "equal"
print dict_rows
max_value = max(dict_rows.values())
min_value = min(dict_rows.values())
print max_value
print min_value
這似乎更接近,但:
prev_line = None
lines = (line.strip() for line in open(csvFile))
## for line in lines:
## print prev_line,line
## prev_line = line
reader = csv.reader(lines, delimiter='\t', quoting=csv.QUOTE_NONE)
i = 1
frames = []
x = bool
for line in reader:
print '%s) %s ' %(i,line)
#print 'Previous: %s \n Current: %s' %(prev_line, line)
#print '%s) %s ' %(prev_line,line)
## if i == 1:
## print 'First line header'
## next_line = reader.next()
if prev_line != None:
## if prev_line[1] != line[1]:
## print '%i) Does NOT %s != %s ?' %(i, prev_line[1],line[1])
if prev_line[1] == line[1]:
print '%i) EQUAL! %s == %s' %(i, prev_line[1],line[1])
num = line[5]
frames.append(num)
x = True
else:
print '%i) Does NOT %s != %s ?' %(i, prev_line[1],line[1])
frames = []
x = False
prev_line = line
if x == True:
min_frame = min(frames)
max_frame = max(frames)
else:
min_frame = 0
max_frame = 0
print min_frame
print max_frame
else:
next_line = reader.next()
print 'Next: %s' % next_line[1]
print '%i) Does %s == %s == %s ?' %(i, prev_line[1],line[1],next_line[1])
if line[1] != next_line[1]:
print '%i) %s != %s' %(i, line[1],next_line[1])
elif line[1] != next_line:
print '%i) Does not! %s != %s' %(i, line[1],next_line[1])
i +=1
請您清理的例子。 –
仍然需要相當多的清理。這相當混亂。 –