1.代码
#元祖的遍历
for d in dimensions:
print(d)

#集合set是不重复元素的无顺序集合。

#这是一个空集合:
fruit = set() #不能用 fruit +{} 构造一个空集合,为什么?
#这个集合有几个元素?
fruit={‘apple’,‘orange’,‘apple’,‘pear’,‘orange’,‘bannana’}
print(‘set fruit contains’+ str(len(fruit))+‘elements.’)

#像集合添加元素:
fruit.add(‘watermelon’)
print(fruit)
#删除元素:
fruit.remove(‘apple’)
print(fruit)

#遍历集合:
fruit = {‘apple’,‘orange’,‘apple’,‘pear’,‘orange’,‘bannana’}
for x in fruit:
print(x)

#判断某个元素是否在集合中:
b1 = 'apple’in fruit #False
b2 = 'orange’in fruit #True
b3 = 'banana’not in fruit #False
print(b1,b2,b3)

#注:in和not in也可以用在列表、元祖和字典中:
3 in[1,2,3]#True
3 in (1,2,3)#True
'a’in {‘a’:80,‘b’:70,‘C’:60}#True

2.输入代码
Python-09
Python-09
3.输出结果
Python-09

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-16
  • 2021-11-07
  • 2021-12-07
  • 2022-02-21
  • 2021-04-04
猜你喜欢
  • 2021-09-21
  • 2021-10-10
  • 2022-12-23
  • 2021-11-27
  • 2021-08-01
  • 2021-07-01
  • 2022-01-06
相关资源
相似解决方案