Search code examples
pythonpython-3.xlistintersection

How can I choose a specific intersection element in a list? TypeError: unhashed type: 'list'


I would like to intersect two lists and print what they have in common. However, I would like to choose the specific element to intersect (e.g. [0] or [hockeymatch], [1], etc). With x[0] or y[1], my intention is not to select the entire line of the match, but to select only [hockeymatch]. I get this error:

TypeError: unhashable type: 'list'

In my code example, I would like to get this output:

'Seattle-Minnesota', 'NHL', '18:00'

So I would just like 'Seattle-Minnesota', 'NHL', '18:00', without:

  • without [1.33] and [62.0], because these numbers are different from [2.5] and [125.0] despite being contained in another 'Seattle-Minnesota', 'NHL', '18:00'
  • without 'Dallas-Arizona', 'NHL', '15:00' and 'Vancouver-Vegas', 'NHL', '20:00', because they are two different games from 'Seattle-Minnesota', 'NHL', '18:00'

IMPORTANT: This is just an example, but I will add more hockey matches. I don't want to manually print by setting only [('Seattle-Minnesota', 'NHL', '18:00')], but I would like to "AUTOMATICALLY" print any hockey match in common, even more than one match in common if there is one).

UPDATE: With x[0] or y[1], my intention is not to select the entire line of the match, but to select only [hockeymatch], for example ('Dallas-Arizona', 'NHL', '15:00 ')] or all of the other matches. I wanted a way to be able to select [hockeymatch], [number1], [number2] in the same line as the match.

In simple terms, I meant:

"considering that other hockey matches will be added in the future, look for one or more common matches (i.e. [hockeymatch]), but exclude from the intersection [number1] and [number2]"

Code:

#x = []
#x.append([[hockeymatch], [number1], [number2]])
x = [[[('Dallas-Arizona', 'NHL', '15:00')], [1.75], [87.5]],
     [('Seattle-Minnesota', 'NHL', '18:00')], [2.5], [125.0]]

#y = []
#y.append([[hockeymatch], [number1], [number2]])
y = [[[('Seattle-Minnesota', 'NHL', '18:00')], [1.33], [62.0]],
       [('Vancouver-Vegas', 'NHL', '20:00')], [0.50], [43.0]]

test = list(set(x[0]).intersection(y[0]))
print(test)

P.S: For greater clarity of the code I have added the comments of when I create the list and how I insert the elements with append


Solution

  • #x = []
    #x.append([[hockeymatch], [tournament], [number1], [number2]])
    x = [[[('Dallas-Arizona', 'NHL', '15:00')], [1.75], [87.5]],
    [('Seattle-Minnesota', 'NHL', '18:00')], [2.5], [125.0]]
    
    #y = []
    #y.append([[hockeymatch], [tournament], [number1], [number2]])
    y = [[[('Seattle-Minnesota', 'NHL', '18:00')], [1.33], [62.0]],
    [('Vancouver-Vegas', 'NHL', '20:00')], [0.50], [43.0]]
    
    t1 = []
    t2 = []
    
    for i in range(len(x)):
         if i == 0:
              t1.append(tuple(x[i][0]))
         else:
              try:
                   if len(x[i][0]) == 3:
                        t1.append(tuple(x[i]))
              except:
                   pass
    
    for i in range(len(y)):
         if i == 0:
              t2.append(tuple(y[i][0]))
         else:
              try:
                   if len(y[i][0]) == 3:
                        t2.append(tuple(y[i]))
              except:
                   pass
    
    print(list((set(t1).intersection(set(t2)))))
    
    

    You can simplify the code by using functions

    x = [[[('Dallas-Arizona', 'NHL', '15:00')], [1.75], [87.5]],
    [('Seattle-Minnesota', 'NHL', '18:00')], [2.5], [125.0]]
    
    
    y = [[[('Seattle-Minnesota', 'NHL', '18:00')], [1.33], [62.0]],
    [('Vancouver-Vegas', 'NHL', '20:00')], [0.50], [43.0]]
    
    t1 = []
    t2 = []
    
    def matches(your_l, l_used):
         for i in range(len(l_used)):
              if i == 0:
                   your_l.append(tuple(l_used[i][0]))
              else:
                   try:
                        if len(l_used[i][0]) == 3:
                             your_l.append(tuple(l_used[i]))
                   except:
                        pass
    
    matches(t1, x)
    matches(t2, y)
    
    print(list((set(t1).intersection(set(t2)))))