Phyton Code Help

Hi there!

I am trying to find solution to my thesis but I stucked in it. I have a list with 155x4 matrix and I am trying to find repeated elements for all rows. For example;as
Can somebody help to write the code, pls?

can’t write the Python but the logic is fairly simple - take all 4 elements from first row, see if any are in the 2nd (delete any that aren’t form your list) and then repeat through all the rows… if you are left with any values then they are in all rows

Can you explain how you are storing it?
What I mean is, do you have a list of lists, or is it a list of lists with lists.

List of lists, each inner list reprsenting a row

[
[x,x,x,x, ...],
[x,x,x,x, ...],
[x,x,x,x, ...]
]

list of list of lists; outer list representing list of matrices, first inner list representing matrix with innermost list representing rows.

[
  [
  [x,x,x,x, ...],
  [x,x,x,x, ...],
  [x,x,x,x, ...],
  [x,x,x,x, ...]
  ],
  [
  [x,x,x,x, ...],
  [x,x,x,x, ...],
  [x,x,x,x, ...],
  [x,x,x,x, ...]
  ]
]

Only list of lists. We have one data set as list but we want new list from that

Oh man. Well I’ll just submit this, I just finished it. To be honest. You’re not being that clear.

EDIT: The code below has an issue, it doesn’t reset the second pointers index below the first pointer after it hits the next row. But now that anyone viewing this knows that, it should be easy to fix.

#Test list
#list_of_ms = [[[x+y+z for z in range(4)]for y in range(10)]for x in range(3)]
 
def multi_matrice_counter(matrice_list):
    result = {}
 
    for m_indice, matrice in enumerate(matrice_list):
        for row_indice, row in enumerate(matrice):
            for element_indice, element in enumerate(row):
                if element not in result.keys():
                    result[element] = []
                for c_m_ind, comparison_matrice in enumerate(matrice_list[m_indice:]):
                    for c_r_ind, comparison_row in enumerate(comparison_matrice[row_indice:]):
                        if element_indice + 1 == len(row):
                            pass
                        else:
                            for c_e_ind, comparison_element in enumerate(comparison_row[element_indice:]):
                                if element == comparison_element:
                                    #Positon Tracks
                                    ind_e = c_e_ind + element_indice
                                    ind_r = c_r_ind + row_indice
                                    ind_m = c_m_ind + m_indice
                                    result[element].append({"matrice":ind_m, "row": ind_r, "column": ind_e})
    return result
 
 

I’m going to shoot straight. Data set can mean any kind of form, it could be a file with a delimiter of any kind, a json file, a dictionary, an object, set, etc.

Lets start with what form is a single matrix in?
Does the code need to work on a single matrix only?
Does the code need to remember elements when crossing between matrices?
Does the code need to cross over between matrices, and what format are those?

Either you’d need to supply me with detailed information, or you’ll have to arrange your data to go into the code I write.

@Gerschel When aydinca gets his job as a Python developer are your going to write all of his code for him? Lol

@aydinca What you need to do is write out your code in sentences step by step before you even start to write code. Then you convert the sentences into Python code.

1 Like

@brandon_wallace I’ll charge a consultation fee, gotta give a freebie to get them hooked.

1 Like

@aydinca
Hey man. I put together what I think you are looking for, in this process I realized I have a mistake in the last one that uses the dictionary, in short, I forgot to bring the index back down to zero when traversing the next row and matrice.
Anyways, this one doesn’t have that issue.

#Test list
matrice = [[(y+3)**z + 2 for z in range(4)][::-1] for y in range(10)]

def find_first_duplicate(matrice):
    """Find which value has a duplicate
    if there are more than one, only returns
    the first one."""
    height = len(matrice)
    width = len(matrice[0])
    upper_limit = height * width
    for pointer1 in range(upper_limit - 1):
        for pointer2 in range(1, upper_limit):
            p1_row, p1_col = divmod(pointer1, width)
            p2_row, p2_col = divmod(pointer2, width)
            if matrice[p1_row][p1_col] == matrice[p2_row][p2_col]:
                return matrice[p2_row][p2_col]



def find(val, matrice):
    """Find provided val in matrice
    returns list of indices where
    indice is the row it was found in.
    """
    result = []
    for row_indice, row in enumerate(matrice):
        for element in row:
            if val == element:
                result.append(row_indice)
    return result

print(find(find_first_duplicate(matrice), matrice))
1 Like

@Gerschel Nice bro! Good idea. You have to make those $$$ some how.
I lke the second version of the code better the first one had so many for loops. With those many for loops it make sense to break up the code into functions. What have you made with Python? Do you have a Github?

@brandon_wallace
I haven’t really made much, at least nothing I’ve kept. I mess around a lot in the interpreter. I did have many toy scripts I’ve made, but they were on a laptop that’s no longer with us (house fire).
The kind of toying around I do is mostly functional programming and logic structures. I don’t know why that’s what I’ve fell in love with, but it just makes more sense to me. So I write scripts like:

Note: half adder and full adder were not fully implemented yet, I know I can use the full language to script a basic cpu, registers, alu, ram, …; but I’ve challenged myself to do it as if functions are the only object that exist

def T(x):                                                                                                                                                                                  
    '''Sets a standard behavior                                                                                                                                                            
    Given x and y, returns x.                                                                                                                                                              
    Aka, left side.                                                                                                                                                                        
    '''                                                                                                                                                                                    
    return lambda y: x                                                                                                                                                                     
                                                                                                                                                                                           
def F(x):                                                                                                                                                                                  
    '''Sets a standard behavior that represents False                                                                                                                                      
    Given x and y, return y.                                                                                                                                                               
    Aka, return the right side                                                                                                                                                             
    '''                                                                                                                                                                                    
    return lambda y: y                                                                                                                                                                     
                                                                                                                                                                                           
                                                                                                                                                                                           
# Keeping with a standard established by T and F, we can build basic logic gates                                                                                                           
                                                                                                                                                                                           
def NOT(x):                                                                                                                                                                                
    '''If x is the Function that represents True.                                                                                                                                          
    Not(True) would result in a return of False.                                                                                                                                           
    Since this would be a return of the left side,                                                                                                                                         
    we put false in the x position.                                                                                                                                                        
    If x happened to be False, and the behavior of False                                                                                                                                   
    is to return the y side, we put True on the right.                                                                                                                                     
    So now Not(T) returns False                                                                                                                                                            
           Not(F) returns True                                                                                                                                                             
    '''                                                                                                                                                                                    
    return x(F)(T)                                                                                                                                                                         
                                                                                                                                                                                           
def OR(x):                                                                                                                                                                                 
    '''Since or returns true if one side or                                                                                                                                                
    the other is true, if the left side (our x) is true,                                                                                                                                   
    don't bother checking right, (refering to the behavior                                                                                                                                 
    of T, it returns the left side).                                                                                                                                                       
                                                                                                                                                                                           
    If left (our x) is false, it will return the right
    if right is false, both were false
    if right is true, then result is true.
    '''
    return lambda y: x(T)(y)


def AND(x):
    '''If left of the and is true then return right side,
    y is the right side
    if right side is false, answer is false,
    if right side is true, then both were true.
    if left (the x) was false then return false.
    Refer to the behavior of F
    '''
    return lambda y: x(y)(F)

def XOR(x):
    '''not(a and b) and (a or b)'''
    return lambda y: AND(NOT(AND(x)(y)))(OR(x)(y))


def HALF_ADDER(x):
    '''(AND(x)(y),XOR(x)(y))
    '''
    def f(y):
        def g():
            def h():
                return AND(x)(y)
        return XOR(x)(y), g
    return f


def FULL_ADDER(x):
    def f(y):
        def g(z, x=HALF_ADDER(x)(y)):
            print(x)
            #FIXME: T()() not T(,)
            #Possibly remove tuple in halfadder
            return (HALF_ADDER(z)(T(*x)), F(*x))
        return g
    return f
1 Like

One of my hobbies over the years has been to find others having coding problems and break it down for them. The funny thing is, when someone says, hey I need this, I’m thinking it’s there loss, if someone says, I’m trying to understand this, that’s who I love working with. I just have a good grasp on how python works and I can pretty much manipulate it to do anything. It’s to a point where everything just feels like creating another language within a language, so I make it short and sweet, but then need to make it less convoluted, and by convoluted, I mean one-liners that are not pythonic.

1 Like