简体   繁体   中英

Python: Compare List with a List in File

I am strugling with this problem that looks simple but I'm stuck! Well, I have to build a function where I receive a list of categories like:

input Example1: ['point_of_interest', 'natural_feature', 'park', 'establishment']
input Example2: ['point_of_interest', 'establishment']
input Example3: ['sublocality', 'political']

So that list could be with variable elements inside I guess from 1 till 4 not more

So with this same data I am gonna create a file with that input in a way that if the new input is not in the file, append it to the file .

The way is each list is an element itself, I mean I have to compare the full elements of the list and if I can find other list exactly equal I don´t have to add it.

In my code I just tried to add the first element in the file because really I don't know how to add the full list to compare with the next list.

def categories(category):
    number = 0
    repeat = False
    if os.path.exists("routes/svm/categories"):
        with open('routes/svm/categories', 'rb') as csvfile:
            spamreader = csv.reader(csvfile)
            for categoryFile in spamreader:
                if (cmp(categoryFile,category) == 0):
                    number += 1
                    repeat = True
                if not repeat:
                    categoriesFile = open('routes/svm/categories', 'a') 
                    category = str(category[0])     
                    categoriesFile.write(category) 
                    categoriesFile.write('\n')
                    categoriesFile.close()
                else:
                    categoriesFile = open('routes/svm/categories', 'w')
                    category = str(category[0])     
                    categoriesFile.write(category)
                    categoriesFile.write('\n')
                    categoriesFile.close()      

EDIT: Some explanation by @KlausWarzecha: Users might enter a list with (about 4) items. If this list ( = this combination of items) is not in the file already, you want to add the list (and not the items separately!) to the file?

The problem is really simple. You may take the following approach if it works for you:

  1. Read all the contents of the CSV into a list
  2. Add all the non-matching items from the input into this list
  3. re-write the CSV file

You may start with this sample code:

# input_list here represents the inputs
# You may get input from some other source too
input_list = [['point_of_interest', 'natural_feature', 'park', 'establishment'], ['point_of_interest', 'establishment'], ['sublocality', 'political']]
category_list = []
with open('routes/svm/categories', 'rb') as csvfile:
    spamreader = csv.reader(csvfile)
    for categoryFile in spamreader:
        print categoryFile
        category_list.append(categoryFile)
for item in input_list:
    if (item in category_list):
        print "Found"
    else:
        category_list.append(item)
        print "Not Found"

# Write `category_list` to the CSV file

Please use this code as a starting point and not as a copy-paste solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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