0

sorry if this is a duplicate thread but I couldn't find one.

I'm currently trying to make an inventory for a game that is in the works. The problem I run into is that, when you have a lot of a particular item, it kind of wastes a lot of space.

Here's my code: (inv is the inventory)

inv = ["item1","item2","item3","item2","item2","item1"]

print(', '.join(inventory))

Output:

item1, item2, item3, item2, item2, item1

I'd like to, instead of doing that, do this:

item1 x2, item2 x3, item3

I don't really care if you need item3 to be x1, either way would be fine, thanks.

cs95
  • 379,657
  • 97
  • 704
  • 746
Ryan
  • 59
  • 10

3 Answers3

2

There are many options actually. The main goal is to count your items, and print them out in order.

One option is to use collections.OrderedDict. Others are collections.Counter/collections.defaultdict, but I'll give you an example with the former:

from collections import OrderedDict

counter = OrderedDict()

inv = ["item1", "item2", "item3", "item2", "item2", "item1"]
for i in inv:
    if i not in counter:
        counter[i] = 0

    counter[i] += 1

for c in counter:
    if counter[c] > 1:
        print(c + ' x{}'.format(counter[c]))
    else:
        print(c)

This prints:

item1 x2
item2 x3
item3

You're free to modify this example as needed if you want to print them out in one line. This code was written in python3.x.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • @RyanTartaglia Quite so. If you're on python3, you can use `print(..., end=', ')`. If you're on python 2, first import print using `from __future__ import print_function` and do that. – cs95 Jun 29 '17 at 23:29
  • @RyanTartaglia You'll have to do that in both print statements :) – cs95 Jun 29 '17 at 23:32
  • @RyanTartaglia Noticed you revoked acceptance. What's wrong? Anything I can help with? – cs95 Jun 30 '17 at 12:54
  • @RyanTartaglia Hey, no worries. As an asker you're not obligated to accept. But thank you for doing so. :) – cs95 Jul 05 '17 at 00:58
  • Also, my bad... did mean to upvote your question before but it slipped my mind. Did it now. – cs95 Jul 05 '17 at 00:59
  • @RyanTartaglia Yes... I have an idea you might be unknowingly using the same referenced list in multiple places... can't say without your code. Open a new question and put your code there and I'd be happy to help. – cs95 Jul 31 '17 at 23:19
  • @RyanTartaglia Was it a problem with my answer? Or how you adapted it? If it was the former, I won't stop you from unmarking it... :) – cs95 Jul 31 '17 at 23:24
0

Here is an example with Counter:

from collections import Counter

inv = ["item1", "item2", "item3", "item2", "item2", "item1"]

print (', '.join('%s x%d' % (item, count)
                 for item, count in sorted(Counter(inv).items())))
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0
out = ['{} x{}'.format(item, inv.count(item)) if inv.count(item) > 1 else item 
   for item in set(inv)]

out:

['item1 x2', 'item2 x3', 'item3']
Alter
  • 3,332
  • 4
  • 31
  • 56
  • Same as Prune's answer, but less readable... – cs95 Jun 29 '17 at 23:24
  • I'd tend to agree with @Coldspeed here. When you have to began splitting list comprehensions across several lines, you'd want to consider refactoring it into a vanilla `for` loop. – Christian Dean Jun 29 '17 at 23:27
  • I think that's a valid point, I don't like taking up a lot of space for simple operations though – Alter Jun 29 '17 at 23:31