I have a list, which contains information about the class number and the keywords. I will call it 'class-list':
[(0, ['word1', 'word2', 'word3', 'word4']), (1, ['word5', 'word6', 'word7', 'word8']), (2, ['word1', 'word9', 'word3', 'word7'])
And I have a postgres database, where every entry has a list of words, let's say a 'db-list'.
['word1', 'word2', 'word10', 'word7', 'word3']
The idea is to assign the class to the data in the database based on the 'class-list'. If 'db-list' contains the word1 and the word2 then the class '0' will be assigned to this description. I don't know how to write the condition "if two elements of the 'db-list' (of k) == two elements of the 'class-list'". Any help is highly appreciated!
cur.execute("SELECT * From table")
dbrows = cur.fetchall()
for k in dbrows:
if two elements of the 'db-list' (of k) == two elements of the 'class-list':
sql = "INSERT INTO table VALUES (class);"""
cur.execute(sql)
You can create sets from both keyword lists and count elements in the intersection subset. Example:
dblist = [(0, ['word1', 'word2', 'word3', 'word4']), (1, ['word5', 'word6', 'word7', 'word8']), (2, ['word1', 'word9', 'word3', 'word7'])]
dbrow = ['word1', 'word2', 'word10', 'word7', 'word3']
for class_idx, kwords in dblist:
if len(set(dbrow) & set(kwords)) == 2:
print kwords # or do whatever you need
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.