![]() |
How to output set items without the curly braces ? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to output set items without the curly braces ? (/thread-18256.html) |
How to output set items without the curly braces ? - jimthecanadian - May-10-2019 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 RE: Iterate multiple Items at once - Yoriz - May-10-2019 candidates = ({'book'}, {'pencil'}, {'School bag'}) for x in candidates: print(', '.join(y for y in x))
candidates2 = ({'book', 'pencil'}, {'book', 'school bag'}, {'pencil', 'School bag'}) for x in candidates2: print(', '.join(y for y in x))
RE: Iterate multiple Items at once - jimthecanadian - May-10-2019 Thanks, Yoriz. exactly what I needed RE: How to output set items without the curly braces ? - perfringo - May-11-2019 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 |