def avg_temp_march(f):
"""(file open for reading) -> float
Return the average temperature for the month of March
over all years in f.
"""
file = open(f, 'r')
file.readline()
file.readline()
file.readline()
lines = file.read().strip().split('\n')
file.close
for line in lines:
month = line.split(' ')[0:]
march = month[2]
Run Code Online (Sandbox Code Playgroud)
该表是:
['24.7', '25.7', '30.6', '47.5', '62.9', '68.5', '73.7', '67.9', '61.1', '48.5', '39.6', '20.0']
['16.1', '19.1', '24.2', '45.4', '61.3', '66.5', '72.1', '68.4', '60.2', '50.9', '37.4', '31.1']
['10.4', '21.6', '37.4', '44.7', '53.2', '68.0', '73.7', '68.2', '60.7', '50.2', …Run Code Online (Sandbox Code Playgroud) 我有一份清单
['r', 'o', 'c', 'o', 'c', 'o']`
Run Code Online (Sandbox Code Playgroud)
并希望成功
[['r','o'], ['c', 'o'], ['c', 'o']]
Run Code Online (Sandbox Code Playgroud)
我该怎么做?此外,我需要将新列表分组为上面示例中的"n",如果n为3则n为2,结果应为:[['r','o','c'] [' O", 'C', 'O']]
def get_key(file):
'''(file open for reading) -> tuple of objects
Return a tuple containing an int of the group length and a dictionary of
mapping pairs.
'''
f = open(file, 'r')
dic = f.read().strip().split()
group_length = dic[0]
dic[0] = 'grouplen' + group_length
tup = {}
tup['grouplen'] = group_length
idx = 1
dic2 = dic
del dic2[0]
print(dic2)
for item in dic2:
tup[item[0]] = item[1]
print(tup)
return tup
Run Code Online (Sandbox Code Playgroud)
结果是:{'grouplen': '2', '"': 'w'}
dic 2是:
['"w', '#a', '$(', '%}', '&+', "'m", …Run Code Online (Sandbox Code Playgroud)