Reputation: 98
I have two multidimensional list I have to check if the element is in the multidimensional list My code is:
import tkinter as tk
from tkinter import filedialog
import numpy as np
import os
root= tk.Tk()
root.withdraw()
filepath =filedialog.askopenfilename(filetypes = (("trace files","*.trace"),("out files",".out")))
file=open(filepath)
file_path = file.name
ext= os.path.splitext(file_path)
readData=file.readlines()
list1=[]
list2=[]
pht=[[1,0]]
goodPred=0
badPred=0
count=0
for read in readData:
split= read.split(' ')
addr =split[0]
action= split[1]
list1.append([addr,action.strip()])
# if(len(pht)>500):
# del pht[0]
for i in range(len(list1)):
if list1[i][0] in pht ##how to check from index 1 of pht for eg:pht[##what to mention here##][1]##:
list2_index = list2.index(list1[i][0])
print(list2_index)
else:
# print(list1[i][0])
list2.append(list1[i][0])
My list1 is as follows:
[['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T'], ['3086703274', 'T']]
My Pht is as follows for eg:
[['1','3086703274'], ['1','3086703274'], ['1','3086703274',]]
How can I use pht of second element of every sublist in the list and compare it with list1[i][0]
Upvotes: 1
Views: 55
Reputation: 3455
if list1[i][0] in [val[1] for val in pht]:
If pht
is a fixed list then of course you are not going to calculate it in each loop but put [val[1] for val in pht]
in a new list like pht_keys, thus:
pht_keys = [val[1] for val in pht]
for index, element in enumerate(list1):
if element[0] in pht_keys:
list_2_index = list2.index(index)
...
I discourage the use of
for i in range(len(list1)):
print(f'element index {i} is {list1[i]}')
Do it the pythonic way with enumerate if you need the index
for index, element in enumerate(list1):
print(f'element index {index} is {element}')
Upvotes: 1