我制作了一个程序,可以在.txt列表中编写最年轻和最年长的人。
现在我需要让程序将人和年龄分成特定的年龄组。
[0-6], [7-15], [16-18], [19-30], [31-50], [51-)
例如,列表将是:
Stacy 11
David 20
George 5
Wiliam 15
Annie 8
Christina 10
程序应该打开一个新的.txt文件,如下所示:
0-6
Georgie 5
7-15
Annie 8
Christina 10
Stacy 11
Wiliam 15
19-30
David 20
等。我想你们都明白我的意思。 现在我的程序打印出最年轻和最旧的列表。
这是:
def parse_info():
info = open("info.txt", "r")
max_age = 0
max_name = ''
min_age = float('inf')
min_name = ''
for line in info:
m_list = line.split(" ")
if int(m_list[1]) > max_age:
max_age = int(m_list[1])
max_name = m_list[0]
elif int(m_list[1]) < min_age:
min_age = int(m_list[1])
min_name = m_list[0]
info.close()
return ((min_name,min_age),(max_name,max_age))
#end of function
nameAge=parse_info()
f = open("output.txt","w")
f.write(nameAge[0][0]+" "+str(nameAge[0][1])+"\n")
f.write(nameAge[1][0]+" "+str(nameAge[1][1]))
如何让程序首先打印出最年轻和最老的列表,然后将所有人分类到上面给定的年龄组?
程序应该像这样写入.txt文件:
George 5
David 20
0-6
George 5
7-15
Annie 8
Christina 10
Stacy 11
Wiliam 15
19-30
David 20
答案 0 :(得分:1)
最简单的方法是使用字典进行分组并检查分数的范围:
from collections import OrderedDict
import csv
# create dict with score ranges as keys
d = OrderedDict((("0-6", []), ("7-15", []), ("16-18", []), ("19-30", [])))
with open("match.txt") as f, open("grouped.txt", "w") as out:
wr, read = csv.writer(out,delimiter=" "), csv.reader(f,delimiter=" ")
mn, mx = float("inf"), float("-inf")
highest, lowest = None, None
for row in read:
name, score = row
score = int(score)
if 0 <= score < 7:
d["0-6"].append((name, score))
elif 7 <= score < 16:
d["7-15"].append((name, score))
elif 16 <= score < 19:
d["16-18"].append((name, score))
elif 19 <= score <= 30:
d["19-30"].append((name, score))
if score < mn:
mn = score
lowest = (name, score)
if score > mx:
mx = score
highest = (name, score)
for k, v in d.items():
wr.writerow([k])
wr.writerows(v)
print(d)
{'16-18': [], '0-6': [('George', 5)], '19-30': [('David', 20)], '7-15': [('Stacy', 11), ('Wiliam', 15), ('Annie', 8), ('Christina', 10)]}
print("Highest scorer was {} with a score of {}.".format(*highest))
print("Lowest scorer was {} with a score of {}.".format(*lowest))
Highest scorer was David with a score of 20.
Lowest scorer was George with a score of 5.
OrderedDict将保持顺序,所以我们只需要遍历编写key
的dict项目,这是组范围和元组列表,它们都是属于该组的名称和分数。范围。将delimiter
设置为" "
的csv.reader将为我们进行拆分,因此我们只需要将得分转换为int并检查它所属的范围。
答案 1 :(得分:0)
一些伪代码:
将人员附加到子组:
a_list = []
创建一个组
>>> a=range(19,31)
>>> a
[19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
for name, age in people:
if age in int(a):
a_list.append(name)
答案 2 :(得分:0)
只是我的愚蠢尝试:
def parse_info(filename):
max_age = 0
max_name = ''
min_age = float('inf')
min_name = ''
list_of_0_6 = []
list_of_7_15 = []
list_of_16_30 = []
with open(filename, 'r') as info:
lines = info.read().splitlines()
for line in lines:
name, age = line.split()
age = int(age)
if age < min_age:
min_age = age
min_name = name
if age > max_age:
max_age = age
max_name = name
if age < 7:
list_of_0_6.append(line)
elif age < 16:
list_of_7_15.append(line)
else:
list_of_16_30.append(line)
return [min_name, str(min_age), max_name, str(max_age), list_of_0_6, list_of_7_15, list_of_16_30]
def writefile(data, filename):
with open(filename, 'w') as outputfile:
outputfile.write(data[0] + ' ' + data[1] + '\n')
outputfile.write(data[2] + ' ' + data[3] + '\n')
outputfile.write('\n0-6\n')
for entry in data[4]:
outputfile.write(entry + '\n')
outputfile.write('\n7-15\n')
for entry in data[5]:
outputfile.write(entry + '\n')
outputfile.write('\n16-30\n')
for entry in data[6]:
outputfile.write(entry + '\n')
my_data = parse_info('info.txt')
writefile(my_data, 'output.txt')
答案 3 :(得分:0)
如果这些组是互斥的,具有一定的特定性,并且您知道如何以用于理解python的propper格式加载文件,如何对年龄进行排序,然后拆分新组的第一次出现?< / p>
就性能而言,这将是n*log(n)
,而不是大多数其他答案提供的n*number_of_groups
解决方案。
答案 4 :(得分:0)
我认为这是首先对列表进行排序的好方法,而不是使用一些辅助函数来计算某个年龄段的人员。
def read_info():
"""Read info file"""
with open("info.txt", "r") as f:
name, age = [], []
for line in f.readlines():
n, a = line.strip().split(' ')
name.append(n)
age.append(int(a))
return name, age
def sort_info_by_age(name, age):
"""Sort arrays by age so that youngest member is at first place"""
sorted_ids = sorted(range(len(age)), key=lambda k: float(age[k]))
new_name = [name[i] for i in sorted_ids ]
new_age = [age[i] for i in sorted_ids ]
return new_name, new_age
def get_ids_for_age_intervall(start_age, end_age, ages):
"""get indexes for members in a given age intervall"""
ids = []
for i, age in enumerate(ages):
if (start_age<=age) and (age<=end_age):
ids.append(i)
return ids
name, age = read_info()
name, age = sort_info_by_age(name, age)
groups = [(0,6), (7,15), (16, 18), (19, 30), (31, 50)]
with open("output.txt", "w") as f:
#output younges and oldest member
f.write("%s %i\n" % (name[0], age[0]))
f.write("%s %i\n" % (name[-1], age[-1]))
#go trough age groups and write to file
for start_age, end_age in groups:
ids = get_ids_for_age_intervall(start_age, end_age, age)
if ids:
f.write("\n%i-%i\n" % (start_age, end_age))
for i in ids:
f.write("%s %i\n" % (name[i], age[i]))
答案 5 :(得分:-1)
def parse_info(filepath):
people_list = []
groups = {"0-6": [], "7-15": [], "19-30": []}
with open(filepath, 'r') as info:
for line in info:
people_list.append({"name": line.split(" ")[0], "age": line.split(" ")[1]})
people_list = sorted(people_list, key=lambda k: k['age'])
youngest = people_list[0]
oldest = people_list[-1]
for person in people_list:
if 0 <= person['age'] < 7:
groups['0-6'].append(person)
elif 7 <= person['age'] < 16:
groups['7-15'].append(person)
elif 19 <= person['age'] <= 30:
groups['19-30'].append(person)
return youngest, oldest, groups
youngest, oldest, groups = parse_info('info.txt')
with open("output.txt", "w") as output:
output.write(youngest["name"] + " " + each["age"] + "\n")
output.write(oldest["name"] + " " + each["age"] + "\n")
output.write("\n")
for key, value in groups.iteritems():
output.write(key + "\n")
for each in value:
output.write(each["name"] + " " + each["age"] + "\n")
output.write("\n")