Reputation: 81
My results look like this:
('04', 3)
('06', 1)
('07', 1)
('09', 2)
I want them to look like this:
04 3
06 1
07 1
09 2
I've tried split (Not a string, can't do that) .replace (requires string as left operand not a tuple.) I 'think' I've tried list comprehension correctly. I'm not clear what I should actually be doing to accomplish my desired end result. I'm trying to use n00b functions since I'm learning this therefore, I'm ignore Lambda as I have no clue what that is. I am trying to stick to list, dictionary, comprehension, slice etc at this point.
My example below won't run as written, I'm just trying to show my last attempt. When I comment out lines 15, 16, 17, it runs and creates the example shown above.
fname = input("Enter file:")
if len(fname) < 1 : fname = "mbox-short.txt"
emails = open(fname)
counts=dict()
for email in emails:
result=email.startswith('From ')
if result is True:
time=(email.split()[5])
hour=(time.split(':')[0])
counts[hour]=counts.get(hour, 0) +1
tmp=list()
for k, v in counts.items():
tmp.append( (k, v) )
tmp.sort()
for char in tmp:
if char in "(',)":
tmp.replace(char,'')
for k in tmp:
print (k)
Upvotes: 0
Views: 46
Reputation: 2544
This may help you
# ....
# Rest of your code
# ....
tmp=list()
for k, v in counts.items():
tmp.append( (int(k), int(v)) ) # !!! make it integer
tmp.sort()
for k in tmp:
print (k)
Upvotes: 1
Reputation: 26039
Assuming [('04', 3), ('06', 1), ('07', 1), ('09', 2)]
is your input, you could just do:
tupls = [('04', 3), ('06', 1), ('07', 1), ('09', 2)]
for x, y in tupls:
print(x, y)
# 04 3
# 06 1
# 07 1
# 09 2
Upvotes: 1
Reputation: 51683
mydata = [('04', 3), ('06', 1), ('07', 1), ('09', 2)]
for tup in mydata:
print(*tup) # this uses the default sep = " " for print
# *tup provides each element of tup to print
Output:
04 3
06 1
07 1
09 2
See
sep=" "
Upvotes: 1
Reputation: 106901
You can use the str.join
method on a space:
for k in tmp:
print (' '.join(k))
Upvotes: 1