0

I have a python program with a for loop that returns to me integers in tuples. These tuples are contained in lists as shown:

[(1,), (2,), (3,), (5,)]
[(4,)]    
[(2,)]
[(1,), (2,), (3,), (4,)]

How can I get the mode from such a result?

Jorge
  • 615
  • 8
  • 13
  • By "mode" do you mean most frequently occurring number? – Tadhg McDonald-Jensen May 30 '16 at 20:49
  • Yes, I want to get the most frequently occurring number from the integers that are present in those lists. – Jorge May 30 '16 at 20:52
  • then take a look at [Finding the mode of a list in python](http://stackoverflow.com/questions/10797819/finding-the-mode-of-a-list-in-python) – Tadhg McDonald-Jensen May 30 '16 at 20:53
  • That is not hard, my problem is how I will extract the integers from the result shown above and use them to get the mode. My result is returned exactly as indicated. – Jorge May 30 '16 at 20:55
  • ok... but can't you just combine the answers from [Convert list of tuples to list?](http://stackoverflow.com/questions/10941229/convert-list-of-tuples-to-list) and then ones from [Finding the mode of a list in python](http://stackoverflow.com/questions/10797819/finding-the-mode-of-a-list-in-python) to make a code that does this? – Tadhg McDonald-Jensen May 30 '16 at 20:59
  • `mode = collections.Counter(n for tup in data for n in tup).most_common(1)` – Tadhg McDonald-Jensen May 30 '16 at 21:02
  • thanks, I think that might work but in Convert list of tuples to list? I didn't see how to combine several lists of tuples to a list – Jorge May 30 '16 at 21:27
  • well normally to access each number in a 3d list you would have three nested for loops, you can similarly do that in generator comprehension: `collections.Counter(n for sub_list in lists for tup in sub_list for n in tup).most_common(1)` – Tadhg McDonald-Jensen May 30 '16 at 21:34
  • it is returning an TypeError saying 'int' object is not iterable – Jorge May 30 '16 at 21:45
  • Could you edit your question? I am voting to reopen this question and I think if you add some extra detail on what you are doing I'm sure I could be more help. – Tadhg McDonald-Jensen May 30 '16 at 21:50

0 Answers0