📜  在Python中设置 clear()

📅  最后修改于: 2022-05-13 01:55:07.277000             🧑  作者: Mango

在Python中设置 clear()

clear() 方法从集合中删除所有元素。

句法:

set.clear()
parameters:
The clear() method doesn't take any parameters.
Return:
The clear() method doesn't return any value.
# set of letters
GEEK = {'g', 'e', 'e', 'k', 's'}
print('GEEK before clear:', GEEK)
  
# clearing vowels
GEEK.clear()
print('GEEK after clear:', GEEK)

输出:

GEEK before clear: set(['s', 'e', 'k', 'g'])
GEEK after clear: set([])

应用:

It is used to clear set.
# set of letters
GEEK = {6, 0, 4, 1}
print('GEEK before clear:', GEEK)
  
# clearing vowels
GEEK.clear()
print('GEEK after clear:', GEEK) 

输出:

GEEK before clear: set([0, 1, 4, 6])
GEEK after clear: set([])