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

课程 9. Sets

难度:

任务«数据组合»

💻 Python
你计划举办一个派对,并希望制定最终的购物清单。你已经有一个初步清单,但一些客人建议添加他们最喜欢的饮料和零食。编写一个程序,通过排除重复项来帮助你形成最终的购物清单。

输入格式

初步采购清单,以空格分隔。 客人提供的报价清单,也以空格分隔。

输出格式

按字母顺序排序的最终购物清单,其中每个元素单独显示在一行上

示例

输入

果汁 水 薯片 饼干
水 牛排 薯片 水果

输出


可乐
饼干
果汁
水果
薯片

提示

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 的问题——我可以解释函数、方法,并帮助你完成当前任务!