-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lists and For Loops-312.py
128 lines (100 loc) · 3.68 KB
/
Lists and For Loops-312.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
## 1. Lists ##
row_2=['Instagram',0.0,'USD',2161558,4.5]
row_3=['Clash of Clans', 0.0, 'USD',2130805,4.5]
## 2. Indexing ##
row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
ratings_1=row_1[3]
ratings_2=row_2[3]
ratings_3=row_3[3]
total=(ratings_1+ratings_2+ratings_3)
average=total/3
## 3. Negative Indexing ##
row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
rating_1=row_1[-1]
rating_2=row_2[-1]
rating_3=row_3[-1]
total_rating=(rating_1+rating_2+rating_3)
average_rating=total_rating/3
## 4. Retrieving Multiple List Elements ##
row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4= ['Temple Run', 0.0, 'USD', 1724546, 4.5]
row_5 = ['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]
fb_rating_data=[row_1[0],row_1[3],row_1[-1]]
insta_rating_data=[row_2[0],row_2[3],row_2[-1]]
pandora_rating_data=[row_5[0],row_5[3],row_5[-1]]
total=fb_rating_data[-1]+insta_rating_data[-1]+pandora_rating_data[-1]
avg_rating=total/3
## 5. List Slicing ##
row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Temple Run', 0.0, 'USD', 1724546, 4.5]
row_5 = ['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]
first_4_fb=row_1[:4]
last_3_fb=row_1[-3:]
pandora_3_4=row_5[2:4]
## 6. List of Lists ##
row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Temple Run', 0.0, 'USD', 1724546, 4.5]
row_5 = ['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]
app_data_set=[row_1, row_2, row_3, row_4, row_5]
avg_rating=(app_data_set[0][-1]+app_data_set[1][-1]+app_data_set[2][-1]+app_data_set[3][-1]+app_data_set[4][-1])/5
## 7. Opening a File ##
from csv import reader
opened_file=open('AppleStore.csv')
read_file=reader(opened_file)
apps_data=list(read_file)
print(len(apps_data))
print(apps_data[:1])
print(apps_data[1:4])
## 8. Repetitive Processes ##
row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Temple Run', 0.0, 'USD', 1724546, 4.5]
row_5 = ['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]
app_data_set = [row_1, row_2, row_3, row_4, row_5]
for each_list in app_data_set:
print(each_list)
## 9. For Loops ##
row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Temple Run', 0.0, 'USD', 1724546, 4.5]
row_5 = ['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]
app_data_set = [row_1, row_2, row_3, row_4, row_5]
rating_sum=0
for row_ in app_data_set:
rating=row_[-1]
rating_sum=rating+rating_sum
print(rating_sum)
avg_rating=rating_sum/len(app_data_set)
## 10. The Average App Rating ##
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
rating_sum=0
for row in apps_data[1:]:
rating=float(row[7])
rating_sum=rating+rating_sum
avg_rating=float(rating_sum)/len(apps_data[1:])
## 11. Alternative Way to Compute an Average ##
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
all_ratings=[]
for row in apps_data[1:]:
rating=float(row[7])
all_ratings.append(rating)
avg_rating=sum(all_ratings)/len(apps_data[1:])
print(avg_rating)