最初のリストにはカテゴリが含まれ、2 番目のリストにはそのカテゴリのアイテムが含まれます。
例えば:
list1 = {"vehicle", "fruits", "mobile phone", etc.....}
list2 = {"vehicle"=>"toyota", "vehicle"=>"honda", "vehicle"=>"..", "fruits"=>"orange", "fruits"=>"apple", "fruits"=>"...", "mobile phone"=>"nokia", "mobile phone"=>"samsung", "mobile phone"=>"iphone", "mobile phone"=>"...."}
したがって、この形式で表示する場合:
Category 1
-items
-items
-items
-items
Category 2
-items
-items
-items
-items
Category 3
-items
-items
-items
-items
Category n
-items
-items
-items
-items
現在、両方のリストをループしています。アイテムがこのリストに属しているかどうかを確認して表示します。これらのアイテムとカテゴリを表示するより良い方法はありますか?? これらのカテゴリを個別に表示する場合は、3 回ループする必要があります。
ここに私が意味するものの例があります。
<!--display vehicle-->
for a in list1:
for b in list2:
if a==vehicle && a == b.key:
print(a)
print(b.value)
end if
end for
end for
<!--display fruits-->
for a in list1:
for b in list2:
if a==fruits && a == b.key:
print(a)
print(b.value)
end if
end for
end for
<!--display mobile phone-->
for a in list1:
for b in list2:
if a==mobile phone && a == b.key:
print(a)
print(b.value)
end if
end for
end for