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

课程 9. Sets

难度:

任务«对称差»

💻 Python
两位朋友,爱丽丝和鲍里斯,正在为派对做准备。他们每个人都列出了一份需要购买的物品清单。你的任务是确定哪些物品只需要其中一人购买(也就是说,这些物品只出现在一位朋友的清单中,而另一位朋友的清单中没有)。每位朋友将自行购买这些物品。

输入格式

首先输入整数 n ——爱丽丝列表中商品的数量。

然后输入 n 行 ——爱丽丝列表中的商品名称。
接着输入整数 m ——鲍里斯列表中商品的数量。
然后输入 m 行 ——鲍里斯列表中的商品名称。

输出格式

找出仅出现在其中一个列表中的产品名称(即仅出现在两个列表之一中的产品)。产品应按字母顺序排序,并以空格分隔。

示例

输入

3
苹果
香蕉
橙子
2
香蕉
猕猴桃

输出

橙子 苹果 猕猴桃

提示

Sets in python

Sets in Python are unordered collections of unique elements. They are a powerful tool for working with data when the speed of checking whether an item belongs to a collection and the absence of duplicates is important. They have a number of useful properties and methods that make it possible to efficiently perform mathematical operations on data, such as union, intersection, difference, and others.

Main characteristics of sets

  • Unique elements: There can be no duplicate elements in the set. If you try to add an element that already exists, the array simply won't change. This is their key feature, ideal for removing duplicates.
  • Disorder: The elements of the set do not have a specific order. This means that you cannot access an element by index (for example, my_set[0]), as is done in lists. When iterating over a set, the order of the elements is not guaranteed.
  • Changeability: You can add and remove multiple items. However, the elements of the set themselves must be immutable (hashed) objects. These can be numbers, strings, tuples. You cannot add mutable objects to a set, such as lists or other sets, because their contents may change, which violates the internal logic of sets.

Creating sets

Empty set

To create an empty set , be sure to use the set() function, because the syntax {} creates an empty dictionary. This is a common mistake of novice programmers.

# Correct
my_set = set()

# Wrong (this will create a dictionary)
my_dict = {} 

A set with elements

You can create a set by listing the elements in curly brackets or by passing an iterable object (such as a list) to the set() function..

my_set = {1, 2, 3, 4, 5}

A useful tip: Using the set() functionThis is the easiest way to remove duplicates from the list.

my_list = [1, 2, 2, 3, 4, 3, 5, 5]
unique_elements = set(my_list)
print(unique_elements)  # Output: {1, 2, 3, 4, 5}

Basic operations with sets

Adding elements

To add one element, use the add() method.

my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}

# Trying to add an existing element will not change
anything my_set.add(2)
print(my_set) # Output: {1, 2, 3, 4}

Deleting elements

There are three ways to delete:

  • remove(element): Deletes the specified element. If the element is not found, it causes the KeyError error. Use this method when you are sure that an element should exist, and its absence is an error in the program logic.
  • discard(element): Deletes the specified element, but does not cause an error if the element is not found. This is a safe way to delete when you are not sure if an item is in the set.
  • pop(): deletes and returns an arbitrary element of the set. Since the sets are unordered, you cannot predict which element will be deleted. The method will cause a KeyError if the set is empty.
my_set = {1, 2, 3}
my_set.remove(2)
print(my_set) # Output: {1, 3}

# my_set.remove(4) # This will cause a KeyError error

my_set.discard(4) # There will be no error, the set will remain {1, 3}

popped_element = my_set.pop()
print(popped_element) # Output: 1 or 3 (depends on implementation)
print(my_set) # Output: remaining element

Clearing the set

The clear() method removes all elements from the set, making it empty.

my_set = {1, 2, 3}
my_set.clear()
print(my_set) # Output: set()

Operations on sets

These operations do not change the original sets, but create and return a new one.

Union

Returns a new set containing all the unique elements from both sets. The | operator or the union() method is used. When to use: When you need to collect all the unique items from multiple collections into one.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2
# or
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5}

Intersection

Returns a new set containing only the elements that are in both sets. The & operator or the intersection() method is used. When to use: When you need to find common elements between two groups.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1 & set2
# or
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {3}

Difference

Returns a new set containing the elements that are in the first set but are missing from the second. The - operator or the difference() method is used. When to use: When you need to find what is in one group but not in another. The order is important: set1 - set2 is not the same as set2 - set1.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1 - set2
print(difference_set) # Output: {1, 2}

difference_set_2 = set2 - set1
print(difference_set_2) # Output: {4, 5}

Symmetrical Difference

Returns a new set containing elements that are in one of the sets, but not both at once. The ^ operator or the symmetric_difference() method is used. When to use: When you need to find elements unique to each group, excluding the common ones.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1 ^ set2
# or
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set) # Output: {1, 2, 4, 5}
main.py
测试 1
测试 2
测试 3
测试 4
测试 5
测试 6
测试 7
测试 8
测试 9
测试 10
开发者解答

🎉 恭喜! 🎉

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

AD

广告

red-snake blue-snake green-snake

正在运行代码...

AI助手

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