Posts: 6
Threads: 3
Joined: Jul 2020
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
Posts: 1,145
Threads: 114
Joined: Sep 2019
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
Output: Kendle Fire {'Memory': 16, 'Processor': 2.2, 'Cost': 199}
Apple IPad {'Memory': 64, 'Processor': 2.65, 'Cost': 319}
Posts: 8,165
Threads: 160
Joined: Sep 2016
Note that values in your dict are set , i.e. unordered collection
Gribouillis likes this post
Posts: 6
Threads: 3
Joined: Jul 2020
(Apr-12-2023, 06:42 PM)menator01 Wrote: 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
Output: Kendle Fire {'Memory': 16, 'Processor': 2.2, 'Cost': 199}
Apple IPad {'Memory': 64, 'Processor': 2.65, 'Cost': 319}
This is perfect Thank you, I assumed that nested for Loops would be needed but wasn't sure on the execution. Thanks again.
Posts: 6,809
Threads: 20
Joined: Feb 2020
Apr-12-2023, 07:32 PM
(This post was last modified: Apr-12-2023, 07:59 PM by deanhystad.)
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") Output: Computers
Computer(name='Apple IPad', memory=64, processor='2.65', cost=319)
Computer(name='Dell Streak', memory=16, processor='1.8', cost=59.99)
Computer(name='Kendle Fire', memory=16, processor='2.2', cost=199)
Computer(name='Lenovo Tab', memory=32, processor='2.3', cost=129)
Computer(name='Samsung Galaxy', memory=8, processor='1.2', cost=114.99)
By price
Computer(name='Dell Streak', memory=16, processor='1.8', cost=59.99)
Computer(name='Samsung Galaxy', memory=8, processor='1.2', cost=114.99)
Computer(name='Lenovo Tab', memory=32, processor='2.3', cost=129)
Computer(name='Kendle Fire', memory=16, processor='2.2', cost=199)
Computer(name='Apple IPad', memory=64, processor='2.65', cost=319)
By memory
Computer(name='Samsung Galaxy', memory=8, processor='1.2', cost=114.99)
Computer(name='Kendle Fire', memory=16, processor='2.2', cost=199)
Computer(name='Dell Streak', memory=16, processor='1.8', cost=59.99)
Computer(name='Lenovo Tab', memory=32, processor='2.3', cost=129)
Computer(name='Apple IPad', memory=64, processor='2.65', cost=319)
Less than $150
Computer(name='Samsung Galaxy', memory=8, processor='1.2', cost=114.99)
Computer(name='Dell Streak', memory=16, processor='1.8', cost=59.99)
Computer(name='Lenovo Tab', memory=32, processor='2.3', cost=129)
Posts: 7,324
Threads: 123
Joined: Sep 2016
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
Posts: 6,809
Threads: 20
Joined: Feb 2020
Apr-12-2023, 08:10 PM
(This post was last modified: Apr-12-2023, 08:10 PM by deanhystad.)
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 Output: All computers
name memory processor cost
0 Kendle Fire 16 2.2 199.00
1 Samsung Galaxy 8 1.2 114.99
2 Dell Streak 16 1.8 59.99
3 Apple IPad 64 2.65 319.00
4 Lenovo Tab 32 2.3 129.00
Cheap computers
name memory processor cost
1 Samsung Galaxy 8 1.2 114.99
2 Dell Streak 16 1.8 59.99
4 Lenovo Tab 32 2.3 129.00
|