Python 3.8下对比API返回JSON列表、提取新增条目及优化相同条目对比方式的技术咨询
Hey there! Let's break down practical solutions to your two problems—your current nested loop approach will get slow as listings grow, and we can fix that while making it easy to spot new entries.
1. More Efficient Way to Compare Matching Entries
Your current nested loops (for listingOld in prevListings + for listingNew in newListings) have an O(n²) time complexity, which gets sluggish as the number of NFTs increases. Instead, we can convert both listing lists into dictionaries keyed by each NFT's id—this lets us look up entries in O(1) time, making the whole process way faster.
Here's how to refactor your comparison logic:
# Convert lists to dictionaries using 'id' as the key for instant lookups prev_dict = {item['id']: item for item in prevListings} new_dict = {item['id']: item for item in newListings} # Check for changes in existing listings (like sold status updates) for nft_id, new_item in new_dict.items(): if nft_id in prev_dict: old_item = prev_dict[nft_id] # Replicate your original logic for tracking lastSoldPrice changes if new_item['lastSoldPrice'] is not None and new_item['lastSoldPrice'] != old_item['lastSoldPrice']: # Do your action here—e.g., notify about the sale await bot.get_channel(893292122298540083).send(f"NFT {new_item['name']} sold for {new_item['lastSoldPrice']} SOL!")
This cuts the comparison time down to O(n), which scales much better as your listing set grows.
2. Generate a List of Only New Entries
To get entries that exist in newListings but not in prevListings, we can use set operations on the dictionary keys, then extract the corresponding items from new_dict.
Here's the code to create your dedicated "new entries" list:
# Get IDs that are present in the new listings but missing from the previous ones new_nft_ids = new_dict.keys() - prev_dict.keys() # Build a list containing only the new NFT entries new_entries = [new_dict[nft_id] for nft_id in new_nft_ids] # Notify and process new entries if any exist if new_entries: await bot.get_channel(893292122298540083).send(f"Found {len(new_entries)} new NFT listing(s)!") for entry in new_entries: # Customize this to share details like price, image, etc. await bot.get_channel(893292122298540083).send( f"New Listing: {entry['name']}\nPrice: {entry['price']} SOL\nImage Link: {entry['link_img']}" )
This gives you a clean, filtered list of only the NFTs added since your last API call.
Bonus: Combine Both Logics for Clean Code
You can wrap both the change detection and new entry extraction into a single organized block:
prev_dict = {item['id']: item for item in prevListings} new_dict = {item['id']: item for item in newListings} # Check for sold/updated listings first for nft_id, new_item in new_dict.items(): if nft_id in prev_dict: old_item = prev_dict[nft_id] if new_item['lastSoldPrice'] is not None and new_item['lastSoldPrice'] != old_item['lastSoldPrice']: await bot.get_channel(893292122298540083).send(f"✅ Sold: {new_item['name']} for {new_item['lastSoldPrice']} SOL") # Then check for new listings new_nft_ids = new_dict.keys() - prev_dict.keys() new_entries = [new_dict[nft_id] for nft_id in new_nft_ids] if new_entries: await bot.get_channel(893292122298540083).send(f"🔍 Found {len(new_entries)} new listings!") for entry in new_entries: await bot.get_channel(893292122298540083).send( f"**{entry['name']}**\nPrice: {entry['price']} SOL\nSeller: {entry['seller_address']}" )
This code is efficient, readable, and fully compatible with Python 3.8.
内容的提问来源于stack exchange,提问作者Bewinxed

