यदि आपको बहुत सारे चर मिल गए हैं और आप अपनी शब्दकोश समझ को हार्डकोड नहीं करना चाहते हैं, तो यह एक तरीका है।
नोट: आपको सभी चर घोषित करने की आवश्यकता है।
इसके अलावा आपको चर नामों की सूची घोषित करने की आवश्यकता है।
list_of_var_names = ['triangles', 'circles', 'squares']
dict(zip(index, [dict(zip(list_of_var_names, i))
for i in (globals().get(i) for i in list_of_var_names)]))
और चरण दर चरण विभाजित करने के लिए:
In [1]: index = [1,2,3]
...:
...: triangles = [4,5,6]
...: circles = [7,8,9]
...: squares = [10,11,12]
...:
In [2]: list_of_var_names = ['triangles', 'circles', 'squares']
In [3]: [globals().get(i) for i in list_of_var_names] # getting list of variable values in list_of_var_names order
Out[3]: [[4, 5, 6], [7, 8, 9], [10, 11, 12]]
In [4]: [dict(zip(list_of_var_names, i)) for i in (globals().get(i) for i in lis
...: t_of_var_names)]
Out[4]:
[{'circles': 5, 'squares': 6, 'triangles': 4},
{'circles': 8, 'squares': 9, 'triangles': 7},
{'circles': 11, 'squares': 12, 'triangles': 10}]
In [5]: dict(zip(index, [dict(zip(list_of_var_names, i))
...: for i in (globals().get(i) for i in list_of_var_names)]
...:))
...:
Out[5]:
{1: {'circles': 5, 'squares': 6, 'triangles': 4},
2: {'circles': 8, 'squares': 9, 'triangles': 7},
3: {'circles': 11, 'squares': 12, 'triangles': 10}}
मैं एक बार और उल्लेख करना चाहते हैं इस समाधान है कि अगर अच्छा है अगर आप चर का एक टन हो और आप स्पष्ट रूप से नहीं करना चाहती dict समझ घोषित । अन्य मामलों में यह यहां प्रस्तुत किए गए अन्य समाधानों का उपयोग करने के लिए अधिक उपयुक्त और अधिक पठनीय होगा।
हमें बताएं कि आप अब तक की कोशिश की? –