python_work2

1.1

class1 = ['John', 'Bill', 'Niki', 'Mark', 'Mark']
class2 = ['Tom','Linda','Bill']

for name1 in class1:
    for name2 in class2:
        if name1 == name2:
            print(name1)

运行结果:

Bill

1.2

num1 = int(input('input tne number of student in class1:'))
# 添加班级人数

class1 = set()
# 初始化集合class1

for i in range(0, num1):
    name = input('input student %d name:' % (i + 1))
    class1.add(name)
# 遍历填入班级里学生的名字

num2 = int(input('input the number of student in class2:'))

class2 = set()

for i in range(0, num2):
    name = input('input student %d name:' % (i + 1))
    class2.add(name)

    same = class1 & class2
    # 找出相同姓名

    print("duplication of name:")
    for name in same:
        print(name)
# 遍历打印同姓的学生

运行结果

input tne number of student in class1:3
input student 1 name:bill
input student 2 name:ali
input student 3 name:dear
input the number of student in class2:2
input student 1 name:bill
duplication of name:
bill
input student 2 name:lili
duplication of name:
bill

1.3

num1 = int(input('input tne number of student in class1:'))
# 添加班级人数

class1 = set()
# 初始化集合class1

for i in range(0, num1):
    name = input('input student %d name:' % (i + 1))
    class1.add(name)
# 遍历填入班级里学生的名字

num2 = int(input('input the number of student in class2:'))

class2 = set()

for i in range(0, num2):
    name = input('input student %d name:' % (i + 1))
    class2.add(name)

    Only2 = class2 - class1
    # 找出只在class2中出现的姓名

    print("only2 of name:")
    for name in Only2:
        print(name)
# 遍历打印只在class2中出现的学生

运行结果

input tne number of student in class1:2
input student 1 name:lili
input student 2 name:ber
input the number of student in class2:2
input student 1 name:son
only2 of name:
son
input student 2 name:lili
only2 of name:
son

你可能感兴趣的:(python)