DataStructure Python

1.Count the occurrence of each element from a list

sample_list = [11, 45, 8, 11, 23, 45, 23, 45, 89]

  1. Find the intersection (common) of two sets and remove those elements from the first set

first_set = {23, 42, 65, 57, 78, 83, 29}
second_set = {57, 83, 29, 67, 73, 43, 48}

1 Like

1)sample_list = [11, 45, 8, 11, 23, 45, 23, 45, 89]
print(“Original list:\n”,sample_list)

print("11 occurrence: ",sample_list.count(11))
print("45 occurrence: ",sample_list.count(45))
print("23 occurrence: ",sample_list.count(23))

2)first_set = {23, 42, 65, 57, 78, 83, 29}
second_set = {57, 83, 29, 67, 73, 43, 48}
for i in first_set and second_set:
first_set.remove(i)
print(“first_set:”,first_set);
print(“second_set:”,second_set);

1 Like

Set_1 = {23, 42, 65, 57, 78, 83, 29}
Set_2 = {57, 83, 29, 67, 73, 43, 48}

print(“Original sets:”)
print(Set_1)
print(Set_2)

print(“\nRemove the intersection”)
for i in Set_1 & Set_2:
Set_1.remove(i)

print("set1: ", Set_1)
print("set2: ", Set_2)

1 Like
first_set = {23, 42, 65, 57, 78, 83, 29}
second_set = {57, 83, 29, 67, 73, 43, 48}
res =first_set.intersection(second_set)
for i in res:
    first_set.remove(i)
print(first_set) 

``
1 Like