Reputation: 127
I have a data list like this:
[['Alice', 'Bob', 'Charlie'], [2, 8, 3], [4, 1, 5], [3, 2, 5]]
and I have a list of numbers(key):
[4, 1, 5]
I want to take this (key) and compare it against the data list if found, which in the above example is true then it will print the corresponding name to it (which is Bob) if not found then it just prints error message
How i can do that?
my code:
from sys import argv, exit
import csv
import re
import pandas as pd
argc = len(argv)
if argc != 3:
print("Invalid file(s)")
exit(1)
file = open(argv[1] , "r")
file1 = open(argv[2] , "r")
text = file1.read()
strl = []
with file:
csv = csv.reader(file,delimiter=",")
for row in csv:
strl = row[1:9]
break
df = pd.read_csv(argv[1])
data = df.loc[:, df.columns != 'name'].to_numpy().tolist()
data.insert(0, df["name"].tolist())
numberx = []
for g in range(len(strl)):
pattern = re.compile('(%s)'%strl[g])
res = pattern.findall(text)
repeats = len(res)
intm = repeats
pattern = re.compile('(%s)+'%strl[g])
res = pattern.findall(text)
repeats = len(res)
intg=repeats - 1
number = intm - intg
numberx.append(number)
in my code case the (key) is numberx
and data list is data
Upvotes: 3
Views: 683
Reputation: 195553
You can use zip()
to "tie" name to its list and then do simple ==
compare:
data = [['Alice', 'Bob', 'Charlie'], [2, 8, 3], [4, 1, 5], [3, 2, 5]]
numberx = [4, 1, 5]
for name, lst in zip(data[0], data[1:]):
if numberx == lst:
print('List found! Name = {}'.format(name))
break
else:
print('Error!')
Prints:
List found! Name = Bob
Upvotes: 6