Each value in sheet 1 and 2 have characteristics that can take on different numeric values. I want to check if there is an object from sheet 2 with specific characteristics in sheet1

l1= [[a,b,c,x,y,z],[a,b,c,x,y,z],[a,b,c,x,y,z],...] l2= [z,b,c] while True: if ... break 

In the if condition, I need to check if there is an object from the second sheet in the first sheet with specific values ​​of the parameters z and b, the value of c does not matter in the search. How can this be done?

Update

L1 is a sheet consisting of cards and each variable is a cocnret characteristic of the inherent map, for example, the beginning of the path (z), the end of the path (b), the number of cards of this (c) type, etc.

L2 is a sheet that is updated and each time this sheet is assigned one card with only 3 of 6 characteristics: beginning, end and number.

Based on the first two variables at the beginning (z) and end (b), you need to check if there is a card with the same parameters in the stack of cards. Count does not matter.

Update 2

Let's say

 l2 = [1,4,20] 

How to get rid of all l1 and check if there is a card with the same values ​​of z=1 and b=4 as for a card from l2 ?

  • What exactly did you try to do, and where did the difficulties arise? - Xander
  • And by the way, for what purpose is such a strange data structure chosen for l1 - a tuple from lists? It would be more logical to the contrary - a list of tuples. - Xander
  • In order for l1 to be a list, everything that to the right of the equal sign in the first line needs to be wrapped in another pair of square brackets. And so this is not a list, but a tuple. However, this decision has little effect. You did not answer - what exactly you can not. Without this clarification, we will not be able to help you. - Xander

5 answers 5

It is difficult to grasp the true meaning of the question. All confused.
Just in case I do two options as I understood.

  • res1 - if you need to find the same z , b , c
  • res2 - if you need to find the same z , b

Code:

 l1= [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]] l2= [18,14,15] res1 = [ x for x in l1 if l2 == x[5:6]+x[1:3] ] res2 = [ x for x in l1 if l2 == x[5:6]+x[1:2] ] 
    1. To iterate through the list l1, use the for ... in ... construct

    For example:

     for item in l1: здесь выполняем проверку на совпадение 
    1. To get only the necessary parameters from the list of each card, use indices, like this:

      l2 [0]

    Such a construction will extract from the list l2 only the zero parameter (in programming, the count goes not from the first element, but from the zero, and only then goes the first, second, etc.)

    1. To compare two parameters, use the if branch operator and the comparison operator ==

    How to use them, you can easily find in Google.

    In general, I would advise you to read at least the most basic, before trying to solve specific problems.

      Suppose l2 = [1,4,20] How can I l2 = [1,4,20] rid of all l1 and check if the card has the same values ​​of z=1 and b=4 like the card from l2 ?

      For readability, let's turn the values ​​into l1 , l2 into collections.namedtuple :

       #!/usr/bin/env python from collections import namedtuple Map = namedtuple('Map', 'a,b,c,x,y,z') l1 = [[a,b,c,x,y,z],[a,b,c,x,y,z],[a,b,c,x,y,z],...] l2 = [1, 4, 20] z, b = l2[:2] # reference found_maps = [m for m in map(Map, l1) if z == mz and b == mb] 

      If it suffices to find only one suitable card:

       found = next((m for m in map(Map, l1) if z == mz and b == mb), None) 

      The search ends on the first element found, without going through the entire l1 list.

      In both cases, if found can be used to check if at least one item is found.

      • Maybe I’ll ask for stupidity now, because I haven’t worked with namedtuple before, but still - how does your code take into account that l2 and elements from l1 do not match in structure? In l2, some elements are missing, and they go in a different order. And I don’t understand how the Map (* l2) can work correctly for it - Xander
      • @Alexander: truly noticed — the code as it was written did not work. Corrected. - jfs

      It seems to me that the task is rather trivial, it’s just that the author has made too many confusing descriptions. In fact, we need to go through the lists from the list, and find the one in which the desired values ​​are present. It is solved quite simply by a cycle and a condition:

       l1 = [[1,4,2,7,3,5],[1,8,9,2,5,3,7],[3,2,1,5]] res = None for item in l1: if 4 in item and 7 in item: res = item 

      The result will be res = [1,4,2,7,3,5]

      In the author's definitions, the cycle will look something like the following (for clarity, I took the individual logical expressions into brackets):

       for item in l1: if (l2[0] in item) and (l2[1] in item): res = item 

      If you need to find all entries in the list, then you need to declare the variable res as a list and replace the add operation with adding an element:

       res = [] for item in l1: if (l2[0] in item) and (l2[1] in item): res.append(item) 

        Try using SET

         set1 = set(l1) set2 = set(l2) set1.interconnection(set2) set2 & set2