![](/img/trans.png)
[英]why list product duplicates elements after doing append action in python
[英]How to append elements in a list after checking for its duplicates in a loop?
我有一堆列,其中包含信用卡號以及從.csv文件讀取的其他非信用卡號。 我想首先使用正則表達式過濾信用卡號,然后將每個(卡號)值傳遞給執行Luhn檢查以查看是否有效的信用卡的函數。 如果該函數返回true,則將信用卡的索引值附加到列表中。 我稍后使用索引值使用.iloc來獲取整行。
這是我到目前為止所做的
data = pd.read_csv("fetched_data.csv")
summ = data['summary']
values =np.array(summ)
creditcards = []
regex_match_index_list =[]
Validcardsfound = 0
no_duplicate_list =[]
for i in range(len(values)):
temp = re.findall(r'(\b(?:\d[ -]*?){13,16}\b)',str(values[i]))
if temp:
for each in temp:
if doLuhn(str(each)) is True:
#print ("In the loop")
creditcards.append(each)
Validcardsfound = Validcardsfound + 1
regex_match_index_list.append(i)
elif doLuhn(str(temp)) is False:
pass
#print (str(temp))
else:
pass
我的問題是如何刪除重復的卡,然后附加索引值。
先感謝您!
使用集合可能是這樣做的一種方式:
data = pd.read_csv("fetched_data.csv")
summ = data['summary']
values =np.array(summ)
creditcards = set()
regex_match_index_list =[]
Validcardsfound = 0
no_duplicate_list =[]
for i in range(len(values)):
temp = re.findall(r'(\b(?:\d[ -]*?){13,16}\b)',str(values[i]))
if temp:
for each in temp:
if doLuhn(str(each)):
# Add unique valid credit card numbers to set
if not each in creditcards:
creditcards.add(each) #add new card to set
Validcardsfound = Validcardsfound + 1 #increment number of unique cards found
regex_match_index_list.append(i) #append index of new card found
print(creditcards) # credit cards found
print(regex_match_index_list) # index of credit cards in values array
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.