Python 如何使用我的程序将年龄分组?

Python 如何使用我的程序将年龄分组?,python,function,Python,Function,我制作了一个程序,可以将最年轻和最年长的人写在一个.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 Wil

我制作了一个程序,可以将最年轻和最年长的人写在一个.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
George 5
David 20

0-6
George 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]))
一些伪代码:

要将人员附加到子组,请执行以下操作:

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)

最简单的方法是使用dict分组并检查分数的范围:

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.
从集合导入订单数据
导入csv
#创建以分数范围为键的dict
d=订单数据(((((“0-6”)、((“7-15”)、(([])、((“16-18”)、((“19-30”)、[]))
以open(“match.txt”)作为f,open(“grouped.txt”、“w”)作为out:
wr,read=csv.writer(out,delimiter=“”),csv.reader(f,delimiter=“”)
mn,mx=float(“inf”),float(“inf”)
最高,最低=无,无
对于读取中的行:
名称,分数=行
分数=整数(分数)
如果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')
def parse_info(文件名):
最大年龄=0
max_name=“”
最小年龄=浮动('inf')
最小名称=“”
清单0_6=[]
列表(共7个,共15个)=[]
清单(共16个)30=[]
打开(文件名为“r”)作为信息:
lines=info.read().splitlines()
对于行中的行:
name,age=line.split()
年龄=int(年龄)
如果年龄<最小年龄:
最小年龄=年龄
min_name=name
如果年龄>最大年龄:
最大年龄=年龄
max_name=name
如果年龄<7岁:
第0行第6行的列表。追加(第行)
elif年龄<16岁:
第7行和第15行的列表。追加(第行)
其他:
第16行和第30行的列表。追加(第行)
返回[min_name、str(min_age)、max_name、str(max_age)、list_of_0_6、list_of_7_15、list_of_16_30]
def writefile(数据、文件名):
打开(文件名为“w”)作为输出文件:
outputfile.write(数据[0]+''+数据[1]+'\n')
outputfile.write(数据[2]+''+数据[3]+'\n')
outputfile.write('\n0-6\n')
对于数据[4]中的输入:
outputfile.write(条目+'\n')
outputfile.write('\n7-15\n')
对于数据[5]中的输入:
outputfile.write(条目+'\n')
outputfile.write('\n16-30\n')
对于数据[6]中的输入:
outputfile.write(条目+'\n')
my_data=parse_info('info.txt'))
writefile(我的_数据'output.txt')

如果组是互斥的,具有一定的顺序,并且您知道如何以propper格式加载文件,以便python理解,那么如何根据年龄进行排序,然后在第一次出现新组时进行拆分


就性能而言,这将是
n*log(n)
,而不是大多数其他答案提供的
n*number\u组
解决方案。

我认为这是一个好方法,首先对列表进行排序,然后使用一些帮助函数来计算特定年龄段的人

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]))
def read_info():
“”“读取信息文件”“”
以open(“info.txt”、“r”)作为f:
姓名,年龄=[],[]
对于f.readlines()中的行:
n、 a=行.strip().split(“”)
name.append(n)
年龄.附加(int(a))
返回姓名、年龄
按年龄(姓名、年龄)定义排序信息:
“”“按年龄对数组排序,使最年轻的成员排在第一位”“”
排序的id=sorted(范围(len(age)),key=lambda k:float(age[k]))
new_name=[name[i]表示排序的_id中的i]
new_age=[排序的_ID中i的年龄[i]
返回新名称、新年龄
def获取年龄间隔的年龄ID(开始年龄、结束年龄、年龄):
“”“获取给定年龄间隔内成员的索引”“”
ids=[]
对于i,枚举中的年龄(年龄):
如果(开始时间
def解析信息(文件路径):
人员列表=[]
组={“0-6”:[],“7-15”:[],“19-30”:[]}
打开(文件路径“r”)作为信息:
对于行输入信息:
人员列表.append({“name”:line.split(“”[0],“age”:line.split(“”[1]))
人员列表=已排序(人员列表,键=lambda k:k['age'])
最年轻的=人员列表[0]
最老的=人名单[-1]
对于人员列表中的人员:

如果0,这是完全错误的。如果你有一个
log(n)
排序算法,你应该与全世界分享它。二次解在哪里?@padraiccningham,我想你会告诉我为什么?你知道排序是(n logn),我的解是0(n)是的,这是一个打字错误。顺便说一句,你的是
n*numberofu组。
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")