Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 List Overlap Josh_Python890 Programmer named Tim Posts: 8 Threads: 6 Joined: Sep 2017 Reputation: 0 Likes received: 0 #1 Jun-12-2019, 12:00 AM I'm doing online problems to get me back into python after not touching it for a couple of years. Problem comes from http://www.practicepython.org/exercise/2...erlap.html Take two lists, say for example these two: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes. Extras: Randomly generate two lists to test this Write this in one line of Python (don’t worry if you can’t figure this out at this point - we’ll get to it soon) This is my attempt but it doesn't work: ```def exc5(): a = random.sample(range(20), 10) b = random.sample(range(20), 10) c = [i for i in a if a in b] print(a) print(b) print(c) exc5() ```Sample output: ``````Output: [9, 17, 6, 4, 12, 16, 18, 5, 1, 7] [4, 5, 19, 12, 9, 18, 14, 2, 8, 11] [] ``````As you can see the numbers which are in both lists should be in the third list but it's an empty list. I am aware you can do this with sets but I'm interested as to why this doesn't work. micseydel Involuntary Spiderweb Collector Posts: 2,251 Threads: 56 Joined: Sep 2016 Reputation: 63 Likes received: 676 #2 Jun-12-2019, 12:51 AM You want "if i in b" not "if a in b". Also the `in` operator in this case will not be efficient for large inputs. Happy to talk about that if you want but waiting so as to not hijack your thread. Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more. Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :) perfringo Da Bishop Posts: 1,359 Threads: 7 Joined: Jun 2018 Reputation: 137 Likes received: 300 #3 Jun-12-2019, 01:59 AM (This post was last modified: Jun-12-2019, 01:59 AM by perfringo. Edited 1 time in total.) “without duplicates” requirement directs my thoughts toward sets. Sonething like: convert lists to sets, do set operation, convert result to list. Edit: need to read more carefully, you already considered sets. My mistake. 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. micseydel Involuntary Spiderweb Collector Posts: 2,251 Threads: 56 Joined: Sep 2016 Reputation: 63 Likes received: 676 #4 Jun-12-2019, 02:47 AM Yes, sets are exactly what you'd use to improve the efficiency. Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more. Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :) kotter Programmer named Tim Posts: 10 Threads: 2 Joined: Jun 2019 Reputation: 1 Likes received: 1 #5 Jun-12-2019, 03:23 PM (This post was last modified: Jun-12-2019, 03:23 PM by kotter. Edited 1 time in total.) I believe your fourth line is wrong. You have: ```c = [i for i in a if a in b] ```But it should be: ```c = [i for i in a if i in b] ```Edit: This was already said, never mind « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post List Overlap Trouble erfanakbari1 1 525 Mar-18-2019, 02:36 PM Last Post: ichabod801

Forum Jump:

Users browsing this thread: 1 Guest(s)