Python Forum
How to print counter without bracket in python and sort data.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print counter without bracket in python and sort data.
#1
How to print counter without bracket and sort result in python ?

I need to have this final result :

2 : 2
3 : 3
4 : 4
5 : 3
7 : 2
But I'v got this :

Counter({4: 4, 3: 3, 5: 3, 2: 2, 7: 2})
Here is my code :

counter=collections.Counter(lst)
    print(counter)
    counter.keys()
    for key, value in counter.items():
        #print(key,":", value)
        ctx = (key, value)
        print(ctx)
    tab.append(ctx)
    tab.sort()
    #print(tab)
    #print(*tab, sep = "\n")
Or this result but not sorted with this print options :

print(key,":", value)
3 : 3
4 : 4
2 : 2
5 : 3
7 : 2
Thanks for any help. Big Grin
Reply
#2
If you put sorted() around counter.items() they will come out sorted.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 1,204 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  Grouping Data based on 30% bracket purnima1 4 1,196 Mar-10-2023, 07:38 PM
Last Post: deanhystad
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,321 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  How to sort values descending from a row in a dataframe using python sankarachari 1 1,425 Aug-16-2021, 08:55 AM
Last Post: jamesaarr
  Sort data from JSON file Dummy_in_programming 2 2,452 Jan-04-2021, 06:17 PM
Last Post: deanhystad
  how to print all data from all data array? korenron 3 2,477 Dec-30-2020, 01:54 PM
Last Post: korenron
  python sort date beginner2020 13 6,058 Nov-06-2020, 03:30 PM
Last Post: beginner2020
  What's the best way to sort this data with Python? xtrax 2 1,702 Mar-15-2020, 08:08 AM
Last Post: Larz60+
  How to print data in bracket in list in one side. phob0s 2 2,187 Jul-23-2019, 08:00 AM
Last Post: phob0s
  Python word counter and ranker Drone4four 6 7,556 Jan-18-2019, 11:58 PM
Last Post: Drone4four

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020