简体   繁体   中英

Find matching elements in one array

I have a list of objects which looks like this:

Bidder - Timestamp: 11, User ID: 8, Action: BID, Loan ID: 430, Rate: 0.20 -- missed
Bidder - Timestamp: 13, User ID: 13, Action: BID, Loan ID: 430, Rate: 0.15
Bidder - Timestamp: 17, User ID: 8, Action: BID, Loan ID: 430, Rate: 0.10 -- miss
Bidder - Timestamp: 18, User ID: 1, Action: BID, Loan ID: 431, Rate: 0.15
Bidder - Timestamp: 19, User ID: 3, Action: BID, Loan ID: 431, Rate: 0.14
Bidder - Timestamp: 21, User ID: 3, Action: BID, Loan ID: 431, Rate: 0.14

I'm trying to find all matching User ID but am having trouble doing so, the problem i get with my code is that i end up missing one matching ID which i have shown above.

Here is my code that i currently have which gets first element and compares it against the next:

self._bidders = [] # List of bidders

for idx, firstElement in enumerate(self._bidders):
    FirstElement = firstElement
    NextElement = self._bidders[(idx + 1) % len(self._bidders)]
    if FirstElement.user_id == NextElement.user_id:
       #Do something

How would i make sure i get all matching User ID without missing any and without using any imports?, any suggestions/help would be appreciated.

Here is a method to convert your list of objects into a data frame, from which you will then be able to find matches quite easily:

# Create a list of lists
data = ['Bidder - Timestamp: 11, User ID: 8, Action: BID, Loan ID: 430, Rate: 0.20',
        'Bidder - Timestamp: 13, User ID: 13, Action: BID, Loan ID: 430, Rate: 0.15',
        'Bidder - Timestamp: 17, User ID: 8, Action: BID, Loan ID: 430, Rate: 0.10',
        'Bidder - Timestamp: 18, User ID: 1, Action: BID, Loan ID: 431, Rate: 0.15',
        'Bidder - Timestamp: 19, User ID: 3, Action: BID, Loan ID: 431, Rate: 0.14',
        'Bidder - Timestamp: 21, User ID: 3, Action: BID, Loan ID: 431, Rate: 0.14']

df = pd.DataFrame([d.split(',') for d in data])
# df = pd.DataFrame([list(d).split(',') for d in data]) # Use this for your list of objects

df2 = pd.DataFrame()
for i in range(len(df.columns)):
    name = df.iloc[:,i].str.split(':', expand=True)[0][0].strip()
    values = df.iloc[:,i].str.split(':', expand=True)[1].str.strip()
    df2[name] = values

print(df2)
  Bidder - Timestamp User ID Action Loan ID  Rate
0                 11       8    BID     430  0.20
1                 13      13    BID     430  0.15
2                 17       8    BID     430  0.10
3                 18       1    BID     431  0.15
4                 19       3    BID     431  0.14
5                 21       3    BID     431  0.14
# Find matches
df2[df2['User ID'] == '8']
  Bidder - Timestamp User ID Action Loan ID  Rate
0                 11       8    BID     430  0.20
2                 17       8    BID     430  0.10

Try this, a for loop:

data = ['Bidder - Timestamp: 11, User ID: 8, Action: BID, Loan ID: 430, Rate: 0.20',
        'Bidder - Timestamp: 13, User ID: 13, Action: BID, Loan ID: 430, Rate: 0.15',
        'Bidder - Timestamp: 17, User ID: 8, Action: BID, Loan ID: 430, Rate: 0.10',
        'Bidder - Timestamp: 18, User ID: 1, Action: BID, Loan ID: 431, Rate: 0.15',
        'Bidder - Timestamp: 19, User ID: 3, Action: BID, Loan ID: 431, Rate: 0.14',
        'Bidder - Timestamp: 21, User ID: 3, Action: BID, Loan ID: 431, Rate: 0.14']
l = [[i.split(':')[0].strip() for i in data[0].split(',')]]
for i in data:
    l.append([x.split(':')[1].strip() for x in i.split(',')])

Or a one-liner list comprehension:

data = ['Bidder - Timestamp: 11, User ID: 8, Action: BID, Loan ID: 430, Rate: 0.20',
        'Bidder - Timestamp: 13, User ID: 13, Action: BID, Loan ID: 430, Rate: 0.15',
        'Bidder - Timestamp: 17, User ID: 8, Action: BID, Loan ID: 430, Rate: 0.10',
        'Bidder - Timestamp: 18, User ID: 1, Action: BID, Loan ID: 431, Rate: 0.15',
        'Bidder - Timestamp: 19, User ID: 3, Action: BID, Loan ID: 431, Rate: 0.14',
        'Bidder - Timestamp: 21, User ID: 3, Action: BID, Loan ID: 431, Rate: 0.14']
l = [[i.split(':')[0].strip() for i in data[0].split(',')]] + [[x.split(':')[1].strip() for x in i.split(',')] for i in data]

You can use a dictionary to first store how many times an id occurs in the list. Then you can filter your original list based on whether an id is a duplicate or not:

bidders = [
    Bidder(11, 8, 'BID', 430, 0.20),
    Bidder(13, 13, 'BID', 430, 0.15),
    Bidder(17, 8, 'BID', 430, 0.10),
    Bidder(18, 1, 'BID', 431, 0.15),
    Bidder(19, 3, 'BID', 431, 0.14),
    Bidder(21, 3, 'BID', 431, 0.14)
]

id_counts = {}
for b in bidders:
    if b.user_id in id_counts:
        id_counts[b.user_id] += 1
    else:
        id_counts[b.user_id] = 1

result = [b for b in bidders if id_counts[b.user_id] > 1]
print(result)

Output

[Timestamp: 11, User Id: 8, Action: BID, Loan ID: 430, Rate: 0.2,
 Timestamp: 17, User Id: 8, Action: BID, Loan ID: 430, Rate: 0.1,
 Timestamp: 19, User Id: 3, Action: BID, Loan ID: 431, Rate: 0.14,
 Timestamp: 21, User Id: 3, Action: BID, Loan ID: 431, Rate: 0.14]

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