Printing specific values out from a dictionary - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Printing specific values out from a dictionary (/thread-39773.html) |
Printing specific values out from a dictionary - mcoliver88 - Apr-12-2023 Hi All, So I have created a dictionary: myDict = { "Name": {"Kindle Fire", "Samsung Galaxy Tab", "Dell Streak", "Apple iPad", "Lenovo Tab"}, "Memory": {16, 8, 16, 64, 32}, "Processor": {2.2, 1.2, 1.8, 2.65, 2.3}, "Cost": {199, 114.99, 59.99, 319, 129}, }I want to write a bit of python code that will print out the keys and the values of the devices that are over 150 in cost. However I am not sure how to do that? Any idea or a place where to look? Thanks RE: Printing specific values out from a dictionary - menator01 - Apr-12-2023 Maybe restructure the dict like this myDict = { 'Kendle Fire': {'Memory': 16, 'Processor': 2.2, 'Cost': 199 }, 'Samsung Galaxy': {'Memory': 8, 'Processor': 1.2, 'Cost': 114.99}, 'Dell Streak': {'Memory': 16, 'Processor': 1.8, 'Cost': 59.99}, 'Apple IPad': {'Memory': 64, 'Processor': 2.65, 'Cost': 319}, 'Lenovo Tab': {'Memory': 32, 'Processor': 2.3, 'Cost': 129} } for item in myDict.keys(): for data, info in myDict[item].items(): if info > 150: print(item, myDict[item])output
RE: Printing specific values out from a dictionary - buran - Apr-12-2023 Note that values in your dict are set , i.e. unordered collection
RE: Printing specific values out from a dictionary - mcoliver88 - Apr-12-2023 (Apr-12-2023, 06:42 PM)menator01 Wrote: Maybe restructure the dict like this This is perfect Thank you, I assumed that nested for Loops would be needed but wasn't sure on the execution. Thanks again. RE: Printing specific values out from a dictionary - deanhystad - Apr-12-2023 Or a dataclass. https://docs.python.org/3/library/dataclasses.html from dataclasses import dataclass @dataclass(order=True) class Computer: name: str memory: float processor: str cost: float computers = [ Computer("Kindle Fire", 16, "2.2", 199), Computer("Samsung Galaxy", 8, "1.2", 114.99), Computer("Dell Streak", 16, "1.8", 59.99), Computer("Apple IPad", 64, "2.65", 319), Computer("Lenovo Tab", 32, "2.3", 129), ] print("Computers", *sorted(computers), sep="\n") print("\nBy price", *sorted(computers, key=lambda x: x.cost), sep="\n") print("\nBy memory", *sorted(computers, key=lambda x: x.memory), sep="\n") print("\nLess than $150", *(c for c in computers if c.cost < 150), sep="\n")
RE: Printing specific values out from a dictionary - snippsat - Apr-12-2023 As mention bye buran so is it set ,then there is one more surprise.>>> memory = {16, 8, 16, 64, 32} >>> memory {16, 8, 32, 64}Oops missing one valueđź‘€ So list/tuple would be more suited for this task,and to show an other way,Pandas do also like this kind of structure.import pandas as pd my_dict = { "Name": ["Kindle Fire", "Samsung Galaxy Tab", "Dell Streak", "Apple iPad", "Lenovo Tab"], "Memory": [16, 8, 16, 64, 32], "Processor": [2.2, 1.2, 1.8, 2.65, 2.3], "Cost": [199, 114.99, 59.99, 319, 129], } df = pd.DataFrame(my_dict) >>> df Name Memory Processor Cost 0 Kindle Fire 16 2.20 199.00 1 Samsung Galaxy Tab 8 1.20 114.99 2 Dell Streak 16 1.80 59.99 3 Apple iPad 64 2.65 319.00 4 Lenovo Tab 32 2.30 129.00 >>> # Select the devices whose cost is over 150 using query >>> selected_devices = df.query('Cost > 150')['Name'] >>> selected_devices 0 Kindle Fire 3 Apple iPad Name: Name, dtype: object RE: Printing specific values out from a dictionary - deanhystad - Apr-12-2023 Pandas plays nice with dataclasses. from dataclasses import dataclass import pandas as pd @dataclass(order=True) class Computer: name: str memory: float processor: str cost: float computers = [ Computer("Kendle Fire", 16, "2.2", 199), Computer("Samsung Galaxy", 8, "1.2", 114.99), Computer("Dell Streak", 16, "1.8", 59.99), Computer("Apple IPad", 64, "2.65", 319), Computer("Lenovo Tab", 32, "2.3", 129), ] df = pd.DataFrame(computers) print("All computers", df, sep="\n") print("\nCheap computers", df[df["cost"] < 150], sep="\n") # could also use query like snippsat
|