Python Forum
Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matching question
#11
(Nov-21-2017, 07:01 PM)iFunKtion Wrote: A slightly simpler way would be to just use the one for loop, as you only need to iterate through one list and check if each iteration is in the other list:
participants = ('bob', 'bill', 'casey')
paid = ('bob', 'bill')

match = 0
for name in participants:
    if name in paid:
        match += 1

print(match)
I would expect this to be quicker than using two loops also.

Thanks!

The final code including writing back each match to the db is:

import psycopg2 as p
import re

conn = p.connect ("dbname='participants_db' user='postgres' host= 'localhost'")
cur = conn.cursor()
cur.execute("select participant_name from participants")
names = cur.fetchall()

cur2 = conn.cursor()
cur2.execute("select paid_name from participants_paid")
paid = cur2.fetchall()


match = 0
for name in names:
    if name in paid:
        match += 1
        cur.execute("UPDATE participants SET participant_paid = 'Yes' WHERE participant_name = (%s)", (name,))

conn.commit()
Reply
#12
Thanks for the update.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020