我有一个嵌套的for循环,如下所示:
cv_list_dic = cv_list()
for cv in cv_list_dic['results']:
cv_name = cv['name']
if qq_cv(cv_name):
cv_versions = (cv['versions'])
cv_id_list = []
for cv_ids in cv_versions:
ver_id = (cv_ids['id'])
cv_id_list.append(ver_id)
max_id = (max(cv_id_list))
qq_cv_id = (cv_name, max_id)
print(qq_cv_id)
输出是一个元组列表(有更多的元组,它只是一个例子):
('cv1', 152)
('cv2', 35)
我的问题是我需要那些for循环之外的那些元组。 我试图创建一个空列表并附加到它,但它只附加最后一项。
答案 0 :(得分:1)
我试图创建一个空列表并附加到它
这是正确的方向,所以你只需要实际编码:)
cv_list_dic = cv_list()
results = [] # here is your list with results
for cv in cv_list_dic['results']:
cv_name = cv['name']
if qq_cv(cv_name):
cv_versions = (cv['versions'])
cv_id_list = []
for cv_ids in cv_versions:
ver_id = (cv_ids['id'])
cv_id_list.append(ver_id)
max_id = (max(cv_id_list))
qq_cv_id = (cv_name, max_id)
print(qq_cv_id)
results.append(print(qq_cv_id))
答案 1 :(得分:0)
这应该有效,将cv_list_dic
定义为空列表(在循环开始之前),并将结果附加到此列表中。
cv_list_dic = cv_list()
qq_cv_id = []
for cv in cv_list_dic['results']:
cv_name = cv['name']
if qq_cv(cv_name):
cv_versions = (cv['versions'])
cv_id_list = []
for cv_ids in cv_versions:
ver_id = (cv_ids['id'])
cv_id_list.append(ver_id)
max_id = (max(cv_id_list))
qq_cv_id.append((cv_name, max_id))
print(qq_cv_id)