Container: list, ancestor, dictionary, collection

set

Introduction

A set is an unordered set. It is a collection of a set of keys and does not store values. In a set, duplicate keys are not allowed. Sets can be used to remove duplicate values. Sets can also perform mathematical set operations, such as union, intersection, difference, and symmetric difference. • Application: de-duplication. Turning a list into a set automatically removes duplicates: set (list name) relationship test. Test the intersection, difference, union and other relationships before the two sets of data

Definition method:

Use the set([]) function or use curly braces {} to note that to create an empty set, you must use set(), not {}, because {} means to create an empty dictionary

	#第一种方式
 m = {
    
    1,2,3,4,5,6,7,8,9,"abcdefg","bdefgeh"}
  # 第二种方式
 n = set([1,2,3,4,5,6,7,8,9,"abcdefg","bdefgeh"])

De-duplication


    list1 = [1,2,3,4,5,6,2,3,4,5,6,7,"a","b","c"]
    y = set(list1)
    x = list(y)
    print(x)

Calculation

  a = {
    
    1,2,3,4,5,6}
    b = {
    
    4,5,6,7,8,9}
    print(a-b)   # 集合差集
    print(a|b)   # 集合并集
    print(a&b)   # 集合交集
    print(a^b)   # 集合的对称差

dictionary

Iterating over the dictionary

  • The first method: XXX.key()
# 该方法会返回一个序列,序列中保存有字典的所有的键
dictionary = {
    
    "at":"857324d764f743da95b34be3c79ee1c8","rt":"ee424e06495242268f9918268bdbafb2"}

print(dictionary)
print(dictionary.keys())
for key in dictionary.keys():
    print(key,dictionary[key])
  • The second method: xxx.values()
dictionary = {
    
    "at":"857324d764f743da95b34be3c79ee1c8","rt":"ee424e06495242268f9918268bdbafb2"}

print(dictionary)
# 该方法返回一个序列,序列中保存有字典的所有的值,
# 该方法只能遍历字典中所有的值,不能遍历键
print(dictionary.values())
for val in dictionary.values():
    print(val)
  • The third method: xxx.items():
dictionary = {
    
    "at":"857324d764f743da95b34be3c79ee1c8","rt":"ee424e06495242268f9918268bdbafb2"}

print(dictionary)
#  xxx.items() : 返回字典中所有的key = values 返回一个序列,序列中包含有双值子序列
print(dictionary.items())
for key, value in dictionary.items():
    print(key,"=", value)

Guess you like

Origin blog.csdn.net/Mwyldnje2003/article/details/112916162