list列表操作

li = [1,2,3,4,'4','456']
print(li.index('4'))
# print(li.index('45'))
l_a = ['1234567','7890']
l_b = [6,7,8]
li.append(l_b)
print(li)
li.extend(l_a)
print(li)

  结果

4
[1, 2, 3, 4, '4', '456', [6, 7, 8]]
[1, 2, 3, 4, '4', '456', [6, 7, 8], '1234567', '7890']

s_a = set([1, 2, 2, 3, 4, 5, 6])
s_b = set([4, 5, 6, 7, 8, 9])
print(s_a)
print(s_b)

# 判断元素是否存在
print(5 in s_a)
print(10 in s_b)

# 并集
print(s_a | s_b)
print(s_a.union(s_b))

# 交集
print(s_a & s_b)
print(s_a.intersection(s_b))

# 差集 A - (A & B)
print(s_a - s_b)
print(s_a.difference(s_b))

# 对称差 (A | B) - (A & B)
print(s_a ^ s_b)
print(s_a.symmetric_difference(s_b))

# 修改元素
s_a.add('x')
s_a.update([4, 5, 60, 70])
print(s_a)
s_a.remove(70)
print(s_a)
# s_a.remove(100)

print(len(s_a))
for i in s_a:
print(i)

 

posted @ 2018-01-27 18:37  IOSyes  阅读(191)  评论(0编辑  收藏  举报