Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is pretty simple and quite obvious to do. If you want to check if any number is in any list, you do this:</p> <pre><code>def any_number_in_any_lists(numbers, lists): for alist in lists: for number in numbers: if number in alist: return True return False </code></pre> <p>If you want to check that <em>all</em> numbers are in <em>all</em> lists, you do this:</p> <pre><code>def all_number_in_all_lists(numbers, lists): for alist in lists: for number in numbers: if number not in alist: return False return True </code></pre> <p>If you want to check for <em>all</em> numbers in <em>any</em> lists, it gets more complicated. The easiest and clearer is to have two methods:</p> <pre><code>def all_numbers_in_list(l, numbers): for n in numbers: if n is not in l: return False return True def all_numbers_in_any_list(lists, numbers): for l in lists: if all_numbers_in_list(l, numbers): return True return False </code></pre> <p>And you'd use the functions above like this:</p> <pre><code>if all_number_in_all_lists([3,5,6], [list1, list2, list3]): do something else: do something else </code></pre> <p>Since you want less code, there are various "shortcuts" you can use. The shortcuts can make the code shorter, but will also make it less clear and harder to read, and hence they are not really a better choice. For example you can use the any function, and a generator expression:</p> <pre><code> def any_number_in_any_lists(numbers, lists): for alist in lists: if any(number in alist for number in numbers): return True return False </code></pre> <p>You can even nest two of them:</p> <pre><code>def any_number_in_any_lists(numbers, lists): return any(any(number in alist for number in numbers) for alist in lists) </code></pre> <p>But understanding that code takes a long time and much more brain power. It is also significantly slower, as it will go through all lists, even if it finds a match in the first one. I can't think of any way that is not slower at the moment, but there may be one. But you are unlikely to find anything that is significantly faster and not significantly more confusing.</p> <p>For the case of checking that all numbers are in all lists, you would do this:</p> <pre><code>def all_number_in_all_lists(numbers, lists): return all(all(number in alist for number in numbers) for alist in lists) </code></pre> <p>Which also, for some reason takes twice the time compared with the function above, even though there will be no shortcuts. It may have to do with the creation of intermediary lists.</p> <p>This is often the case with Python: Simple is best.</p> <p>Line by line explanation of the code you used, since you asked for it:</p> <pre><code>def all_numbers_in_list(l, numbers): </code></pre> <p>Defines a function, called "all_numbers_in_list", with the parameters "l" and "numbers".</p> <pre><code> for n in numbers: </code></pre> <p>For every item in numbers, assign variable "n" to that number, and then do the following block.</p> <pre><code> if n is not in l: </code></pre> <p>If the value of variable "n" is not in the list "l".</p> <pre><code> return False </code></pre> <p>Exit the function with False as return value.</p> <pre><code> return True </code></pre> <p>Exit the function with True as return value.</p> <pre><code>def all_numbers_in_any_list(lists, numbers): </code></pre> <p>Defines a function, called "all_numbers_in_any_list", with the parameters "lists" and "numbers".</p> <pre><code> for l in lists: </code></pre> <p>For every item in lists, assign variable "l" to that number, and then do the following block.</p> <pre><code> if all_numbers_in_list(l, numbers): </code></pre> <p>If calling the function "all_numbers_in_list" with the parameters "l" and "numbers" return True, do the following block:</p> <pre><code> return True </code></pre> <p>Exit the function with True as return value.</p> <pre><code> return False </code></pre> <p>Exit the function with False as return value.</p> <p>Did that help?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload