jeapedu 127 集合的習題1

链接: https://pan.baidu.com/s/13xwfMjtKxEK6m8OP27tE8A 密码: v9ea

# 1 all the list elements in other list
# 2 find common key in two dict
# 3 find different item in two tuple
# 4 count char times in string
# 5 combine two lists, duplicate
# 6 find same value's key
# 7 delete elements of list b in list a

li1 = list(range(1, 10))
li2 = list(range(1, 20))
lis1 = set(li1)
lis2 = set(li2)
print(lis1, lis2)
t = lis2.issuperset(lis1)
print(t)
s = lis1.issubset(lis2)
print(s)

d1 = dict(zip(map(chr, range(97, 123)), range(97, 123)))
print(d1)
d2 = dict(zip(map(chr, range(103, 111)), range(103, 111)))
print(d2)
ds1, ds2 = set(d1), set(d2)
print(ds1, ds2)
print(ds1.intersection(ds2))

t1 = ('a', 13, 'ddd', 48)
t2 = ('b', 48, 99)
ts1, ts2 = set(t1), set(t2)
print(ts1, ts2)
print(ts1.difference(ts2).union(ts2.difference(ts1)))


s1 = "aaabbcccc"
ss1 = set(s1)
print(ss1)
sd1 = {}
for x in ss1:
    sd1[x] = list(s1).count(x)
print(sd1)


li1 = list(range(5, 10))
li2 = list(range(7, 15))
lis1, lis2 = set(li1), set(li2)
lic = list(lis1.union(lis2))
print(li1, li2, lic)

d1 = {'a' : 11, 'b' : 12, 'c' : 13, 'd' : 11}
s1 = set(d1.values())
d2 = {}
for x in s1: 
    pass

li1 = list(range(15, 20))
li2 = list(range(18, 25))
lis1, lis2 = set(li1), set(li2)
print(list(lis1.difference(lis2)))

# 1 all the list elements in other list
# a is b's subset
li1 = list(range(1, 20))
print(li1)
li2 = list(range(3, 9, 2))
print(li2)
s1 = set(li1)
s2 = set(li2)
t = s2.intersection(s1)
print('t', t)
if len(t) == len(s2):
    print('li2 all in li1')
def isin(a, b):
    c = 0
    for x in a:
        if x in b:
            c += 1
    if c == len(a):
        print('in')
isin(li2, li1)
阅读更多

没有更多推荐了,返回首页