جواب «Step 1: Read the Questionnaire» چالش 1 از جلسهی 52
سؤال
قدم ۱: جدول پرسشنامه آماده است. در حلقه، خطی که رشته (ستون آخر) را به labels اضافه میکند جا افتاده؛ آن را بنویس. خروجی: features: ['maths', 'science', 'literature', 'art', 'likes_people', 'likes_building']، students: 28 و چهار خط maths 7، science 7، humanities 7، art 7.
راهنما
ستون آخر parts[-1] است: labels.append(...).
یک جواب درست
DATA = '''
maths,science,literature,art,likes_people,likes_building,field
18,15,12,10,0,1,maths
17,14,13,11,0,1,maths
19,16,11,9,1,1,maths
16,13,14,12,0,1,maths
18,17,12,13,0,0,maths
15,14,10,11,0,1,maths
17,12,15,10,1,1,maths
14,18,13,10,1,0,science
15,19,12,11,1,0,science
13,17,14,12,1,0,science
16,18,11,10,1,0,science
12,16,15,13,1,0,science
15,17,13,9,0,0,science
14,16,12,14,1,1,science
11,12,18,13,1,0,humanities
10,13,17,12,1,0,humanities
12,11,19,14,1,0,humanities
13,14,16,11,1,0,humanities
9,12,18,15,0,0,humanities
12,13,17,10,1,1,humanities
11,15,16,12,1,0,humanities
10,11,14,18,0,0,art
12,12,15,19,1,0,art
11,13,13,17,0,1,art
13,11,16,18,1,0,art
9,10,14,16,0,0,art
14,12,12,17,0,1,art
12,14,15,16,1,0,art
'''.strip()
lines = DATA.splitlines()
names = lines[0].split(",")[:-1]
rows = []
labels = []
for line in lines[1:]:
parts = line.split(",")
rows.append([int(v) for v in parts[:-1]])
labels.append(parts[-1])
print("features:", names)
print("students:", len(rows))
for field in ["maths", "science", "humanities", "art"]:
print(field, labels.count(field))
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن