Home > Mobile >  how to find a list in a bunch of lists in python
how to find a list in a bunch of lists in python

Time:10-26

I want a way to make it so that I can use list B to find out if a copy of it's self exists in list A. I want a way to make it so that I can use list B to find out if a copy of it's self exists in list A.

this is the way data a has been collected:

for i in range(len(fsa)):
    if fsa[i] < fsb[i]:
        kol.append('1')
    else:
        kol.append('0')

start = 0
end = len(fsa)
j = [ ]
for x in range(start, end, 6):
    m = fsb[x:x 6]
    t = kol[x:x 6]
    if m[0] < m[-1]:
        t.append('up')
    else:
        t.append('down')
    j.append(t)

counter = Counter(tuple(j) for j in j)
for members, count, in counter.items():
    print(list(members), count)

output:

listA = ['1', '1', '0', '1', '0', '1', 'down'] 2
['0', '0', '1', '1', '1', '1', 'up'] 2
['1', '0', '0', '1', '0', '1', 'up'] 1
['0', '0', '0', '1', '1', '0', 'up'] 2
['1', '1', '0', '0', '0', '0', 'up'] 1
['0', '0', '1', '1', '0', '1', 'down'] 1
['1', '0', '0', '0', '0', '1', 'down'] 1
['1', '1', '1', '1', '1', '1', 'up'] 1

this is how data b was collected:

for _ in range(num):
    inner = driver.find_element_by_xpath("//html/body/div[1]/div[2]/div/div/div/div[2]/div/div[2]/div[2]/div/div/div[2]/div[4]/span[1]").get_attribute("innerHTML")
    print(inner)
    lok.append(inner)
    time.sleep(20)#the hour
print(lok)

lokk = []
num = 7
for _ in range(num):
    inner = driver.find_element_by_xpath("//html/body/div[1]/div[2]/div/div/div/div[2]/div/div[2]/div[2]/div/div/div[2]/div[4]/span[1]").get_attribute("innerHTML")
    print(inner)
    lokk.append(inner)
    time.sleep(20)
print(lokk)

output:

listB = ['1', '1', '1', '1', '1', '1']

list A also shows how many times that particular list has appeared

so I want a way to first find the repeating list of list B in list A, secondly to select the one with the most repetitions in the case that there are multiple versions of it.

I tried a bunch of things but non really helped as i am still quite new at coding

CodePudding user response:

As many pointed out, the code provided is not properly formatted, so I made some assumptions. Here is a half-solution to get you unstuck, try to modify this to get what you want. You will learn more by modifying this code than if I were to give you the final solution.

from collections import Counter  # for comparing lists' content
from typing import List  # for annotations

listA = [
  ['1', '1', '0', '1', '0', '1', 'down'],
  ['0', '0', '1', '1', '1', '1', 'up'],
  ['1', '0', '0', '1', '0', '1', 'up'],
  ['0', '0', '0', '1', '1', '0', 'up'],
  ['1', '1', '0', '0', '0', '0', 'up'],
  ['0', '0', '1', '1', '0', '1', 'down'],
  ['1', '0', '0', '0', '0', '1', 'down'],
  ['1', '1', '1', '1', '1', '1', 'up'],
]

listB = ['1', '1', '1', '1', '1', '1']

def find_match(metrix: List[List[str]], list_: List[str]) -> List[List[str]]:
  list_counter = Counter(list_)

  # Solution 1: Pythonic solution
  matches = [match for match in metrix if Counter(match[:-1]) == list_counter]

  # Solution 2: beginner friendly solution
  # matches = []
  # for m_list in metrix:
  #   if Counter(m_list[:-1]) == list_counter:
  #     matches.append(m_list)
  return matches

# Complexities
# if n == metrix.length and m == metrix[0].length; then
# Time: O(nm);
# Space: O(nm);

print(find_match(listA, listB))
# outputs: [['1', '1', '1', '1', '1', '1', 'up']]

CodePudding user response:

Here's something that works. Not sure if it's exactly what you want, but you can modify it to fit your needs.

Note that it assumes listB has the same length as the first part of each of listA elements (before "up"/"down").

listA =[ 
        ['1', '1', '0', '1', '0', '1', 'down', 2],
        ['0', '0', '1', '1', '1', '1', 'up', 2],
        ['1', '0', '0', '1', '0', '1', 'up', 1],
        ['0', '0', '0', '1', '1', '0', 'up', 2],
        ['1', '1', '0', '0', '0', '0', 'up', 1],
        ['0', '0', '1', '1', '0', '1', 'down', 1],
        ['1', '0', '0', '0', '0', '1', 'down', 1],
        ['1', '1', '1', '1', '1', '1', 'up', 1]
        ]
    
listB = ['1', '1', '1', '1', '1', '1']

listC = ['0','0','0','0','0','0']


def find_list(list1,list2):
    index = -1
    occ = 0
    for i,l in enumerate(list2):
        if l[:len(list1)]==list1:
            if l[-1]>occ:
                index = i
                occ = l[-1]
    if index == -1:
        return "The 1st list doesn't appear in the 2nd one."
    else:
        return f"The 1st list appears in the 2nd one at index {index} with a number of occurences equal to {occ}."
    
    
print(find_list(listB, listA))

# The 1st list appears in the 2nd one at index 7 with a number of occurences equal to 1.

print(find_list(listC, listA))

# The 1st list doesn't appear in the 2nd one.
  • Related