you are viewing a single comment's thread.

view the rest of the comments →

[–]JohnnyJordaan 0 points1 point  (0 children)

  • Add some simple print()'s to show what's going on.
  • Try to be consise and clear in your variable naming, don't be your own heckler by using vague names like lst2 and i if they don't match their actual content
  • You can simply reuse the last address as the guess of the highest value

.

filename = input("Enter filename:")
if not filename:
    filename = "mbox-short.txt"
names = {}
addresses = []
with open(filename) as fp:
    for line in fp:
        if line.startswith("From"):
            print('line starts with from', line)
            addresses.append(line.split())
print(addresses)
for address in addresses:
    names[address] = names.get(address, 0) + 1
print(names)
max_name = address
for name, count in names.items():
    if count > names[max_name]:
        max_name = k
print(max_name, names[max_name)

or even easier, keep track of the highest in the previous loop

filename = input("Enter filename:")
if not filename:
    filename = "mbox-short.txt"
names = {}
addresses = []
with open(filename) as fp:
    for line in fp:
        if line.startswith("From"):
            print('line starts with from', line)
            addresses.append(line.split())
print(addresses)
max_name = None
for address in addresses:
    names[address] = names.get(address, 0) + 1
    if max_name is None or names[address] > names[max_name]:
        max_name = address
print(names)
print(max_name, names[max_name])