Bottom Page

• 1 Vote(s) - 4 Average
• 1
• 2
• 3
• 4
• 5
 Check if multiple values exist in a list glidecode Silly Frenchman Posts: 27 Threads: 9 Joined: Oct 2016 Reputation: 0 Likes received: 1 #1 Oct-06-2016, 06:50 PM I am trying to find a way of testing whether or not at least one element from a list #1 is present in a list #2 One thing I've found is this thread: http://stackoverflow.com/questions/74028...-in-a-list though I dont' really understand the first(accepted) answer. If someone could break that answer down or suggest an alternative way of doing it, that would be a big help.  It would look like: mylist = [1,2,3,4] mylist2 = [4,5,6] if (any(mylist) in mylist2): wavic So-and-so of the Yard Posts: 2,898 Threads: 39 Joined: Sep 2016 Reputation: 89 Likes received: 366 #2 Oct-06-2016, 07:07 PM If the list values are uniques is better to use sets. http://www.programiz.com/python-programming/set ```for value in list1:     if value in list2:         print(value, "True")    else:        print(value, "False") ``` "As they say in Mexico 'dosvidaniya'. That makes two vidaniyas." https://freedns.afraid.org Ofnuts Norwegian Blue Posts: 687 Threads: 37 Joined: Sep 2016 Reputation: 24 Likes received: 218 #3 Oct-06-2016, 07:20 PM The SO answer is making sets from the lists. In Python a set is a collection without duplicate elements: ```>>> set('abracadabra') set(['a', 'r', 'b', 'c', 'd'])```These sets mimic the mathematical objects of the same name on which you can use the intersection operator to determine the elements that two sets have in common. Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V. Your one-stop place for all your GIMP needs: gimp-forum.net wavic So-and-so of the Yard Posts: 2,898 Threads: 39 Joined: Sep 2016 Reputation: 89 Likes received: 366 #4 Oct-06-2016, 07:28 PM Why this code doesn't work? ```print(value, "True") for value in list1 if value in list2 ``` "As they say in Mexico 'dosvidaniya'. That makes two vidaniyas." https://freedns.afraid.org j.crater Posts: 1,098 Threads: 33 Joined: Sep 2016 Reputation: 44 Likes received: 158 #5 Oct-06-2016, 07:30 PM (Oct-06-2016, 07:20 PM)Ofnuts Wrote: The SO answer is making sets from the lists. In Python a set is a collection without duplicate elements: ```>>> set('abracadabra') set(['a', 'r', 'b', 'c', 'd'])```These sets mimic the mathematical objects of the same name on which you can use the intersection operator to determine the elements that two sets have in common. Therefore, if you want to know whether sets (or lists, turned to sets) have any elements in common, you want to check if intersection of the sets is an empty set or not. Yoriz Posts: 1,240 Threads: 28 Joined: Sep 2016 Reputation: 120 Likes received: 283 #6 Oct-06-2016, 08:55 PM (This post was last modified: Oct-06-2016, 08:56 PM by Yoriz. Edited 1 time in total.) (Oct-06-2016, 07:28 PM)wavic Wrote: Why this code doesn't work? ```print(value, "True") for value in list1 if value in list2 ``` Because it is not valid python code ```mylist = [1,2,3,4] mylist2 = [4,5,6] print([(value, "True") for value in mylist if value in mylist2])`````````Output:[(4, 'True')] `````` glidecode Silly Frenchman Posts: 27 Threads: 9 Joined: Oct 2016 Reputation: 0 Likes received: 1 #7 Oct-06-2016, 10:21 PM Thanks for all of the answers guys. Good start for me here on the forum! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post How to pass multiple values from one sample to nc variable? Baloch 0 83 Jun-01-2020, 09:27 PM Last Post: Baloch Inserting values from multiple lists sqlite azulu 1 252 May-24-2020, 08:40 AM Last Post: ibreeden UnUnloading values from multiple widgets in a container UGuntupalli 3 245 Apr-20-2020, 08:53 PM Last Post: UGuntupalli Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 306 Apr-10-2020, 02:39 PM Last Post: buran Multiple lambda functions in zipped list not executing psolar 0 155 Feb-13-2020, 12:53 PM Last Post: psolar Randomly assign values in List 1 to a value in List 2 Seneca260 7 782 Dec-16-2019, 12:57 PM Last Post: BamBi25 Syntax not exist noorpy 1 214 Nov-14-2019, 09:23 AM Last Post: perfringo How do I map a list of values to specified colors? larkypython 4 395 Nov-05-2019, 09:22 AM Last Post: larkypython Save all values to pandas of multiple classes jenniferruurs 0 258 Sep-13-2019, 12:10 PM Last Post: jenniferruurs get value if it's exist [python] senait 1 302 Aug-21-2019, 06:47 AM Last Post: buran

Forum Jump:

Users browsing this thread: 1 Guest(s)