我正在尝试比较由用户输入和模式匹配动态生成的不同数量和长度的列表。我没有包含所有匹配的代码,但你应该明白我想要做什么。
根据另一个 Stack Overflow 帖子的建议,我使用了“列表列表”。我已经使用用户输入的查询数量来命名列表并访问它们。
在程序结束时,我想在列表之间进行一些比较,但我不知道如何做到这一点。首先,我只想比较列表元素并在所有列表中找到匹配的元素,但是我还想在以后执行其他列表比较。一旦我在 'for query in dom_queries' 循环之外,我就是不知道如何访问单个列表。
我超级卡住了,真的很感激一些帮助!
谢谢,
# set dom_count and initialise query_list
dom_count = 0
dom_queries = []
# get the number of query domains
domain_number = raw_input('How many domains do you want to find intersects for? ')
# Grab query ID's
while dom_count < int(domain_number):
dom_count += 1
query_domain = raw_input('domain ID query ' + str(dom_count) + ': ')
dom_queries.append(query_domain)
# initialise lists for query_matches
list_of_lists = []
for i in range(len(dom_queries)):
list_of_lists.append( [] )
list_pos = 0
# do some matching here for each dom_query, incrementing list position for each query
# and put matches into the list
for query in dom_queries:
some_match = re.search(r'XYZ',some_line)
list_of_lists[int(list_pos)].append(some_match.group())
list_pos += 1
# HERE IS WHERE I'M STUCK!!!
# I would like to compare all list's generated and find list entries
# that exist in each list (can be any number of lists with different lengths).
for i in range (len(dom_queries)):
common = list(set(list_of_lists[i] & .... \/^.^\/ ??