Mittwoch, 9. Oktober 2013
Musterlösung zu Wiederholung Python (POS1: 2BHIF)
Eine mögliche Lösung zur Aufgabe Wiederholung Python (POS1: 2BHIF) ist folgende:
"""
text_analyzer.py
Task 1: Repeat previous material (1st class)
2012-09-10, Harald R. Haberstroh (hp@htlwrn.ac.at)
"""
def input_sentences():
'''returns al list of sentences/lines'''
lst = []
eof = False
while not eof:
try:
line = input("Satz: ")
lst.append(line)
except EOFError:
eof = True
return lst
def _remove_punctuation(string):
'''helper removes all punctuation marks within string'''
for punctuation in '''"',;.:-''':
string = string.replace(punctuation, " ")
return string
def split_sentences(lst):
'''split a list (lst) of sentences into list of list of words'''
wordlist = []
for sentence in lst:
wordlist.append(_remove_punctuation(sentence).split())
return wordlist
def purge_bad_words(lst):
'''remove all bad words (length less than 2 chars, words
containing non alphabetic chars)
'''
for sublist in lst:
idx = 0
while idx < len(sublist):
if not len(sublist[idx]) >= 2 or not sublist[idx].isalpha():
del(sublist[idx])
else:
idx += 1
return lst
def analyze_words(lst):
'''returns a dictionary of words (with the number of occurrences)
of the list of list of words'''
dict = {}
for sublist in lst:
for word in sublist:
word = word.lower()
if word in dict:
dict[word] += 1
else:
dict[word] = 1
return dict
def analyze_letters(lst):
'''returns a dictionary of letters (with the number of occurrences)
of the list of list of words'''
dict = {}
for sublist in lst:
for word in sublist:
for char in word.lower():
if char in dict:
dict[char] += 1
else:
dict[char] = 1
return dict
def purge_analyzed_letters(dic):
'''remove umlauts'''
for umlaut in "äöüß":
if umlaut in dic:
del(dic[umlaut])
return dic
def _key(tupl):
'''returns 2nd element of tuple for sorting'''
return tupl[1]
def sort_letters(dic):
'''returns a sorted list of tuples of letters, sorted by
number of letters'''
tuples = list(dic.items())
tuples.sort(key=_key, reverse=True)
return tuples
def test_input():
'''returns a fixed list of sentences for testing purposes'''
return ['Maxi und Mini verliefen sich im Wald, aber bald.',
'10 alte Fledermäuse flogen im Wald',
'Hat die alte Meisterhexe...',
"Seid's gewesen. Denn als Geister...",
'Erst hervor der alte Meister.']
if __name__ == '__main__':
#lst = input_sentences()
lst = test_input()
print('split_sentences(lst)')
wordlist = split_sentences(lst)
print(wordlist)
print('\npurge_bad_words(wordlist)')
wordlist = purge_bad_words(wordlist)
print(wordlist)
print('\nanalyze_words(wordlist)')
print(analyze_words(wordlist))
print('\nanalyze_letters(wordlist)')
dic = analyze_letters(wordlist)
print(dic)
print('\npurge_analyzed_letters(dic)')
dic = purge_analyzed_letters(dic)
print(dic)
print('\nsort_letters(dic)')
sorted_letters = sort_letters(dic)
print(sorted_letters)
print('\n\n*** task 9 ***\n')
print('''"abcdef".index("c")''') # ValueError: substring not found
print('''ValueError: substring not found''')
print('''"abcdef".index("g")''') # ValueError: substring not found
print('''ValueError: substring not found''')
print('''"abcdef".index("de")''')
print("abcdef".index("de"))
print('''"c" in "abcdef" ''')
print("c" in "abcdef")
print('''"g" in "abcdef" ''')
print("g" in "abcdef")
print('''"de" in "abcdef" ''')
print("de" in "abcdef")
print('''"abcdef"[2:4]''')
print("abcdef"[2:4])
print('''"abcdef"[2:]''')
print("abcdef"[2:])
print('''"abcdef"[-1]''')
print("abcdef"[-1])
print('''"abcdef"[2:-1]''')
print("abcdef"[2:-1])
print('''[1, 2, 3, 4, 5].index(3)''')
print([1, 2, 3, 4, 5].index(3))
print('''[1, 2, 3, 4, 5].index(9)''') # ValueError: 9 is not in list
print('''ValueError: 9 is not in list''')
print('''[1, 2, 3, 4, 5][5]''') # IndexError: list index out of range
print('''IndexError: list index out of range''')
print('''[1, 2, 3, 4, 5][2:4]''')
print([1, 2, 3, 4, 5][2:4])
print('''[1, 2, 3, 4, 5][-2]''')
print([1, 2, 3, 4, 5][-2])
print('''[1, 2, 3, 4, 5][5:9]''')
print([1, 2, 3, 4, 5][5:9])
print('''[1, 2, 3, 4, 5][4:]''')
print([1, 2, 3, 4, 5][4:])
print('''len("abc") + len(range(3)) + len({1, 2, 3}) + len({1: 2, 2: 3})''')
print(len("abc") + len(range(3)) + len({1, 2, 3}) + len({1: 2, 2: 3}))
Labels: Lösung, POS1-2, Python
Abonnieren Kommentare [Atom]
Kommentar veröffentlichen