Python Forum
How to output set items without the curly braces ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to output set items without the curly braces ?
#1
Hi all,

I have not coded for a while and am still getting used to all the new fangled data types :)

I am taking a course in Database mining and we have been set a first problem of mining frequent patterns using the apriori algorithm. Now I have a pretty solid understanding of the logic but I when I am trying to print because of the way I have my data represented I can't figure out how to do it the way I would like. I am able to get through the first pass where I end up with a list of 1 item sets and a related value in another list. so the data looks like this candidates({book},{pencil}, {School bag},...) and I am able to iterate through it quite simply by doing this:

for x in candidates:
    for y in x:
        print(y ))
I do this because I do not wan the curly braces printed do I dereference he set element. Now on the second pass I have a list of sets containing two elements candidates({book, pencil},{book, school bag}, {pencil, School bag},...) and I would like to print it the same way with both set elements on one line. I have tried this but, no luck

for x in candidates:
    for y,z in x:
        print(y + " , " + z))
Thanks for any pointers
Reply
#2
candidates = ({'book'}, {'pencil'}, {'School bag'})

for x in candidates:
    print(', '.join(y for y in x))
Output:
book pencil School bag
candidates2 = ({'book', 'pencil'}, {'book', 'school bag'},
               {'pencil', 'School bag'})

for x in candidates2:
    print(', '.join(y for y in x))
Output:
pencil, book book, school bag pencil, School bag
Reply
#3
Thanks, Yoriz. exactly what I needed
Reply
#4
There is also possibility to use unpacking, which can be used with any iterable (note that sets are unordered):

>>> first = ({'book'}, {'pencil'}, {'School bag'})
>>> for element in first:
...     print(*element)
...
book
pencil
School bag
>>> second = ({'book', 'pencil'}, {'book', 'school bag'}, {'pencil', 'School bag'})
>>> for element in second:
...     print(*element, sep=', ')
... 
pencil, book
book, school bag
School bag, pencil
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiprocessing.Queue Issues (Missing/Corrupted Items/No Output) python3noob 0 3,209 Aug-03-2019, 09:38 PM
Last Post: python3noob
  search and replace dots inside curly braces kchinnam 1 2,677 May-01-2018, 06:53 PM
Last Post: Larz60+
  No curly braces in python? RedSkeleton007 9 9,253 Jul-28-2017, 11:01 AM
Last Post: tony1812
  Are braces rather than indentation ever wanted? michaelcollier 22 14,354 May-02-2017, 08:37 AM
Last Post: volcano63
  JSON API / no braces marucho21 1 3,928 Feb-01-2017, 07:35 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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