根据条件将两个列表合并为第三个 [英] combining two list into third based on condition

查看:97
本文介绍了根据条件将两个列表合并为第三个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是python 3代码, 有2个输入文件courses.txt和student.txt,我能够从一个列表中的courses.txt和另一个列表中的学生信息中读取课程信息. 现在,我要创建第三个列表,在该列表中应将特定学期的学生注册到该课程.

This is python 3 code, there are 2 input files courses.txt and student.txt I am able to read the course information from courses.txt in a list and student information in another list. Now I want to create a third list where a student of a particular semester should be registered to the course.

#course registration
list_courses=[]
with open("courses.txt",'r') as myfile1:
    for line in myfile1:
        list_courses.append(line.strip().split(','))


list_student=[]
with open("students.txt",'r') as myfile1:
    for line in myfile1:
        list_student.append(line.strip().split(','))

list_final=[]
for index,student in enumerate(list_student):
     semester=student[0]
     roll_number=(student[1])
     print (("semester=%s roll_number=%s")%(semester,roll_number))
     if roll_number.startswith('cs') :
        for index2,course in enumerate (list_courses):
            if course[0].startswith('cs'):
               list_final=list_student[index]+list_courses[index2]

print (list_final)

courses.txt文件是

the files courses.txt is

cs101,C programming
cs102,Digital logic and design
cs103,Electrical engineering
cs231,IT networks
cs232,IT Workshop
cs233,IT programming
cs301,Compilers and automata
cs302,Operating Systems
cs303,Networks
cs401,Game Theory
cs402,Systems Programming
cs403,Automata
ec101,Digitization
ec102,Analog cicuit design
ec103,IP Telephony
ec201,Wireless Network
ec202,Microwave engineering
ec203,Antenna
ec301,Maths2
ec302,Theory of Circuits
ec303,PCB design
ec401,PLC programming
ec402,Scada
ec403,VLSI

带有数字的课程表示该课程的学期,即cs101表示计算机科学专业的学生,​​其101中的前1表示学期1, 如果cs202表示cs的第二学期课程.

the courses with numbers signify the semester of course i.e. cs101 means computer science student the first 1 in 101 means semester 1, if it cs202 means a second semester course for cs.

students.txt文件如下

The students.txt file is as follows

4,cs2313,Someone 
7,ec3325,Someone 
7,cs4890,Someone 
1,ec1008,Someone 
3,cs2002,Someone 
3,cs2009,Someone 

数字4,7代表学期,行中的第二个元素代表卷号,第三个元素代表学生的姓名,现在我想要一个新列表,其中基于学期,我已将课程添加到学生信息中,即以下记录输入list3 7,ec3325,Someone,ec301,ec302,ec303

The numbers 4,7 represent semester,second element in line represent roll number and third element represent name of student, now I want a new list in which based on semester I have added the course to student info i.e some record of following type in list3 7,ec3325,Someone ,ec301,ec302,ec303

我无法理解以下部分中的更改

I am not able to understand what to change in following part

for index,course in enumerate(list_courses):
     print(index,course[0])
     if(course[0].statswith(cs1))

以使list3 = list.append(list2 append(以cs1或所需的开头的课程)值)

so that list3=list.append(list2 append(courses that begin with cs1 or desired ))value)

输出为

['3', 'cs2009', 'someone', 'cs403', 'Automata']

输出应该是

['3', 'cs2009', 'someone', 'cs301','Compilers and automata'
'cs302','Operating Systems','cs303,'Networks']

请注意,由于列表中的第一个条目是3(学期),因此课程从cs3xx开始

Note the courses begin with cs3xx since the first entry in list is 3 (semester)

我在哪里犯错?

推荐答案

根据您的代码进行一些调整:

Based on your code just some tweaks:

#course registration
list_courses=[]
with open("courses.txt",'r') as myfile1:
    for line in myfile1:
        list_courses.append(line.strip().split(','))


list_student=[]
with open("students.txt",'r') as myfile1:
    for line in myfile1:
        list_student.append(line.strip().split(','))

list_final=[]
for semester, roll_num, name in list_student:
    faculty = roll_num[:2]
    temp_result = [semester, roll_num, name]
    course_lvl = faculty + semester
    for course_num, course_name in list_courses:
        if course_num.startswith(course_lvl):
           temp_result.extend([course_num, course_name])

    # omitting the students with no courses (comment if that's not what you want)
    if len(temp_result) > 3:           
        list_final.append(temp_result)

    # including students with no courses (uncomment if that's what you want)
    # list_final.append(temp_result)

print (list_final)

在我更好地理解了OP问题之后:

from math import ceil

list_final=[]
    for semester, roll_num, name in list_student:
        faculty = roll_num[:2]
        temp_result = [semester, roll_num, name]
        course_lvl = faculty + str(ceil(int(semester) / 2))
        for course_num, course_name in list_courses:
            if course_num.startswith(course_lvl):
               temp_result.extend([course_num, course_name])

        list_final.append(temp_result)

    print (list_final)

这篇关于根据条件将两个列表合并为第三个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆