📜  在Python中设置 add()

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

在Python中设置 add()

如果元素不存在于集合中,则 set add() 方法将给定元素添加到集合中。

句法:

set.add(elem)
The add() method doesn't add an element to the
set if it's already present in it otherwise it 
will get added to the set.
Parameters:
add() takes single parameter(elem) which needs to 
be added in the set.
Returns:
The add() method doesn't return any value.
# set of letters
GEEK = {'g', 'e', 'k'}
  
# adding 's'
GEEK.add('s')
print('Letters are:', GEEK)
  
# adding 's' again
GEEK.add('s')
print('Letters are:', GEEK)

输出:

('Letters are:', set(['k', 'e', 's', 'g']))
('Letters are:', set(['k', 'e', 's', 'g'])

应用:

It is used to add a new element to the set. 
# set of letters
GEEK = {6, 0, 4}
  
# adding 1
GEEK.add(1)
print('Letters are:', GEEK)
  
# adding 0 
GEEK.add(0)
print('Letters are:', GEEK)

输出:

('Letters are:', set([0, 1, 4, 6]))
('Letters are:', set([0, 1, 4, 6]))


将元组添加到集合中:

# Python code to demonstrate addition of tuple to a set.
s = {'g', 'e', 'e', 'k', 's'}
t = ('f', 'o')
  
# adding tuple t to set s.
s.add(t)
  
print(s)

输出 :

{'k', 's', 'e', 'g', ('f', 'o')}