0

Edit: I was using Jupyter notebook, I had two different scripts in a row while working, The script shown here is one, and the error shown here is from the other script. (mistake) Thanks for your time! I intentionally learned more though.

I'm trying to find an intersection between 10000 randomly generated lists of 6 elements numbers between 1 to 49 and a single list that I wrote myself also 1 to 49...

I tried using def like in the following script:

import random
lst1 = [7, 10, 21, 35, 48, 19]
lst2 = []
for i in range(10000):
    r = random.sample(range(1, 50), 6)
    lst2.append(r)
 #HERE #(   
def intersection(lst1, lst2): 
    lst3 = [value for value in lst1 if value in lst2] 
    return lst3 #)
    
    
#print(results)   
print("------------")
print(Intersection(lst1, lst2))

but I get the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-88-0caf870aa4e6> in <module>()
     13 #print(results)
     14 print("------------")
---> 15 print(Intersection(lst1, lst2))

<ipython-input-51-a4e2d32a8078> in Intersection(lst1, lst2)
      7 
      8 def Intersection(lst1, lst2):
----> 9     return set(lst1).intersection(lst2)
     10 
     11 

TypeError: unhashable type: 'list'

Is there something I'm missing? I tried to look online but couldn't find any solutions!

Temosviii
  • 7
  • 5
  • The best way may be to use sets instead of lists, as they support that operation, e.g. [Best way to find the intersection of multiple sets?](https://stackoverflow.com/questions/2541752/best-way-to-find-the-intersection-of-multiple-sets) – Kemp Dec 07 '20 at 16:45
  • The error doesnt match the piece of code showen... – adir abargil Dec 07 '20 at 16:47
  • @kemp I looked into the link, the context in the link is related to python 2.6, I'm using python 3. According to my very modest knowledge of programming, I know that they got different approaches from each other. – Temosviii Dec 07 '20 at 18:00
  • @Temosviii That answer still applies to Python 3. https://docs.python.org/3.8/library/stdtypes.html#set – Kemp Dec 07 '20 at 18:10
  • @Kemp That is learned! Thank you for informing me! – Temosviii Dec 07 '20 at 18:20

4 Answers4

2

You can use sets:

set(list1).intersection(set(list2))
Tarik
  • 10,810
  • 2
  • 26
  • 40
  • Thank you for your response. I'm not sure where to use it exactly in the script. I'm new to programming. trying to figure out python. – Temosviii Dec 07 '20 at 17:52
1

You can use sets which is going to be a boost in speed

def intersection(lst1, lst2): 
    return list(set(lst1) & set(lst2))

Note: I used the list constructor to be consistent with your code.

>>> intersection([1,2,3,4],[3,4,5,6])
[3, 4]
>>>
dcg
  • 4,187
  • 1
  • 18
  • 32
  • Thank you for your response. The error is gone, yet it doesn't find any intersections! I'll try to work on that! – Temosviii Dec 07 '20 at 17:45
  • It doesn't calculate intersections in-place, but it does do the intersection. Look the sample output. – dcg Dec 07 '20 at 18:07
0

Turn your lists into sets and use set1.intersection(set2) for your different sets.

LCMa
  • 445
  • 3
  • 13
0

I changed list2 just to show that code is working you have some error in your code you can do something like that:

import random

lst1 = [7, 10, 21, 35, 48, 19]
lst2 = [6,7,10]
# for i in range(10000):
#     r = random.sample(range(1, 50), 6)
#     lst2.append(r)


# HERE #(
def intersection(lst1, lst2):
   lst3 = [value for value in lst1 if value in lst2]
   return lst3  # )

def Intersection(lst1, lst2):
   return set(intersection(lst2,lst1))
print(Intersection(lst1, lst2))
TanjiroLL
  • 1,354
  • 1
  • 5
  • 5
  • Yes, it works if both lists are written down by me, but the point is to make it find intersections between the numbers I wrote within the written ranges and between randomly generated numbers within the written ranges. I'll try to work on that. Thank you. – Temosviii Dec 07 '20 at 17:50
  • it works in all cases). I just modified list2 to show that it works. You can return it as it was and the code still works) – TanjiroLL Dec 07 '20 at 18:01
  • It actually worked right now with no problems... I have no idea what kind of odd error was that, in the beginning!! Thanks for clarifying, coder00! – Temosviii Dec 07 '20 at 18:08
  • glad to help you) – TanjiroLL Dec 07 '20 at 18:10
  • Though this works, `[value for value in lst1 if value in lst2]` is inefficient for large lists, as each `if value in list2` requires to iterate on the list, which is O(len(list2)) in worst case. It is much better to use sets for that (testing if a value is in a set is just O(1)), and you can even determine directly and efficiently the intersection of sets. See the other answers for more efficient solutions. – Thierry Lathuille Dec 07 '20 at 18:32
  • I know that. I just modified your own code, so that I can show you the bugs in your code) – TanjiroLL Dec 07 '20 at 18:39