-1
我試過多種方法去除最終輸出,但每次都是一樣的。我不明白爲什麼,我的最終解決方案是在這個例子中:過濾器複製行不起作用
#!/usr/bin/python3
import re
import sys
import json
## Variables
fileOfHosts = "/path/to/file"
## Define lists and dics
my_dic = {}
resultOut = []
## Functions:
# Filter duplicated lines in list
def f1(seq):
newlist = []
for i in seq:
if i not in newlist:
newlist.append(i)
return newlist
# Json converter
def f2(inout):
out = json.dumps(inout)
return out
## Open file as list of lines
with open(fileOfHosts) as fileOfHosts:
result = list(fileOfHosts)
## Parse lines and generate dictionary
for f in f1(result):
sortByWord = re.findall(r"[\w+\.']+", f)
listOfTwo = sortByWord[:2]
if len(listOfTwo) == 2:
my_dic[listOfTwo[0]] = listOfTwo[1]
resultOut.append(my_dic.copy())
## Display list of dictoary as json
print(f2(resultOut))
我也試過在字典列表的末尾過濾。但總是我有相同的重複行。
有人可以提供一個更好的解決方案來篩選出重複?
編輯:這一切
首先是不是在評論中提到的問題的副本。在發佈之前,我嘗試過提到的解決方案。
實際上似乎問題不在於重複刪除方法,而是在創建字典時進行重複。
代碼:
for f in result:
sortByWord = re.findall(r"[\w+\.']+", f)
# print(sortByWord)
listOfTwo = sortByWord[:2]
# print(listOfTwo)
if len(listOfTwo) == 2:
print(listOfTwo)
my_dic[listOfTwo[0]] = listOfTwo[1]
resultOut.append(my_dic)
輸出(打印(listOfTwo)):
['define', 'host']
['host_name', 'HOST_name']
['alias', 'HOST_name']
['address', '127.0.0.1']
['register', '1']
['timezone', 'Europe']
['use', 'user']
['_SNMPCOMMUNITY', 'public']
['_SNMPVERSION', '3']
['_HOST_ID', '184']
['define', 'host']
['host_name', 'HOST_name']
['alias', 'HOST_name']
['address', '127.0.0.1']
['register', '1']
['timezone', 'Europe']
['use', 'user']
['_SNMPCOMMUNITY', 'public']
['_SNMPVERSION', '3']
['_HOST_ID', '185']
輸出(打印(F2(resultOut))):
[{"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}]
我不理解爲什麼。
沒有閱讀所有的代碼,該函數'set'只返回獨特的元素,所以'名單(套(...))'也許可以幫助? – Sosel
可能的重複[如何在保留順序的同時從列表中刪除重複項?](https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-同時保留順序) – Mark
請在你的問題中添加一些示例行。 –