Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matrix
#6
There are also some other approaches to explore:

- there are sets in Python (from documentation: "Basic uses include membership testing and eliminating duplicate entries."). Problem with solution below is that it will not short circuit (following code assumes that in case of empty matrix elements are considered equal as well, if its not the case comparison should be == 1):

In [1]: m = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]

In [2]: len({el for row in m for el in row}) <= 1    # 0 when empty matrix, 1 when all equal
Out[2]: True

In [3]: n = [[1, 1, 1], [1, 1, 1], [1, 2, 1]]

In [4]: len({el for row in n for el in row}) <= 1
Out[4]: False
- Python has built-in itertools module and it's documentation contains recipies where one of them is:

def all_equal(iterable):
    "Returns True if all the elements are equal to each other"
    g = groupby(iterable)
    return next(g, True) and not next(g, False)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Matrix - by Helmi - Feb-02-2019, 03:02 AM
RE: Matrix - by ichabod801 - Feb-02-2019, 03:06 AM
RE: Matrix - by Helmi - Feb-02-2019, 03:34 AM
RE: Matrix - by ichabod801 - Feb-02-2019, 04:30 AM
RE: Matrix - by perfringo - Feb-02-2019, 04:58 AM
RE: Matrix - by perfringo - Feb-02-2019, 08:37 AM
RE: Matrix - by Helmi - Feb-02-2019, 01:02 PM
RE: Matrix - by ichabod801 - Feb-02-2019, 04:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 984 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,523 May-03-2021, 06:30 AM
Last Post: Gribouillis
  matrix from matrix python numpy array shei7141 1 3,830 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020