פייתון/פייתון גרסה 3/מילון/תרגול

s1 = """Callimachus was a native of the Greek colony of Cyrene, Libya.
He was a noted poet, critic and scholar at the Library of Alexandria and enjoyed the patronage of the Egyptian–Greek Pharaohs Ptolemy II Philadelphus and Ptolemy III Euergetes.

s2=Although he was never made chief librarian, he was responsible for producing a bibliographic survey based upon the contents of the Library.
This, his Pinakes, 120 volumes long, provided the foundation for later work on the history of ancient Greek literature.
He is among the most productive and influential scholar-poets of the Hellenistic age."""
  1. צרו מילון שמונה את מספר ההופעות של אות בכל אחד מרצפי המחרוזות לעיל.
  2. צרו מילון שמונה המופיעים של מילה ברצף.
  3. השוו בין שני המיליונים ומצאו איזה מפתחות זהים.

מודל לפתרון סעיף 1 עריכה

d = {}  

for letter in s:
    if letter not in d:
        #creat new key:
        d[letter] = 1
        # append to value frequency appear of a letter in one 
    else:
        d[letter] = d[letter]+1
  • שמו לב שהפתרון הוא עבור מחרוזת של אותיות ששווה למשתנה s ולא s1 או s2

מודל לפתרון סעיף 2 עריכה

s = """Callimachus was a native of the Greek colony of Cyrene, Libya.
He was a noted poet, critic and scholar at the Library of Alexandria and enjoyed the patronage of the Egyptian–Greek Pharaohs Ptolemy II Philadelphus and Ptolemy III Euergetes.
Although he was never made chief librarian, he was responsible for producing a bibliographic survey based upon the contents of the Library.
This, his Pinakes, 120 volumes long, provided the foundation for later work on the history of ancient Greek literature.
He is among the most productive and influential scholar-poets of the Hellenistic age."""

#split s:
s2=s.split()

#creat a dictory:
d = {}  

for word in s2:
    if word not in d:
        #creat new key:
        d[word] = 1
        # append to value frequency appear of a letter in one 
    else:
        d[word] = d[word]+1
print(d)

פתרון עריכה

s1 = """Callimachus was a native of the Greek colony of Cyrene, Libya.
He was a noted poet, critic and scholar at the Library of Alexandria and enjoyed the patronage of the Egyptian–Greek Pharaohs Ptolemy II Philadelphus and Ptolemy III Euergetes."""

s2="""Although he was never made chief librarian, he was responsible for producing a bibliographic survey based upon the contents of the Library.
This, his Pinakes, 120 volumes long, provided the foundation for later work on the history of ancient Greek literature.
He is among the most productive and influential scholar-poets of the Hellenistic age."""

#split s:
s_split1=s1.split()
s_split2=s2.split()

#creat a dictory for d1:
d1 = {}  

for word in s_split1:
    if word not in d1:
        #creat new key:
        d1[word] = 1
        # append to value frequency appear of a letter in one 
    else:
        d1[word] = d1[word]+1

#creat a dictory for s2:  
d2 = {}

for word in s_split2:
    if word not in d2:
        #creat new key:
        d2[word] = 1
        # append to value frequency appear of a letter in one 
    else:
        d2[word] = d2[word]+1

שמו לב בכדי להשוואות בין המילונים אין לבצע את הפעולה:

#compare between two list:
##incorrect :
d_new=[]
for (d1_keys, d2_keys) in zip (d1, d2):
    if d1_keys == d2_keys:
        d_new.append()

print(d_new)
>>>[]

מפני שפיתון תשווה בין המפתח הראשון במילון אחד למפתח שני. יתכן מאוד שהמפתחות האלו לא יהיו זהים ולכן נקבל חיתוך ריק. מצד שני יתכן מאוד כי קיים מפתח אחר, במיקום אחר שזהה למפתח הראשון ברשימה הראשונה.

על כן בכדי להשוואות בין המילונים חייבים להשתמש בקבוצות:

#correct:

seq_d1=set(d1.keys())
seq_d2=set(d2.keys())
print(seq_d2 & seq_d2)

>>>{'later', 'age.', 'producing', 'scholar-poets', 'chief', 'among', 'Library.', 'He', 'he', 'Pinakes,', 'volumes', 'librarian,', 'survey', '120', 'history', 'ancient', 'of', 'was', 'work', 'his', 'never', 'long,', 'This,', 'upon', 'based', 'the', 'responsible', 'most', 'made', 'for', 'foundation', 'on', 'literature.', 'is', 'and', 'productive', 'Hellenistic', 'bibliographic', 'Greek', 'contents', 'influential', 'a', 'provided', 'Although'}