Data Structure: List
Data Structure: List
A data structure has zero or more elements with some relationship. We have
already studied the concept of a list.
Let us look at a few programs illustrating the usefulness and the power of the
set data structure.
# name : 0_intro_set.py
# set :
# is a data structure
# has # of elements
# elements are unique
# make a set : { }
a = { 10, 30, 10, 40, 20, 50, 30 }
print(a) # elements are unique; there is no particular order
# list : l
# ith element : l[i]
# next element : l[i + 1]
# previous element : l[i - 1]
# is a sequence
# concept of position for each element
# set :
# not sequence
# no concept of an element in a particular position
# represents a finite set of math
# set in an iterable
for e in a :
print(e, end = " ")
print()
# set operations
s1 = {1, 2, 3, 4, 5}
s2 = {1, 3, 5, 7, 9}
# union
print(s1 | s2) # {1, 2, 3, 4, 5, 7, 9}
# intersection
print(s1 & s2) # {1, 3, 5}
# set diference
print(s1 - s2) # {2, 4}
# symmetric diference
print(s1 ^ s2) # {2, 4, 7, 9}
#print("what : ", s1[0])# TypeError: 'set' object does not support indexing
s5 = set("mississippi")
print(s5) # {'s', 'i', 'm', 'p'}
This is same as the earlier case – but the elements of the set are sorted
into a list by using the function sorted.
print(sorted(set(str2.split())))
# name : 2_operations_set.py
# create sets with the required elements
"""
# not good; it works
# version 1
s = set()
for e in range(2, n + 1):
s.add(e)
print(s, type(s))
"""
# version 2
s = set(range(2, n + 1))
print(s)
#-------------------------------------
if len(s2) == 0 :
print("empty")
else:
print("non empty")
"""
if s1 :
print("empty")
else:
print("non empty")
if s2 :
print("empty")
else:
print("non empty")
# empty data structure => False
# non-empty data structure => True
s3 = set(range(10)) # 0 .. 9
s3 = s3 - set(range(2, 10, 2))
print(s3)
# name: 3_sieve.py
# generate prime numbers (no division; most efcient algorithm)
# sieve of Eratosthenes
# get a number(say n)
# make a set of numbers from 2 to n - say sieve
# while sieve is not empty
# find the smallest (small)
# print it (that is a prime)
# remove small and its multiples from the sieve
Let us solve some problems and choose the right data structures.
The input to all these problems is a string having # of lines. Each line has a
language name, a writer’s name and his work.
We can split the string based on new line and split each of these lines based on
white space and capture whatever is required.
lang_book_count = {}
for line in all.split('\n'):
lang = line.split()[0]
if lang not in lang_book_count :
lang_book_count[lang] = 0
lang_book_count[lang] += 1
# name: 5_dict.py
# name : 6_dict.py
# name: 7_dict.py
all = """sanskrit kalidasa shakuntala
english r_k_narayan malgudi_days
kannada kuvempu ramayanadarshanam
sanskrit bhasa swapnavasavadatta
kannada kuvempu malegalalli_madumagalu
english r_k_narayan dateless_diary
kannada karanta chomanadudi
sanskrit baana harshacharita
kannada karanta sarasatammana_Samadhi
sanskrit kalidasa malavikagnimitra
sanskrit kalidasa raghuvamsha
sanskrit baana kadambari
sanskrit bhasa pratijnayogandhararayana"""
# name: 8_dict.py
all = """sanskrit kalidasa shakuntala
english r_k_narayan malgudi_days
kannada kuvempu ramayanadarshanam
sanskrit bhasa swapnavasavadatta
kannada kuvempu malegalalli_madumagalu
english r_k_narayan dateless_diary
kannada karanta chomanadudi
sanskrit baana harshacharita
kannada karanta sarasatammana_Samadhi
sanskrit kalidasa malavikagnimitra
sanskrit kalidasa raghuvamsha
sanskrit baana kadambari
sanskrit bhasa pratijnayogandhararayana"""
info = {}
for line in all.split('\n'):
(lang, author, title) = line.split()
if lang not in info :
info[lang] = {}
if author not in info[lang] :
info[lang][author] = []
info[lang][author].append(title)