我想在使用正則表達式的文件中找到IP地址的唯一實例。我覺得他們很好,並嘗試追加到列表中,然後嘗試在我的列表中使用set()
刪除重複項。我發現每個項目都可以,並且有重複的內容,但是我無法獲得簡化的列表。打印我的設置的輸出與打印ips作爲列表相同,不會刪除任何內容。設置()不刪除重複
ips = [] # make a list
count = 0
count1 = 0
for line in f: #loop through file line by line
match = re.search("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", line) #find IPs
if match: #if there's a match append and keep track of the total number of Ips
ips.append(match) #append to list
count = count + 1
ipset = set(ips)
print(ipset, count)
此字符串<_sre.SRE_Match object; span=(0, 13), match='137.43.92.119'>
顯示了60+次在輸出之前,並試圖set()
列表
語法correct..maybe你需要檢查'ips' – rock321987
你正在做一組*比賽對象*。 – user2357112
@ rock321987如果我同時打印 – BitFlow