2

I know my code it's a mess, but I need to get the desired output for an assignment. I didn't learn anything about tuple output I mean how to manipulate... But all that I found on internet is about tuples like: ('A', 10) with a key and a value. And for what I understand I have a tuple with two values.

name = input("Enter file:") if len(name) < 1 : name = "mbox-short.txt" handle = open(name) raw_dat = list() time = list() hour = list() dic = dict() rdy = list() for line in handle : if not line.startswith("From") : continue if line.startswith("From:") : continue raw_dat.append(line.split()) for item in raw_dat : item = item[5] time.append(item) for data in time : hour.append(data[0:2]) for hr in hour: dic[hr] = dic.get(hr, 0) + 1 for tm in sorted(dic.items()) : print(tm) 

My output atm...

('04', 3) ('06', 1) ('07', 1) ('09', 2) ('10', 3) ('11', 6) ('14', 1) ('15', 2) ('16', 4) ('17', 2) ('18', 1) ('19', 1) 

Desired output...

04 3 06 1 07 1 09 2 10 3 11 6 14 1 15 2 16 4 17 2 18 1 19 1 

Thank you.

10
  • Did you mean: ‘print(tm[0], tm[1])’? Commented Nov 22, 2020 at 20:57
  • Thank you very much... great answer short and the perfect solution. Really thank you. Commented Nov 22, 2020 at 20:59
  • 1
    print(*tm) would do it Commented Nov 22, 2020 at 21:06
  • In this code, for line in handle : if not line.startswith("From") : continue if line.startswith("From:") : continue raw_dat.append(line.split()), do you think raw_dat.append(line.split()) will get executed? Commented Nov 22, 2020 at 21:07
  • @JoeFerndz . I don't know what you mean... At first I thought it was a trick question. But I cut(#) the line of code and the whole thing wouldn't work. So I would like to know what you mean by that. It get's executed and it's an important part(at least for me) of the code. Commented Nov 22, 2020 at 22:39

1 Answer 1

1

Thank you @quamrana for helping me... This is the solution given...

name = input("Enter file:") if len(name) < 1 : name = "mbox-short.txt" handle = open(name) raw_dat = list() time = list() hour = list() dic = dict() for line in handle : if not line.startswith("From") : continue if line.startswith("From:") : continue raw_dat.append(line.split()) for item in raw_dat : item = item[5] time.append(item) for data in time : hour.append(data[0:2]) for hr in hour: dic[hr] = dic.get(hr, 0) + 1 for tm in sorted(dic.items()) : print(tm[0], tm[1]) 

original :

print(tm) 

solution (1) :

print(tm[0], tm[1]) 

solution (2) :

print(*tm) 
Sign up to request clarification or add additional context in comments.

4 Comments

How about print(*tm)?
@GrzegorzSkibinski Works the same as print(tm[0], tm[1]). Thank you very much.
I know, it's just shorter and more generic. More 'python-ish' if you will
@GrzegorzSkibinski True, looks much more like I know what I'm doing. Thank you very much for that. Tomorrow I'll have a look of what that " * " does in general.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.