-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterating_dictionaries.py
87 lines (56 loc) · 2.26 KB
/
Iterating_dictionaries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# coding: utf-8
# # Only Keys
# In[18]:
# Create a dictionary called book_title with word frequencies
book_title = {'great': 1, 'expectaions': 2, 'the': 3, 'of': 4, 'the': 5}
# Iterate through each key (word) in the book_title dictionary
for key in book_title:
# Print the key (word)
print(key)
# # Both Keys & Values
# In[23]:
# Create a dictionary called book_title with word frequencies
book_title = {'great': 1, 'expectaions': 2, 'the': 3, 'of': 4, 'the': 5}
# Iterate through each key-value pair in the book_title dictionary
for key, value in book_title.items():
# Print the key (word) and its corresponding value (frequency)
print('Key: {}, Value: {}'.format(key, value))
# # Example
# ## count total fruits
# In[28]:
# Initialize a variable to store the total count
result = 0
# A dictionary representing items in a basket and their quantities
basket_items = {'apples': 4, 'oranges': 19, 'kites': 3, 'sandwiches': 8}
# A list of fruits we want to consider
fruits = ['apples', 'oranges', 'pears', 'peaches', 'grapes', 'bananas']
# Iterate through each key-value pair in the basket_items dictionary
for fruit, count in basket_items.items():
# Check if the fruit is in the list of desired fruits
if fruit in fruits:
# Add the quantity of the fruit to the result
result += count
# Print the total count of desired fruits
print(result)
# ## count fruits and not fruits
# In[34]:
# Initialize a variable to store the total count
fruit_count, not_fruit_count = 0, 0
# A dictionary representing items in a basket and their quantities
basket_items = {'apples': 4, 'oranges': 19, 'kites': 3, 'sandwiches': 8}
# A list of fruits we want to consider
fruits = ['apples', 'oranges', 'pears', 'peaches', 'grapes', 'bananas']
# Iterate through each key-value pair in the basket_items dictionary
for fruit, count in basket_items.items():
# Check if the fruit is in the list of desired fruits
if fruit in fruits:
# Add the quantity of the fruit to the result
fruit_count += 1
else:
# Add the quantity of the not_fruit to the result
not_fruit_count += 1
# Print the total count of types of fruits
print(fruit_count)
# Print the total count of not_fruits
print(not_fruit_count)