Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在学习在线课程。有这个任务。两组 T 和 S。在 S 上写一个推导式语句,其值为 S 和 T 的交集。在推导式末尾的过滤器中使用成员资格测试。我正在考虑{如果 x 是 T,则在 S 中为 x 在 T 中的东西}。
单循环(集合理解)就足够了。
>>> S = {1, 2, 3} >>> T = {4, 3, 5, 1} >>> {x for x in S if x in T} set([1, 3])