• 1
    数据输入输出
    • 任务
  • 2
    条件语句
    • 任务
  • 3
    for循环
    • 任务
  • 4
    字符串
    • 任务
  • 5
    while循环
    • 任务
  • 6
    列表
    • 任务
  • 7
    二维数组
    • 任务
  • 8
    字典
    • 任务
  • 9
    集合
    • 任务
  • 10
    函数与递归
    • 任务
  • к

课程 9. Sets

难度:

任务«过滤兴趣相同的朋友列表»

💻 Python
假设你想组织一次朋友聚会,但你有一份共同兴趣列表,并且只想邀请那些至少拥有其中一项兴趣的朋友。你得到了一个朋友姓名列表以及许多共同兴趣。编写一个程序,创建一个新列表,其中仅包含那些兴趣与共同兴趣有交集的朋友姓名。

输入格式

首先介绍一个由逗号分隔的朋友列表。然后介绍许多共同的兴趣,由逗号分隔。

输出格式

程序应显示朋友的朋友的排序列表,用逗号分隔,这些人的兴趣与共同兴趣有交集。

示例

输入

安娜,鲍里斯,维克多,加林娜,德米特里
音乐,电影,体育
安娜:音乐,舞蹈
鲍里斯:电影,游戏
维克多:体育,书籍
加林娜:绘画,唱歌
德米特里:电影,旅行

输出

安娜、鲍里斯、维克多、德米特里

提示

Comparing sets

Subset

A set A is a subset of set B if all elements of set A, without exception, are also contained in set B. The issubset() method is used to check this condition. and the operator .

A useful tip: The <= operator checks for a "non-strict" subset. This means that any set is a subset of itself (set1<= set1 returns True). If you need to check that the set A is a subset of B, but it is not equal to it, use the strict inequality operator .

set1 = {1, 2}
set2 = {1, 2, 3}
set3 = {1, 2}

# Checking for a subset
print(set1.issubset(set2))  # Output: True
print(set1 <= set2) # Output: True

# Checking for a strict subset
print(set1 < set2) # Output: True
print(set3< set1) # Output: False, since the sets are equal

Superset

A set A is a superset of set B if it contains all the elements that are in set B. The issuperset() method is used for verification. and the operator =.

A useful tip: Similarly to subsets, the = operator checks for a "non-strict" superset. To check for a strict superset (A contains all the elements of B and at least one additional one), use the operator.

set1 = {1, 2, 3}
set2 = {1, 2}

# Checking for a superset
print(set1.issuperset(set2))  # Output: True
print(set1 >= set2) # Output: True

# Checking for a strict superset
print(set1 > set2) # Output: True
print(set1 > {1, 2, 3}) # Conclusion: False, since the sets are equal

Equality of sets

Sets are considered equal if they contain exactly the same set of elements. The order of the elements does not matter at all. The == operator is used for verification.

set1 = {1, 2, 3}
set2 = {3, 2, 1}
set3 = {1, 2, 3, 3} # The duplicate will be ignored when creating a set.

print(set1 == set2) # Output: True
print(set1 == set3) # Output: True

Other set methods

Copying a set

The copy() method returns a surface copy of the set.

Important advice: Do not confuse copying with assignment. If you write set2 = set1, then set2 will not be a new set, but only another reference to set1. Changing set2 will change set1. Use copy() to create an independent array.

set1 = {1, 2, 3}

# Correct copying
set2 = set1.copy()
set2.add(4)
print(f"Original set1: {set1}")  # Output: Original set1: {1, 2, 3}
print(f"Copy of set2: {set2}")    # Output: A copy of set2: {1, 2, 3, 4}

# Incorrect copying (link assignment)
set3 = set1
set3.add(5)
print(f"Original set1 after changing set3: {set1}") # Output: Original set1 after changing set3: {1, 2, 3, 5}
print(f"Reference set3: {set3}")                       # Output: set3 link: {1, 2, 3, 5}

Updating the set

The update() method adds to the current set all the elements from another set or any other iterable object (for example, a list or a tuple). The method modifies the original set and returns nothing (None).

set1 = {1, 2, 3}
set2 = {3, 4, 5}
list1 = [5, 6, 7]

set1.update(set2) # Duplicate {3} will be ignored
print(set1) # Output: {1, 2, 3, 4, 5}

set1.update(list1) # You can update it from the list
print(set1) # Output: {1, 2, 3, 4, 5, 6, 7}

Sets and their applications

Arrays are extremely useful in various tasks due to their speed and unique properties. They are ideal for removing duplicates, quickly checking for an element, and performing mathematical operations (union, intersection, difference).

Removing duplicates from the list

This is one of the most common uses of arrays. Converting a list to a set automatically removes all duplicates. Then, if necessary, the set can be converted back to a list.

It is important to remember: With this transformation, the original order of the list items is not preserved.

numbers = [1, 2, 2, 3, 4, 4, 5, 1]
unique_numbers = list(set(numbers))
print(unique_numbers) # The output can be [1, 2, 3, 4, 5] ( the order is not guaranteed)

Search for common items in two lists

To find the elements that are present in both collections, you can transform them into sets and use the intersection operator &.

list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
common_elements = list(set(list1) & set(list2))
print(common_elements)  # Output: [3, 4] (order is not guaranteed)

Search for unique elements (symmetric difference)

To find the elements that are in one of the lists, but not both at once, use the symmetric difference operator ^.

list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
unique_to_each_list = list(set(list1) ^ set(list2))
print(unique_to_each_list) # Output: [1, 2, 5, 6] ( the order is not guaranteed)
main.py
测试 1
测试 2
测试 3
测试 4
测试 5
测试 6
测试 7
测试 8
测试 9
测试 10
开发者解答

🎉 恭喜! 🎉

你出色地完成了任务!这是一个不小的挑战,但你找到了正确答案。你离编程大师又近了一步!继续保持,每一步都让你更强大。

AD

广告

red-snake blue-snake green-snake

正在运行代码...

AI助手

你好!我是你的编程助手。随时向我提问关于 Python 的问题——我可以解释函数、方法,并帮助你完成当前任务!