-2

For a list x like this [8, 8, 8] this works fine:

from collections import Counter
Counter(x)

For a list x like this though [[6, 88], [35, 64], [15, 7]] the above does not work.

I am not interested in counting via sFrames, panda etc. Can this be done for such a list similarly, or do I have to make a new correctly concatenated value in the list so as to count?

I am not interested in the count of 6, 88, 35,... rather the count of [6,88], [35, 64]...

So the expected output should be:

Counter({[6,88]: 3}, ...)

if at all possible.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
thebluephantom
  • 16,458
  • 8
  • 40
  • 83
  • What answer do you expect from that array? – Jongware Jun 05 '20 at 07:10
  • @usr2564301 I am not interested in the count of 6, 88, 35,... rather the count of [6,88], [35, 64]... – thebluephantom Jun 05 '20 at 07:22
  • I find it somewhat arrogant the closure here as the pointed to answers do not actually provide the answer. I am not an idiot. – thebluephantom Jun 05 '20 at 07:26
  • 1
    That output is not possible, as those lists can't be keys. `Counter(map(tuple, x))`? – jonrsharpe Jun 05 '20 at 07:30
  • @johnsharpe So sir. Thx. I suggest the question be reopened and that answer be provided. Now I know that and can convert to dataframe and check. – thebluephantom Jun 05 '20 at 07:32
  • 1
    That just changes what it's a dupe of, which I've done. – jonrsharpe Jun 05 '20 at 07:34
  • @jonrsharpe OK, but the central point here is the quick down voting on this site. Anyway thx. – thebluephantom Jun 05 '20 at 07:36
  • @jonrsharpe Nice try but I can't use a tuple. – thebluephantom Jun 05 '20 at 07:37
  • 1
    That's what's supposed to happen. Your question was unclear, at least three people (two voters to close and one answerer) interpreted it as being about flattening, and doesn't show any research effort. And *why* can't you use a tuple? What's the *context* for this? Any other secret constraints? – jonrsharpe Jun 05 '20 at 07:38
  • @jonrsharpe Pls, no research effort? If you look at what I have done in the past and a bounty, I find that pretty hard to swallow. I am doing the travelling salesman problem with 10 lines of python and am checking the provided solution painstakingly statement by statement and pure python is a little new. This embodies the issues on SO, aside to the good things. I also provided Counter as a n example. – thebluephantom Jun 05 '20 at 07:42
  • 2
    I'm not saying you've made no effort, I can't know that, I'm saying this question doesn't *show* any. Given the error from the Counter you could have looked up the current dupe as well as I did, for example. – jonrsharpe Jun 05 '20 at 07:43
  • 1
    How about `cnt = [l.count(i) for i in l]`? You can still do something like `cnt[l.index([6, 88])]` to get the count. – Chris Jun 05 '20 at 07:45
  • @Chris I will look at that. – thebluephantom Jun 05 '20 at 07:47
  • @Chris interesting insight, thx, partial solution as I would need to reduce or get distincts. That I think would do the trick. Thx – thebluephantom Jun 05 '20 at 07:51
  • 1
    You don't need to change your original data, you can just do: `Counter(tuple(sub) for sub in lst)` – Tomerikoo Jun 05 '20 at 07:54
  • @jonrsharpe I see answer in the comments not mentioned by the duplicates. Pls consider re-opening. – thebluephantom Jun 05 '20 at 07:58
  • Can you just explain your remark about not being able to use tuples? – Tomerikoo Jun 05 '20 at 07:59
  • My ignorance and it was someone else's code example that I was evaluating, learning from. travelling salesman algorithm in 10 lines. – thebluephantom Jun 05 '20 at 08:00
  • @jonrsharpe and here is the correct dup: https://stackoverflow.com/questions/45019607/count-occurrence-of-a-list-in-a-list-of-lists .... – Tomerikoo Jun 05 '20 at 08:00
  • But I can now , thx anyway. – thebluephantom Jun 05 '20 at 08:01
  • 1
    @thebluephantom BTW, I just noticed that this is actually the same as jonrsharpe's comment from half an hour ago: `Counter(map(tuple, x))` – Tomerikoo Jun 05 '20 at 08:04
  • @Tomerikoo I missed it then, but am happy I know the answer. – thebluephantom Jun 05 '20 at 08:05
  • 2
    Please don't put answers in the question. The dupe about counting lists in lists is a more specific application of the same solution from the dupe about lists being unhashable (i.e. tuples, which apparently you *can* use after all); if one is relevant, both are. I've removed the third, which again means the below (accepted!) answer doesn't apply. – jonrsharpe Jun 05 '20 at 08:19

1 Answers1

-1

Multi-dimensional lists can be flattened with list comprehension, so you could try:

c = Counter([i for sublist in x for i in sublist])
c4llmeco4ch
  • 311
  • 2
  • 11