-3

I have an output in the following format:

report = {frozenset({'banana', 'carrot'}): 3,
          frozenset({'apple'}): 3,
          frozenset({'carrot'}): 4,
          frozenset({'apple', 'carrot'}): 2,
          frozenset({'banana'}): 4}

I would like to convert it to the following format:

banana carrot, 3
apple, 3
carrot, 4
apple carrot, 2
banana, 4

I have tried:

for i in report:
   print i

Which returns:

frozenset(['carrot', 'banana'])
frozenset(['apple'])
frozenset(['carrot', 'apple'])
frozenset(['carrot'])
frozenset(['banana'])

Any help would be appreciated.

kevin
  • 1,914
  • 4
  • 25
  • 30
  • 2
    Can you post what you've got so far? – wogsland Dec 04 '15 at 14:20
  • 5
    Possible duplicate of [How can I convert a Python dictionary to a list of tuples?](http://stackoverflow.com/questions/674519/how-can-i-convert-a-python-dictionary-to-a-list-of-tuples) – SuperBiasedMan Dec 04 '15 at 14:21
  • I don't think my question is a duplicate of http://stackoverflow.com/questions/674519/how-can-i-convert-a-python-dictionary-to-a-list-of-tuples – kevin Dec 04 '15 at 14:29
  • In Python 2, use `.iteritems()` to get the key, value pairs of your dict, then use `' '.join()` on the frozenset keys. Eg, `for s, v in report.iteritems(): print ' '.join(s) + ',', v`. Note that neither dicts nor sets preserve order. – PM 2Ring Dec 04 '15 at 14:34

4 Answers4

2

Here's an answer that's similar to Dave's, except it has one less loop (using the join function to simplify things):

report = {frozenset({'banana', 'carrot'}): 3,
          frozenset({'apple'}): 3,
          frozenset({'carrot'}): 4,
          frozenset({'apple', 'carrot'}): 2,
          frozenset({'banana'}): 4}

for x in report:
    s=", ".join(x)
    s+=", "+str(report[x])
    print(s)

Here is the output:

carrot, apple, 2
banana, 4
apple, 3
carrot, 4
carrot, banana, 3

If you want to change it to a Python list, you could do this:

l=[]
for x in report:
    s=", ".join(x)
    s+=", "+str(report[x])
    l.append(s)

And if you want it sorted, try this:

l.sort()
Brōtsyorfuzthrāx
  • 4,387
  • 4
  • 34
  • 56
1

Here is what i did:

report = {frozenset({'banana', 'carrot'}): 3,
          frozenset({'apple'}): 3,
          frozenset({'carrot'}): 4,
          frozenset({'apple', 'carrot'}): 2,
          frozenset({'banana'}): 4}

for fruits in report:
    for fruit in fruits:
        print fruit,
    print ' , ',
    print report[fruits]

And the output:

carrot apple , 2

apple , 3

carrot banana , 3

carrot , 4

banana , 4

I had to extract each value from the list within the keys list inside report.

*The print with the comma helps me print without newlines.

Community
  • 1
  • 1
Dave
  • 91
  • 7
1

I think this would work:

for f, i in report.items():
    print( ' '.join(f) + ', ' +  str(i) )

The method join of a string s takes an iterable (which frozenset is), turns each element into string and joins them with s inbetween. The dict objects have the method items which returns a list of tuples, which are pairs of keys and values (key is in the 0 position on the tuple, value is in the 1). Look this question for example.

Community
  • 1
  • 1
xealits
  • 4,224
  • 4
  • 27
  • 36
0

You can change the sets to list just by simply using list(set)

for i in report:
    lst = list(i)
    for n in range(0,len(lst)):
        print(lst[n], end=" ")
    print(report[i])
harveyslash
  • 5,906
  • 12
  • 58
  • 111
tigris_kn
  • 1
  • 1