标签:Set Python update set set1 set2 method
Python update() function in set adds elements from a set (passed as an argument) to the set.
Syntax : set1.update(set2)
Here set1 is the set in which set2 will be added.
Parameters : Update() method takes only a single argument. The single argument can be a set, list, tuples or a dictionary. It automatically converts into a set and adds to the set.
Return value : This method adds set2 to set1 and returns nothing.
Example of Python set update()
Example 1: Working with Python set update list
# Python program to demonstrate the
# use of update() method
list1 = [1, 2, 3]
list2 = [5, 6, 7]
list3 = [10, 11, 12]
# Lists converted to sets
set1 = set(list2)
set2 = set(list1)
# Update method
set1.update(set2)
# Print the updated set
print(set1)
# List is passed as an parameter which
# gets automatically converted to a set
set1.update(list3)
print(set1)
输出:
{1, 2, 3, 5, 6, 7}
{1, 2, 3, 5, 6, 7, 10, 11, 12}
Example 2: Python set update element in set
# Python program to demonstrate the
# use of update() method
list1 = [1, 2, 3, 4]
list2 = [1, 4, 2, 3, 5]
alphabet_set = {'a', 'b', 'c'}
# lists converted to sets
set1 = set(list2)
set2 = set(list1)
# Update method
set1.update(set2)
# Print the updated set
print(set1)
set1.update(alphabet_set)
print(set1)
输出:
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 'c', 'b', 'a'}
Example 3: Add elements of the dictionary to Set
number = {1, 2, 3, 4, 5}
num_Dict = {6: 'Six', 7: 'Seven', 8: 'Eight',
9: 'Nine', 10: 'Ten'}
number.update(num_Dict)
print("Updated set: ", number)
输出:
Updated set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
转载:https://www.geeksforgeeks.org/python-set-update/
标签:Set,Python,update,set,set1,set2,method 来源: https://www.cnblogs.com/whiteBear/p/16533762.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。