'wondering about format index
first of all, I'm korean and i'm not good at english but i'll try my best I was wondering about format(last line of this code). this is code that making question (I used translator at #things) this is about calculating average of passerby by time zone (translator got wrong. not row. it's passerby)
import csv
a = [[],[],[],[],[],[],[]] # List Declaration of Size 7 x 24
# a[i] = dictionary {num, wnum, ynum} : Data to be stored in each time zone for each day of the week
# Read data from csv file and store it in a two-dimensional array a[][]
with open('passby_data.csv', 'r') as f :
reader = csv.DictReader(f)
i =j= 0
for row in reader :
a[i].append(row)
j = j + 1
if(j % 24 == 0) : # If you read 24 data, move on to the next day of the week
i = i + 1
# -------------------------------------------------------------------------
day_title = ['MON', 'TUE', 'WED', 'THR', 'FRI', 'SAT', 'SUN'] # the title of the day
hour_title = ['01', '02', '03', '04', '05', '06', \
'07', '08','09', '10', '11', '12', \
'13', '14','15', '16', '17', '18', \
'19', '20','21', '22', '23', '24',]
# Obtain weekly averages by time zone
avgh = []
for j in range(0, 24) : # Repeat 19 to 24 lines for 0 to 23 hours
day_sum = 0 # Initialize to 0 to get the sum by time zone
# Jth Time Zone Weekly Sum
for i in range (0, 7) : # Repeat seven times a week
day_sum = day_sum + int(a[i][j]['num']) # Accumulate the number of rows by time zone j on the i-th day of the week
avgh.append(day_sum/7) # Find the average number of weekly rowers in the j-th time zone
# To output the average floating population by time zone
for j in range (0, 24) : # repeat 24 times
print("[~{0}:00]: {1:4}". format(hour_title[j], int(avgh[j]))) # Output of floating population by time zone
and this is what i got.
[~01:00]: 1
[~02:00]: 0
[~03:00]: 0
[~04:00]: 0
[~05:00]: 0
[~06:00]: 2
[~07:00]: 2
[~08:00]: 12
[~09:00]: 16
[~10:00]: 10
[~11:00]: 3
[~12:00]: 19
[~13:00]: 17
[~14:00]: 7
[~15:00]: 9
[~16:00]: 12
[~17:00]: 9
[~18:00]: 8
[~19:00]: 17
[~20:00]: 15
[~21:00]: 11
[~22:00]: 5
[~23:00]: 6
[~24:00]: 4
Process finished with exit code 0
this is what they tried. i got question at here. print("[~{0}:00]: {1:4}". format(hour_title[j], int(avgh[j]))) # Output of floating population by time zone why they're using {1:4}? not {0:4}. so i tried {0:4}.
import csv
a = [[],[],[],[],[],[],[]] # List Declaration of Size 7 x 24
# a[i] = dictionary {num, wnum, ynum} : Data to be stored in each time zone for each day of the week
# Read data from csv file and store it in a two-dimensional array a[][]
with open('passby_data.csv', 'r') as f :
reader = csv.DictReader(f)
i =j= 0
for row in reader :
a[i].append(row)
j = j + 1
if(j % 24 == 0) : # If you read 24 data, move on to the next day of the week
i = i + 1
# -------------------------------------------------------------------------
day_title = ['MON', 'TUE', 'WED', 'THR', 'FRI', 'SAT', 'SUN'] # the title of the day
hour_title = ['01', '02', '03', '04', '05', '06', \
'07', '08','09', '10', '11', '12', \
'13', '14','15', '16', '17', '18', \
'19', '20','21', '22', '23', '24',]
# Obtain weekly averages by time zone
avgh = []
for j in range(0, 24) : # Repeat 19 to 24 lines for 0 to 23 hours
day_sum = 0 # Initialize to 0 to get the sum by time zone
# Jth Time Zone Weekly Sum
for i in range (0, 7) : # Repeat seven times a week
day_sum = day_sum + int(a[i][j]['num']) # Accumulate the number of rows by time zone j on the i-th day of the week
avgh.append(day_sum/7) # Find the average number of weekly rowers in the j-th time zone
# To output the average floating population by time zone
for j in range (0, 24) : # repeat 24 times
print("[~{0}:00]: {0:4}". format(hour_title[j], int(avgh[j]))) # Output of floating population by time zone
and this is what i got this time.
[~01:00]: 01
[~02:00]: 02
[~03:00]: 03
[~04:00]: 04
[~05:00]: 05
[~06:00]: 06
[~07:00]: 07
[~08:00]: 08
[~09:00]: 09
[~10:00]: 10
[~11:00]: 11
[~12:00]: 12
[~13:00]: 13
[~14:00]: 14
[~15:00]: 15
[~16:00]: 16
[~17:00]: 17
[~18:00]: 18
[~19:00]: 19
[~20:00]: 20
[~21:00]: 21
[~22:00]: 22
[~23:00]: 23
[~24:00]: 24
Process finished with exit code 0
why they're using {1:4}? not {0:4}. python starts with 0. not 1. and even more, those answer are different({0:4} and {1:4}) why they are different? and why {0:4} is even shorter than {1:4}? it's not look like there are four spaces in {0:4} answer.) thanks for watching this long long explanation haha.. i hope you understand what i wanted to say,,, Have a nice day and I really appreciate for your answering.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
