我有一個讀取.csv文件的程序,檢查列長度中是否有任何不匹配(通過將它與頭字段進行比較),然後將它發現的所有內容作爲列表返回(然後將其寫入文件) 。我想這個名單的事,是列出瞭如下結果:在相同的發現不匹配如何找到彼此相鄰的python列表中的重複項,並列出它們的索引?
行號:列的金額該行中
例如
rows: n-m : y
其中n和m是共享與標題不匹配的相同數量的列的行數。
我特地到這些話題,雖然信息是非常有用的,他們不回答這個問題:
Find and list duplicates in a list?
Identify duplicate values in a list in Python
這就是我現在:
r = csv.reader(data, delimiter= '\t')
columns = []
for row in r:
# adds column length to a list
colm = len(row)
columns.append(colm)
b = len(columns)
for a in range(b):
# checks if the current member matches the header length of columns
if columns[a] != columns[0]:
# if it doesnt, write the row and the amount of columns in that row to a file
file.write("row " + str(a + 1) + ": " + str(columns[a]) + " \n")
文件輸出如下所示:
row 7220: 0
row 7221: 0
row 7222: 0
row 7223: 0
row 7224: 0
row 7225: 1
row 7226: 1
時所期望的最終結果是
rows 7220 - 7224 : 0
rows 7225 - 7226 : 1
所以我什麼,我基本上是需要的,我看到它的方式,是一個字典,其中關鍵是有重複的值和值的行被列的量那說錯配。我基本上是想我需要(在一個可怕的書面僞代碼,這並沒有任何意義,現在我寫這個問題後看完年),是在這裏:
def pseudoList():
i = 1
ListOfLists = []
while (i < len(originalList)):
duplicateList = []
if originalList[i] == originalList[i-1]:
duplicateList.append(originalList[i])
i += 1
ListOfLists.append(duplicateList)
def PseudocreateDict(ListOfLists):
pseudoDict = {}
for x in ListOfLists:
a = ListOfLists[x][0] #this is the first node in the uniqueList created
i = len(ListOfLists) - 1
b = listOfLists[x][i] #this is the last node of the uniqueList created
pseudodict.update('key' : '{} - {}'.format(a,b))
然而,這似乎很令人費解做我想要的方式,所以我想知道是否有更有效的方法b)更簡單的方法來做到這一點?