I have one list of lists list1 and one list to check list2 that I'd like to run a for loop to check if elements from list2 are in list1, then append the element present to 'models' which is a list that pulls the first index from each list in list1.
No errors but I believe it might be because of the types I'm working with.
My code:
a = "sample.txt" # Contains multiple rows like "model1//h//4385////ffm1//gg5////lmm"
file1 = open(a, "r")
with open(a, 'r') as file:
lines = [line.rstrip('\n') for line in file]
cleaned_data = []
def split_lines(lines, delimiter, remove='^[0-9.]+$'):
for line in lines:
tokens = line.split(delimiter)
tokens = [re.sub(remove, "", token) for token in tokens]
clean_list = list(filter(lambda e: e.strip(), tokens))
cleaned_data.append(clean_list)
split_lines(lines, "/")
print(cleaned_data)
# outputs: ["model1", "h", "ffm1", "gg5", "1mm"] for each line in txt
##cleaned_data = [["model1", "h", "ffm1", "gg5", "1mm"], then more of this
## in a list of lists
list2 = ['gg5', 'pp6', 'gg7'] # to check element in the cleaned_data
models = []
for item in cleaned_data:
if item[0] not in models:
models.append(item[0])
## models = ['model1', 'model2', 'model3']
for item in cleaned_data:
if item == insulation:
models.append(item)
print(models)
#This is where I want to check if elements in list2 are in the list of lists #in cleaned_data, then append that elements from list2 to index1 of models.
Output:
['model1', 'model2', 'model3']
Expected output:
[['model1', 'gg5'], ['model2', 'pp6'], ['model3', 'gg5']