I am trying to understand Python's lists and Boolean phrases. I want to search two lists and find the intersection between them. list1=[1,3,5] list2=[3,5,6] if list1[i] == any in list2[:]: print(list[i]) This should return TRUE for 3 and 5, but instead is false for all values. Please help me understand why 'any in' does not work here. Note I am using Python3 so some of the syntax is a bit different.
Append in an empty list .I dont know how things work in python 3. But this is how i would do it http://dpaste.com/765415/
Returning by appending I can do, yes. It does appear that your method is better with the nested for loops, but do you have an idea why I can't use 'any in' for this search? Are lists not compatible with boolean phrases the way I used it?
Don't know how that works . I used the any() function and i got a list with just the number '1'.Lol So thats a start i guess.
Lists won't walk through themselves, and Booleans don't walk through lists. Here's your statement: if list1[i] == any in list2[:] I'm not sure if what I'm about to say is *exactly* what's going on, but it will give you an idea of why your statement doesn't really work. list1[i] == any is a Boolean statement and any in list2[:] is a Boolean statement. Let's ignore for a moment that, as your code currently stands, Python doesn't know what "i" is and what "any" is because they're undefined. Because they're both Boolean statements, they're going to be evaluated as such. Let's say list1[i] == any evaluates to be True. Then it's going to check if True is in list2[:] which is probably False unless it's a list of Booleans, which we know it's not. Using "any" as a variable is different than using the any() function, which takes a list of expressions and returns True if any of them are true. All numbers other than 0 evaluate to be True, so any([1,2,3]) is going to return True because 1,2,and 3 are all True (using the function that way doesn't mean much though). So you need some way of looping through the list. Depending on what you actually want, ajitnath's method doesn't actually produce the intersection, because the intersection shouldn't have duplicates. If list1 was [1,2,1,3] and list2 was [1,2,3,4], his program would produce [1,2,1,3] when the intesection is really just [1,2,3]. Lists are actually NOT what you would want to use if you wanted to find the intersection. You actually want to use a data type called a set. set1 = set([1,3,5]) set2 = set([3,5,6]) set3 = set1.intersection(set2) set3 would be the set (3,5). Done!
Yes in my original code I had used i in a for loop, deciding here to cut that part out. Your answers on any() vs any and any in were great! This is exactly why I came here and I appreciate it greatly! Thank you too ajitnath.
Gotcha. In the future, people can help you to the best of their ability when they know the most about your code. In general, you don't have to post *all* your code, but I'd keep for loops intact and let us know what variables are already instantiated so we don't make any false assumptions about what is or isn't working. For example, if you had posted the entire for loop, I could have given you some pointers on how specifically to tweak it. If this were a homework assignment, by the way, I wouldn't use sets because it's probably going against the spirit of the problem. But in general, for most problems, there's probably some built in method or data that will make life a lot easier. Part of the fun of Python is discovering those types and slapping your head and saying "It's all so easy now!" I remember struggling to write a palindrome checker when I was in college, but now I just need to check if word == word[::-1].
I did the same with a palindrome checker :) I'm actually doing 6.189A. Thanks again!
Join our real-time social learning platform and learn together with your friends!