06 Tuple, Set, Dictionary

image.png լուսանկարի հղումը, Հեղինակ՝ Naira Babayan

Open In Colab (ToDo)

Set the Controls for the Heart of the Sun
(c) Pink Floyd

🎦 Տեսադասեր + լրացուցիչ

ToDo 1. Տեսություն 2025
2. Տեսություն 2023 (ToDo)
3. Գործնական 2025
4. Գործնական 2023 (ToDo)
5. Որոշ տնայինների լուծումներ (ToDo)

Google Forms ToDo

📚 Նյութը

Tuple-ներ

Ըստ էության tupleը նույն listնա, որին որ չի կարելի փոփոխել (immutable են)

tuple-ի ստեղծում

# դատարկ tuple ստեղծել
tup = ()
# կամ
tup = tuple()

print(tup)
# էլեմենտներով tuple ստեղծել
tup = (1, "ողջույն", True)
tup = 1, "ողջույն", True


print(tup)
# եթե լիստ ունենք կարանք դարձնենք իրան tuple, երևի գուշակում եք թե ոնց՝
lst = [1, "ողջույն", True]
tup = tuple(lst)
print(tup)
# եթե ուզում ենք tupleի էլեմենտները բաժանենք տարբեր փոփոխականների մեջ, կարանք անենք՝
print(tup)

a, b = 15, 509

a, b, c = tup
print(a, b, c, sep="\n")
tuple(i**2 for i in range(10))




# եթե զուտ (i**2 for i in range(10)) գրենք կսարքենք generator կոչվող բան, որը հետո ենք անցնելու

Tupleին էլեմենտ ավելացնել

tup.append(1)
"asdas"[0] = "A"
# tup.append("պանիր")

tup = (15, 508)
print(tup[0])

tup[0] = 12
print(tup)
  • immutable - string, tuple
  • mutable - list
tup[0] = 0 #  TypeError

ճարներս ինչ, պետքա նոր tuple սարքենք

new_tuple = tup + ("ժարիտ", 509)
print(new_tuple)

indexing / slicing (նույն ձև ոնց listը)

print(new_tuple[1]) # prints: "ողջույն"

print(new_tuple[1:4]) # prints: ('ողջույն', True, 'ժարիտ')

print(new_tuple[-1]) # Output: 509

ցիկլ պտտվելը (լրիվ նույն ձև)

for element in new_tuple:
    print(element)
max((1,2,3))
tuple(i**2 for i in range(10))

Առավելություններ

  1. tuple-ների հետ ավելի ապահովա, որովհետև կարանք վստահ լինենք որ ոչ մեկ մեր tuple-ը չի փոխի
  2. հիշողության տեսանկյունից ավելի օպտիմալ են

set-եր (բազմություններ)

my_set = set() # my_set = {}

print(type(my_set))

# my_set = {}
# print(type(my_set))
my_set = {4, 3, 3, 2}
print(my_set)
my_set = set()

for _ in range(5):
    my_set.add(input()) # append-ի փոխարեն add
    print(my_set)
for i in my_set:
    print(i)
my_set1 = {}
print(type(my_set1)) # զգույշ ենք լինում

էլմենտ ավելացնել / հեռացնել

my_set = {4, 3, 3, 2}
print(my_set)

print(my_set)

# my_set.append()
my_set.add(5) # ոչ թե գրում ենք my_set = my_set.add(4)
print(my_set)

my_set.remove(3)
print(my_set)
print(my_set.pop())
print(my_set)
for i in my_set:
    print(i)

Բազմությունների հետ գործողություններ

image.png

image.png
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

# Միավորում
print(set1.union(set2)) # Output: {1, 2, 3, 4, 5, 6}
# կամ էլ
# set1 | set2 (կամ) # caps lock + right shift-ի կողքը 6

# Հատում
print(set1.intersection(set2)) # Output: {3, 4}
# կամ էլ
# set1 & set2 (և) # uppercase 6

# Տարբերություն
print(set1.difference(set2)) # Output: {1, 2}
# կամ էլ
# print(set1 - set2)

# Սիմետրիկ տարբերություն
print(set1.symmetric_difference(set2)) # Output: {1, 2, 5, 6}
# կամ էլ
# set1 ^ set2 # uppercase 6

Set comparison

a = {1, 2, 3, "a", "b"}
b = {2, 1, 3, "b"}

print(a >= b)

# >, >=, <, <=

Հերթականություն

forum-ում քննարկում set-երի հերթականության վերաբերյալ

a = ['պանիր', 'փիղ', 11, 12]
print(a)
print(set(a))
set(a)[0]
print(a)
# print(set(a)[0])

print()
print(list(set(a)))
print(list(set(a))[0])
my_set = {1, 4, 4, 3, 2}

for element in my_set:
    print(element)

set comprehension

-5, 6 թվերի քառակուսիների բազմությունը ստեղծել

lst = []

for i in range(-5, 6):
    lst.append(i**2)
print(lst)

lst = [i**2 for i in range(-5,6)]
print(lst)
s = set() # s = {}

for i in range(-5, 6):
    s.add(i**2)
    # print(s)

print(s)
[] - list
() - tuple
{} - set
s = [i**2 for i in range(-5,6)]
print(s)

s = tuple(i**2 for i in range(-5,6))
print(s)

s = {i**2 for i in range(-5,6)}
print(s)

Կարանք հանգիստ in, sum len ․․ օգտագործենք

print(s)

print(f"{4 in s = }")
print(f"{len(s) = }")
print(f"{sum(s) = }")

Dictionary (բառարան)

Json

anun = ["aik", "bik"]
hamarner = ["+1", "+2"]

contacts = [["aik", "+1"], ["bik", "+2"]]

Alice-ը՝ 25 տարեկան ա, Bob-ը՝ 30, Charlie-ն 35: Python-ով պահեք էդ ինֆորմացիան

Ներածություն

d = {}
d = dict()

print(type(d))
tariqner = {"Alice": 25, "Bob": 30,
            "Charlie": 35}

print(type(tariqner))
print(tariqner)
print(tariqner["Alice"]) # [0]
# Նոր մարդ ավելացնել
tariqner["Dumbledore"] = 40
print(tariqner)
# փոփոխել արժեքը
tariqner["Dumbledore"] += 1
print(tariqner)
# ջնջել մարդու
del tariqner["Charlie"] # delete
print(tariqner)
print(tariqner.keys())
print(tariqner.values())
print(tariqner.keys()[0])
print(list(tariqner.keys()))

print(list(tariqner.keys())[0])
tariqner[1] = "12312"
print(tariqner)
tariqner[False] = "Barevner"

print(tariqner)
tariqner[False]
tariqner[3.14] = "pi"
tariqner[[1,2]] = 12312
for mard in tariqner: # կամ tariqner.keys()
    print(mard)
for t in tariqner.values():
    print(t)
print(tariqner.items())
for i in tariqner.items():
    print(i, type(i))
    
for i in tariqner.items():
    name, age = i # i[0], i[1]
    print(name, age)
for name, age in tariqner.items():
    print(name, age)

Methods

print(len(tariqner))

key, value, items

print(tariqner.keys())
print(list(tariqner.keys()))
print(tariqner.values())
print(list(tariqner.values()))
print(tariqner.items())
print(list(tariqner.items()))

Արժեք ստանալ (get)

tariqner['ժուլիեն']
tariqner.get('Alice')
# գրում ենք բանալին, ու թե ինչ պետք ա վերադարձնի եթե չկա էդ բանալին
print(tariqner.get('ժուլիեն'))
print(tariqner.get('ժուլիեն', "Չկա"))
print(tariqner.get('Alice', "Չկա"))

update

print(tariqner)
urish_dict = {"John": 32, "Smith": 32, "Alice": 27}

tariqner.update(urish_dict)
print(tariqner)

Dict comprehension

{i for i in range(-5,6)}
{i:i**2 for i in range(-5,6)}
{key:value for ...}

Nested (ներդրված) dictionaries

contacts = {
    "Alice": {"phone": "123-456-7890", "email": "alice@gmail.com"},
    "Bob": {"phone": "098-78-88-78", "address": "Bobastan"}
}
print(contacts.keys())
print(contacts['Bob'].keys())
print(contacts["Bob"]["phone"])
contacts['Bob']['email'] = "123123@..$"
print(contacts['Bob'])

🛠️ Գործնական (ToDo)

Հաշվել որ մրգից քանի հատ կա

mrger = ["apple", "banana", "apple", "orange", "banana", "apple"]
mrger_set = set(mrger)

print(mrger_set, len(mrger_set))
count_dict = {}

for i in mrger_set:
    count_dict[i] = mrger.count(i)    
        
{i:mrger.count(i) for i in mrger_set}
count_dict = {}

for mirg in mrger_set:
    count = 0
    for j in mrger:
        if mirg == j:
            count += 1 
    count_dict[mirg] = count 
    
count_dict   
count_dict = {}

# if len(mrger) == len(set(mrger)):
#     print({i:1 for i in mrger_set})

for mirg in mrger:
    print(mirg, count_dict)

    if mirg not in count_dict:
        count_dict[mirg] = 1
    else:
        count_dict[mirg] += 1
    
    
count_dict   

count_dict = {i:0 for i in mrger_set}

for mirg in mrger:
    count_dict[mirg] += 1
count_dict = {}
print(count_dict)

for mirg in mrger:
    if mirg not in count_dict:
        count_dict[mirg] = 0
    
    count_dict[mirg] += 1   
    print(count_dict)

Telegram

Settings -> Advanced -> (Scroll down) Export Telegram data -> (Scroll down) Choose “Machine readable JSON” -> Export

import json 

PATH = "result.json"

with open(PATH, "r") as f:
    data = json.load(f)
chats = data["chats"]["list"]

chats[0]["type"]
'public_supergroup'
types = [i["type"] for i in chats]

count_dict = {i:0 for i in types}

for i in types:
    count_dict[i] += 1
    
count_dict
{'public_supergroup': 4, 'public_channel': 23}
count_has_message = 0
chats_not_empty = []

for chat in chats:
    if chat["messages"]:
        count_has_message += 1 
        chats_not_empty.append(chat["name"])

print(f"Perc - {count_has_message / len(chats) * 100:.2f}")
print(chats_not_empty)
Perc - 18.52
['Չատերի պահով, երևացող նամակներ', 'Մետրիկ Դասընթաց', 'Հայախոս մաթեմատիկայի համայնք', 'ՄաթԹիմ և ԻԿՄ ֆակուլտետի ՈՒԳԸ', 'դըբդֆհֆտվդվ']
chats_list_not_empty = [chat for chat in chats if chat["messages"]]
msg_counts = {}

for chat in chats_list_not_empty:
    count = 0
    
    msgs = chat["messages"]
    for msg in msgs:
        if msg["type"] == "message":
            count += 1
    
    msg_counts[chat["name"]] = count
                
print(msg_counts)
        # if chat["name"][""]
        # print(*chat["messages"][10:15], sep="\n")
{'Չատերի պահով, երևացող նամակներ': 5, 'Մետրիկ Դասընթաց': 133, 'Հայախոս մաթեմատիկայի համայնք': 12, 'ՄաթԹիմ և ԻԿՄ ֆակուլտետի ՈՒԳԸ': 13, 'դըբդֆհֆտվդվ': 18}

🏡Տնային

Հիմնական տնային

  1. Profound բաժին 24 (tuple-ներ և set-եր) - լրիվ
  2. Profound բաժին 25 (dictionary) - լրիվ

Լուծումներ

🎲 6

Flag Counter