How to compare occurences in the same list of tuples












3















I have a list of tuples like this one:
L = [(23,56,48),(48,93,81),(48,56,23),(54,34,21),(48,98,71),(98,71,48),(56,23,48)]
Some of the tuples are having exactly the same numbers but in different order. I want to count the occurrences of the tuples and overwrite the ones that have the same values inside. I read several answers in here and I managed to do something using the Counter method. But what I tried doesn't seem to work for me. I am new at python so maybe I don't understand the exact use of Counter method.



L1 = Counter()
for item in L:
for element in item:
if element in item:
L1[tuple(item)] = L1[tuple(item)] + 1
print(L1)


The result I get is:



Counter({(23, 56, 48): 3, (48, 93, 81): 3, (48, 56, 23): 3, (54, 34, 21): 3, 
(48, 98, 71): 3, (98, 71, 48): 3, (56, 23, 48): 3})


And the result I want to get is:



Counter({(23, 56, 48): 3, (48, 98, 71): 3, (48, 93, 81): 1, (54, 34, 21): 1})









share|improve this question

























  • What does "overwrite the ones that have the same values inside" mean?

    – DeepSpace
    Jan 2 at 13:27











  • Maybe I cant explain it well, see the actual and the expected results for help, The tuples that contain the same numbers but in different order have to count as the same tuple

    – immalee
    Jan 2 at 13:28











  • Use set() instead for comparing tuples

    – NEGR KITAEC
    Jan 2 at 13:29











  • no the order doesn't matter, and this is the context, I tried to do it as simple as possible to ask my question...I don't know what set() does, or how to use it, I will try and see how it works

    – immalee
    Jan 2 at 13:31











  • Counter is a dictionary at heart and can't use sets as key - it can use frozensets though: Counter(frozenset(t) for t in L)should give you what you want.

    – Patrick Artner
    Jan 2 at 13:44
















3















I have a list of tuples like this one:
L = [(23,56,48),(48,93,81),(48,56,23),(54,34,21),(48,98,71),(98,71,48),(56,23,48)]
Some of the tuples are having exactly the same numbers but in different order. I want to count the occurrences of the tuples and overwrite the ones that have the same values inside. I read several answers in here and I managed to do something using the Counter method. But what I tried doesn't seem to work for me. I am new at python so maybe I don't understand the exact use of Counter method.



L1 = Counter()
for item in L:
for element in item:
if element in item:
L1[tuple(item)] = L1[tuple(item)] + 1
print(L1)


The result I get is:



Counter({(23, 56, 48): 3, (48, 93, 81): 3, (48, 56, 23): 3, (54, 34, 21): 3, 
(48, 98, 71): 3, (98, 71, 48): 3, (56, 23, 48): 3})


And the result I want to get is:



Counter({(23, 56, 48): 3, (48, 98, 71): 3, (48, 93, 81): 1, (54, 34, 21): 1})









share|improve this question

























  • What does "overwrite the ones that have the same values inside" mean?

    – DeepSpace
    Jan 2 at 13:27











  • Maybe I cant explain it well, see the actual and the expected results for help, The tuples that contain the same numbers but in different order have to count as the same tuple

    – immalee
    Jan 2 at 13:28











  • Use set() instead for comparing tuples

    – NEGR KITAEC
    Jan 2 at 13:29











  • no the order doesn't matter, and this is the context, I tried to do it as simple as possible to ask my question...I don't know what set() does, or how to use it, I will try and see how it works

    – immalee
    Jan 2 at 13:31











  • Counter is a dictionary at heart and can't use sets as key - it can use frozensets though: Counter(frozenset(t) for t in L)should give you what you want.

    – Patrick Artner
    Jan 2 at 13:44














3












3








3








I have a list of tuples like this one:
L = [(23,56,48),(48,93,81),(48,56,23),(54,34,21),(48,98,71),(98,71,48),(56,23,48)]
Some of the tuples are having exactly the same numbers but in different order. I want to count the occurrences of the tuples and overwrite the ones that have the same values inside. I read several answers in here and I managed to do something using the Counter method. But what I tried doesn't seem to work for me. I am new at python so maybe I don't understand the exact use of Counter method.



L1 = Counter()
for item in L:
for element in item:
if element in item:
L1[tuple(item)] = L1[tuple(item)] + 1
print(L1)


The result I get is:



Counter({(23, 56, 48): 3, (48, 93, 81): 3, (48, 56, 23): 3, (54, 34, 21): 3, 
(48, 98, 71): 3, (98, 71, 48): 3, (56, 23, 48): 3})


And the result I want to get is:



Counter({(23, 56, 48): 3, (48, 98, 71): 3, (48, 93, 81): 1, (54, 34, 21): 1})









share|improve this question
















I have a list of tuples like this one:
L = [(23,56,48),(48,93,81),(48,56,23),(54,34,21),(48,98,71),(98,71,48),(56,23,48)]
Some of the tuples are having exactly the same numbers but in different order. I want to count the occurrences of the tuples and overwrite the ones that have the same values inside. I read several answers in here and I managed to do something using the Counter method. But what I tried doesn't seem to work for me. I am new at python so maybe I don't understand the exact use of Counter method.



L1 = Counter()
for item in L:
for element in item:
if element in item:
L1[tuple(item)] = L1[tuple(item)] + 1
print(L1)


The result I get is:



Counter({(23, 56, 48): 3, (48, 93, 81): 3, (48, 56, 23): 3, (54, 34, 21): 3, 
(48, 98, 71): 3, (98, 71, 48): 3, (56, 23, 48): 3})


And the result I want to get is:



Counter({(23, 56, 48): 3, (48, 98, 71): 3, (48, 93, 81): 1, (54, 34, 21): 1})






python list dictionary tuples counter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 13:41









Miraj50

2,7801925




2,7801925










asked Jan 2 at 13:26









immaleeimmalee

225




225













  • What does "overwrite the ones that have the same values inside" mean?

    – DeepSpace
    Jan 2 at 13:27











  • Maybe I cant explain it well, see the actual and the expected results for help, The tuples that contain the same numbers but in different order have to count as the same tuple

    – immalee
    Jan 2 at 13:28











  • Use set() instead for comparing tuples

    – NEGR KITAEC
    Jan 2 at 13:29











  • no the order doesn't matter, and this is the context, I tried to do it as simple as possible to ask my question...I don't know what set() does, or how to use it, I will try and see how it works

    – immalee
    Jan 2 at 13:31











  • Counter is a dictionary at heart and can't use sets as key - it can use frozensets though: Counter(frozenset(t) for t in L)should give you what you want.

    – Patrick Artner
    Jan 2 at 13:44



















  • What does "overwrite the ones that have the same values inside" mean?

    – DeepSpace
    Jan 2 at 13:27











  • Maybe I cant explain it well, see the actual and the expected results for help, The tuples that contain the same numbers but in different order have to count as the same tuple

    – immalee
    Jan 2 at 13:28











  • Use set() instead for comparing tuples

    – NEGR KITAEC
    Jan 2 at 13:29











  • no the order doesn't matter, and this is the context, I tried to do it as simple as possible to ask my question...I don't know what set() does, or how to use it, I will try and see how it works

    – immalee
    Jan 2 at 13:31











  • Counter is a dictionary at heart and can't use sets as key - it can use frozensets though: Counter(frozenset(t) for t in L)should give you what you want.

    – Patrick Artner
    Jan 2 at 13:44

















What does "overwrite the ones that have the same values inside" mean?

– DeepSpace
Jan 2 at 13:27





What does "overwrite the ones that have the same values inside" mean?

– DeepSpace
Jan 2 at 13:27













Maybe I cant explain it well, see the actual and the expected results for help, The tuples that contain the same numbers but in different order have to count as the same tuple

– immalee
Jan 2 at 13:28





Maybe I cant explain it well, see the actual and the expected results for help, The tuples that contain the same numbers but in different order have to count as the same tuple

– immalee
Jan 2 at 13:28













Use set() instead for comparing tuples

– NEGR KITAEC
Jan 2 at 13:29





Use set() instead for comparing tuples

– NEGR KITAEC
Jan 2 at 13:29













no the order doesn't matter, and this is the context, I tried to do it as simple as possible to ask my question...I don't know what set() does, or how to use it, I will try and see how it works

– immalee
Jan 2 at 13:31





no the order doesn't matter, and this is the context, I tried to do it as simple as possible to ask my question...I don't know what set() does, or how to use it, I will try and see how it works

– immalee
Jan 2 at 13:31













Counter is a dictionary at heart and can't use sets as key - it can use frozensets though: Counter(frozenset(t) for t in L)should give you what you want.

– Patrick Artner
Jan 2 at 13:44





Counter is a dictionary at heart and can't use sets as key - it can use frozensets though: Counter(frozenset(t) for t in L)should give you what you want.

– Patrick Artner
Jan 2 at 13:44












1 Answer
1






active

oldest

votes


















2














You need to map the tuples with the same values to the same key, one way is sorting the tuples (using sorted):



from collections import Counter

L = [(23,56,48),(48,93,81),(48,56,23),(54,34,21),(48,98,71),(98,71,48),(56,23,48)]


result = Counter(tuple(e) for e in map(sorted, L))
print(result)


Output



Counter({(23, 48, 56): 3, (48, 71, 98): 2, (21, 34, 54): 1, (48, 81, 93): 1})





share|improve this answer





















  • 1





    Maybe Counter(frozenset(t) for t in L)? Not sure what would be more performant

    – DeepSpace
    Jan 2 at 13:32













  • Yes that could be another option too

    – Daniel Mesejo
    Jan 2 at 13:32











  • Again as I said being new to python is the problem, I didn't think about using something like map at all. Thank you I will see how it works and try to understand it.

    – immalee
    Jan 2 at 13:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007215%2fhow-to-compare-occurences-in-the-same-list-of-tuples%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You need to map the tuples with the same values to the same key, one way is sorting the tuples (using sorted):



from collections import Counter

L = [(23,56,48),(48,93,81),(48,56,23),(54,34,21),(48,98,71),(98,71,48),(56,23,48)]


result = Counter(tuple(e) for e in map(sorted, L))
print(result)


Output



Counter({(23, 48, 56): 3, (48, 71, 98): 2, (21, 34, 54): 1, (48, 81, 93): 1})





share|improve this answer





















  • 1





    Maybe Counter(frozenset(t) for t in L)? Not sure what would be more performant

    – DeepSpace
    Jan 2 at 13:32













  • Yes that could be another option too

    – Daniel Mesejo
    Jan 2 at 13:32











  • Again as I said being new to python is the problem, I didn't think about using something like map at all. Thank you I will see how it works and try to understand it.

    – immalee
    Jan 2 at 13:33
















2














You need to map the tuples with the same values to the same key, one way is sorting the tuples (using sorted):



from collections import Counter

L = [(23,56,48),(48,93,81),(48,56,23),(54,34,21),(48,98,71),(98,71,48),(56,23,48)]


result = Counter(tuple(e) for e in map(sorted, L))
print(result)


Output



Counter({(23, 48, 56): 3, (48, 71, 98): 2, (21, 34, 54): 1, (48, 81, 93): 1})





share|improve this answer





















  • 1





    Maybe Counter(frozenset(t) for t in L)? Not sure what would be more performant

    – DeepSpace
    Jan 2 at 13:32













  • Yes that could be another option too

    – Daniel Mesejo
    Jan 2 at 13:32











  • Again as I said being new to python is the problem, I didn't think about using something like map at all. Thank you I will see how it works and try to understand it.

    – immalee
    Jan 2 at 13:33














2












2








2







You need to map the tuples with the same values to the same key, one way is sorting the tuples (using sorted):



from collections import Counter

L = [(23,56,48),(48,93,81),(48,56,23),(54,34,21),(48,98,71),(98,71,48),(56,23,48)]


result = Counter(tuple(e) for e in map(sorted, L))
print(result)


Output



Counter({(23, 48, 56): 3, (48, 71, 98): 2, (21, 34, 54): 1, (48, 81, 93): 1})





share|improve this answer















You need to map the tuples with the same values to the same key, one way is sorting the tuples (using sorted):



from collections import Counter

L = [(23,56,48),(48,93,81),(48,56,23),(54,34,21),(48,98,71),(98,71,48),(56,23,48)]


result = Counter(tuple(e) for e in map(sorted, L))
print(result)


Output



Counter({(23, 48, 56): 3, (48, 71, 98): 2, (21, 34, 54): 1, (48, 81, 93): 1})






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 13:36

























answered Jan 2 at 13:31









Daniel MesejoDaniel Mesejo

18.7k21433




18.7k21433








  • 1





    Maybe Counter(frozenset(t) for t in L)? Not sure what would be more performant

    – DeepSpace
    Jan 2 at 13:32













  • Yes that could be another option too

    – Daniel Mesejo
    Jan 2 at 13:32











  • Again as I said being new to python is the problem, I didn't think about using something like map at all. Thank you I will see how it works and try to understand it.

    – immalee
    Jan 2 at 13:33














  • 1





    Maybe Counter(frozenset(t) for t in L)? Not sure what would be more performant

    – DeepSpace
    Jan 2 at 13:32













  • Yes that could be another option too

    – Daniel Mesejo
    Jan 2 at 13:32











  • Again as I said being new to python is the problem, I didn't think about using something like map at all. Thank you I will see how it works and try to understand it.

    – immalee
    Jan 2 at 13:33








1




1





Maybe Counter(frozenset(t) for t in L)? Not sure what would be more performant

– DeepSpace
Jan 2 at 13:32







Maybe Counter(frozenset(t) for t in L)? Not sure what would be more performant

– DeepSpace
Jan 2 at 13:32















Yes that could be another option too

– Daniel Mesejo
Jan 2 at 13:32





Yes that could be another option too

– Daniel Mesejo
Jan 2 at 13:32













Again as I said being new to python is the problem, I didn't think about using something like map at all. Thank you I will see how it works and try to understand it.

– immalee
Jan 2 at 13:33





Again as I said being new to python is the problem, I didn't think about using something like map at all. Thank you I will see how it works and try to understand it.

– immalee
Jan 2 at 13:33




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007215%2fhow-to-compare-occurences-in-the-same-list-of-tuples%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas