Guest User

Untitled

a guest
Feb 18th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. # This algorithm demonstrates how to apply list comprehension in Python
  2.  
  3. # Criteria that must be met for an entry to be returned
  4. target_values = [{'body_style': 'SUV','drive_train': '4WD','price_range': '$30k-$40k'},
  5. {'body_style': 'Sedan','drive_train':'Front','price_range': '$20k-$30k'}]
  6.  
  7. # Sample dataset of cars
  8. sample_dataset = [{'make': 'Acura','model': 'MDX','body_style': 'SUV','drive_train': '4WD','price_range':'$40k-$50k'},
  9. {'make': 'Acura','model': 'RSX','body_style': 'Sedan','drive_train': 'Front','price_range':'$20k-$30k'},
  10. {'make': 'Acura','model': 'TSX','body_style': 'Sedan','drive_train': 'Front','price_range':'$20k-$30k'},
  11. {'make': 'Acura','model': 'TL','body_style': 'Sedan','drive_train': 'Front','price_range':'$30k-$40k'},
  12. {'make': 'Acura','model': 'RL','body_style': 'Sedan','drive_train': 'Front','price_range':'$40k-$50k'},
  13. {'make': 'Honda','model': 'CR-V','body_style': 'SUV','drive_train': '4WD','price_range':'$30k-$40k'},
  14. {'make': 'Honda','model': 'Pilot','body_style': 'SUV','drive_train': '4WD','price_range':'$40k-$50k'},
  15. {'make': 'Honda','model': 'Civic','body_style': 'Sedan','drive_train': 'Front','price_range':'$10k-$20k'},
  16. {'make': 'Honda','model': 'Accord','body_style': 'Sedan','drive_train': 'Front','price_range':'$20k-$30k'}]
  17.  
  18. # Array to store results
  19. matching_values = []
  20.  
  21. # Search function
  22. def search(body_style, drive_train, price_range, data):
  23. search_result = [element for element in data if element['body_style'] == body_style and element['drive_train'] == drive_train and element['price_range'] == price_range]
  24. return search_result
  25.  
  26. # Iterate over dataset to find cars that meet criteria
  27. for i in sample_dataset:
  28. search_result = search(i['body_style'], i['drive_train'], i['price_range'], target_values)
  29. if search_result:
  30. matching_values.append(i)
Add Comment
Please, Sign In to add comment