gpt4 book ai didi

python - python中的多个集合操作

转载 作者:太空狗 更新时间:2023-10-30 02:52:38 25 4
gpt4 key购买 nike

我是 python 的新手,现在正在学习它。我在网上练习,遇到了以下问题。然而,我试图解决它,尽管我得到了预期的结果,但在线验证器说它是错误的。请指出我哪里出错了。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

In a school, there are total 20 students numbered from 1 to 20. You’re given three lists named ‘C’, ‘F’, and ‘H’, representing students who play cricket, football, and hockey, respectively. Based on this information, find out and print the following:

  • Students who play all the three sports
  • Students who play both cricket and football but don’t play hockey
  • Students who play exactly two of the sports
  • Students who don’t play any of the three sports

Format:

Input:

3 lists containing numbers (ranging from 1 to 20) representing students who play cricket, football and hockey respectively.

输出:

4 different lists containing the students according to the constraints provided in the questions.

例子:输入:

[[2, 5, 9, 12, 13, 15, 16, 17, 18, 19]
[2, 4, 5, 6, 7, 9, 13, 16]
[1, 2, 5, 9, 10, 11, 12, 13, 15]]

预期输出:

[2, 5, 9, 13]
[16]
[12, 15, 16]
[3, 8, 14, 20]

下面是我的代码

C = set(input_list[0])
F = set(input_list[1])
H = set(input_list[2])
A= set(range(1, 21))

print(sorted(list(C & F & H)))
print(sorted(list((C & F) - H)))
print(sorted(list(((C-F)&H | (C-H)&F))))
print(sorted(list(A-(C|F|H))))

我不确定是否真的需要 A。

谢谢,

最佳答案

除了恰好参加两项运动的学生之外,你对所有人都是正确的,这应该是:

(C|F|H) - (C^F^H)

关于python - python中的多个集合操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52594352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com