python different_Python使用示例设置different_update()方法

设置difference_update()方法 (Set difference_update() Method)

difference_update() method is used to update the set with the elements of this (original) set (set1) by removing the elements from another set (set2) that do not exist in the set1 and repeated elements.

difference_update()方法用于通过从另一个集合(set2)中删除set1中不存在的元素和重复的元素来使用此(原始)集合(set1)的元素更新集合。

Syntax:

句法:

set_name1.difference_update(set_name2)

Parameter(s):

参数:

set_name2 – Name of the another/second set to find the difference with set_name1. set_name2 –另一个/第二个集合的名称,用于查找与set_name1的差异。

Return value:

返回值:

The return type of this method is , it returns nothing.

此方法的返回类型为 ,它什么也不返回。

Example 1:

范例1:

# Python Set difference_update() Method with Example

# declaring the sets

cars_1 = {"Porsche", "Audi", "Lexus"}

cars_2 = {"Porsche", "Mazda", "Lincoln"}

# printing the sets before difference_update() call

print("cars_1:", cars_1)

print("cars_2:", cars_2)

# difference_update() method call

cars_1.difference_update(cars_2)

# printing the sets after difference_update() call

print("cars_1:", cars_1)

print("cars_2:", cars_2)

Output

输出量

cars_1: {'Audi', 'Lexus', 'Porsche'}

cars_2: {'Lincoln', 'Mazda', 'Porsche'}

cars_1: {'Audi', 'Lexus'}

cars_2: {'Lincoln', 'Mazda', 'Porsche'}

See the output, cars_1 is updated with the unique elements by removing other elements that exist in cars_2 and repeated element "Porsche".

看到输出,cars_1与独特的元素通过除去存在于cars_2和重复的元素“保时捷”的元件更新。

Example 2:

范例2:

# Python Set difference_update() Method with Example

# declaring the sets

x = {"ABC", "PQR", "XYZ"}

y = {"ABC", "PQR", "XYZ"}

z = {"DEF", "MNO", "XYZ"}

# printing the results

print("Before difference_update()...")

print("x:", x)

print("y:", y)

print("z:", z)

# printing the differences

x.difference_update(y)

y.difference_update(z)

z.difference_update(x)

# printing the results

print("After difference_update()...")

print("x:", x)

print("y:", y)

print("z:", z)

Output

输出量

Before difference_update()...

x: {'ABC', 'XYZ', 'PQR'}

y: {'ABC', 'XYZ', 'PQR'}

z: {'MNO', 'XYZ', 'DEF'}

After difference_update()...

x: set()

y: {'ABC', 'PQR'}

z: {'MNO', 'XYZ', 'DEF'}

翻译自: https://www.includehelp.com/python/set-difference_update-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值