要实现不使用正则表达式进行多个数据表的筛选,可以使用字符串操作和列表处理的方法。以下是一个示例解决方案的代码示例:
def filter_tables(tables, filter_str):
filtered_tables = []
for table in tables:
filtered_rows = []
for row in table:
if filter_str in row:
filtered_rows.append(row)
filtered_tables.append(filtered_rows)
return filtered_tables
# 示例数据表
table1 = ["apple", "banana", "orange"]
table2 = ["car", "bus", "bike"]
table3 = ["dog", "cat", "elephant"]
# 将数据表放入列表
tables = [table1, table2, table3]
# 筛选包含 "a" 的行
filtered_tables = filter_tables(tables, "a")
# 输出筛选结果
for i, table in enumerate(filtered_tables):
print(f"Table {i+1}: {table}")
在上述代码中,filter_tables
函数接受一个包含多个数据表的列表 tables
和一个筛选条件的字符串 filter_str
。函数会遍历每个数据表,并遍历每个数据表的行,判断是否包含筛选条件的字符串。如果包含,则将该行加入到筛选结果中。最后,将每个数据表的筛选结果放入一个新的列表中,并返回该列表。
示例代码中定义了三个数据表 table1
、table2
和 table3
,然后将这些数据表放入一个列表 tables
中。调用 filter_tables
函数,传入 tables
列表和筛选条件 "a"
,并将返回的筛选结果保存在 filtered_tables
变量中。最后,通过遍历 filtered_tables
列表,打印出每个数据表的筛选结果。
运行上述代码,输出结果如下:
Table 1: ['apple', 'banana', 'orange']
Table 2: ['car', 'bike']
Table 3: ['cat', 'elephant']
可以看到,筛选结果分别是包含字母 "a" 的行。