简体   繁体   English

在循环中检查重复项后如何在列表中追加元素?

[英]How to append elements in a list after checking for its duplicates in a loop?

I have a bunch of columns with credit card numbers along with other non creditcard numbers which I am reading from a .csv file. 我有一堆列,其中包含信用卡号以及从.csv文件读取的其他非信用卡号。 I want to filter the credit card numbers first using the regular expression and then passing each of those (card numbers) values to a function that performs the Luhn check to see if valid credit card or not. 我想首先使用正则表达式过滤信用卡号,然后将每个(卡号)值传递给执行Luhn检查以查看是否有效的信用卡的函数。 If the function returns true then I append the index value of the credit card to a list. 如果该函数返回true,则将信用卡的索引值附加到列表中。 I am using index values later using .iloc to get the entire row. 我稍后使用索引值使用.iloc来获取整行。

This is what I have done so far 这是我到目前为止所做的

  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

My question is how do I remove the duplicates cards and then append the index values. 我的问题是如何删除重复的卡,然后附加索引值。

Thank you in advance! 先感谢您!

Using a set might be a way to do this: 使用集合可能是这样做的一种方式:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM