def sortByWordCount(ls: list[str]) -> None:
"""
Sort ls in place by word count.
"""
def wordCount(x: str) -> int:
"""
Return the number of words in the string x.
"""
#help(x.split)
print(x.split())
return len(x.split())
ls.sort(key=wordCount)
x = ["a lot of short words", "few magniloquent words"]
sortByWordCount(x)
print(x)
['a', 'lot', 'of', 'short', 'words'] ['few', 'magniloquent', 'words'] ['few magniloquent words', 'a lot of short words']
lst = [12]
s = "Hello"
#help(sortByWordCount)
#help(lst.reverse)
#help(lst.index)
#print(12 in lst)
#print(lst.index(12))
#help(s.join)
#print("".join(["a", "b", "c"]))
#help(s.split)
#help(s.find)
#print(s.find("lo"))
help(lst.index)
Help on built-in function index:
index(value, start=0, stop=9223372036854775807, /) method of builtins.list instance
Return first index of value.
Raises ValueError if the value is not present.
info = {
"surname": "Richards",
#"surname": "Kenneth",
"given name": "Gregor",
"height": 1.76,
"employer": "University of Waterloo",
"alma mater": "Purdue University",
"graduation year": 2014,
"employment years": 11,
42: 12
}
print(
info[42], info["surname"]
)
print(info)
12 Kenneth
{'surname': 'Kenneth', 'given name': 'Gregor', 'height': 1.76, 'employer': 'University of Waterloo', 'alma mater': 'Purdue University', 'graduation year': 2014, 'employment years': 11, 42: 12}
info["employment years"] = 12
print(info)
{'surname': 'Kenneth', 'given name': 'Gregor', 'height': 1.76, 'employer': 'University of Waterloo', 'alma mater': 'Purdue University', 'graduation year': 2014, 'employment years': 12, 42: 12}
info = {
"surname": "Richards",
"given name": "Gregor",
"height": 1.76,
"employer": "University of Waterloo",
"alma mater": "Purdue University",
"graduation year": 2014,
"employment years": 11
}
for x in info:
print(x, "is", info[x])
lst = list(info)
print(lst)
surname is Richards given name is Gregor height is 1.76 employer is University of Waterloo alma mater is Purdue University graduation year is 2014 employment years is 11 ['surname', 'given name', 'height', 'employer', 'alma mater', 'graduation year', 'employment years']
info = {
"surname": "Richards",
"given name": "Gregor",
"height": 1.76,
"employer": "University of Waterloo",
"alma mater": "Purdue University",
"graduation year": 2014,
"employment years": 11
}
#print(info["citizenship"])
info["citizenship"] = ["USA"]
print(info["citizenship"])
print(info["surname"])
print(info)
#info["last name"] = info["surname"]
if "age" in info:
print("This person is", info["age"], "years old")
info.pop("employer") # Fired for tormenting Science students
print(info["employer"])
['USA']
Richards
{'surname': 'Richards', 'given name': 'Gregor', 'height': 1.76, 'employer': 'University of Waterloo', 'alma mater': 'Purdue University', 'graduation year': 2014, 'employment years': 11, 'citizenship': ['USA']}
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Input In [43], in <cell line: 23>() 20 print("This person is", info["age"], "years old") 22 info.pop("employer") # Fired for tormenting Science students ---> 23 print(info["employer"]) KeyError: 'employer'
import typing
def distribution(
lst: typing.Sequence
) -> dict[typing.Any, int]:
"""
Returns the distribution of values in lst, as a dictionary associating each *value* in the
list with its count.
"""
ret: dict[typing.Any, int] = {}
for val in lst:
#if val not in ret:
if not (val in ret):
ret[val] = 0
ret[val] = ret[val] + 1
return ret
print(distribution([
8, 6, 7, 5, 3, 0, 9, 2, 4, 6, 0, 1
]))
print(distribution("It was the best of times, it was the worst of times"))
{8: 1, 6: 2, 7: 1, 5: 1, 3: 1, 0: 2, 9: 1, 2: 1, 4: 1, 1: 1}
{'I': 1, 't': 8, ' ': 11, 'w': 3, 'a': 2, 's': 6, 'h': 2, 'e': 5, 'b': 1, 'o': 3, 'f': 2, 'i': 3, 'm': 2, ',': 1, 'r': 1}
def distributionChart(lst: typing.Sequence) -> None:
"""
Prints a distribution chart of the frequency of each value in the sequence lst.
"""
dist = distribution(lst)
for key in sorted(dist):
print(key, "*" * dist[key])
distributionChart([8, 6, 7, 5, 3, 0, 9, 2, 4, 6, 0, 1])
distributionChart("It was the best of times, it was the worst of times")
0 ** 1 * 2 * 3 * 4 * 5 * 6 ** 7 * 8 * 9 * *********** , * I * a ** b * e ***** f ** h ** i *** m ** o *** r * s ****** t ******** w ***
def distributionChart(lst: typing.Sequence) -> None:
"""
Prints a distribution chart of the frequency of each value in the sequence lst.
"""
dist = distribution(lst)
def appearances(key: typing.Any) -> int:
"""
Return the number of times that the value key appears in the list lst.
"""
return dist[key]
for key in sorted(dist, key=appearances, reverse=True):
print(key, "*" * dist[key])
distributionChart([8, 6, 7, 5, 3, 0, 9, 2, 4, 6, 0, 1])
distributionChart("It was the best of times, it was the worst of times")
6 ** 0 ** 8 * 7 * 5 * 3 * 9 * 2 * 4 * 1 * *********** t ******** s ****** e ***** w *** o *** i *** a ** h ** f ** m ** I * b * , * r *
annoying = {}
annoying[0.3-0.2] = "Hello"
annoying[0.1] = "world"
print(annoying)
{0.09999999999999998: 'Hello', 0.1: 'world'}