python的集合的创建和访问

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sehanlingfeng/article/details/91174858

a = {'a', 'b', 'c'}             # 这是一个集合,集合是只有key的字典

print(type(a))                  # <class 'set'> a的类型是一个集合

b = {
    'a': 1,
    'b': 2,
    'c': 3
}

t = 'c' in a                    # t是bool型,该语句的意思是在集合a中寻找'c'
print(t)                        # True 找到'c'

x = 'a' in b
print(x)

l = [1, 2, 3, 2, 4, 5, 2]

s1 = set(l)
print(s1)                       # {1, 2, 3, 4, 5} s1是一个集合
print(list(s1))                 # [1, 2, 3, 4, 5] 把集合s1编程列表
print(s1, type(s1), list(s1))   # {1, 2, 3, 4, 5} <class 'set'> [1, 2, 3, 4, 5]

猜你喜欢

转载自blog.csdn.net/sehanlingfeng/article/details/91174858