我有一个字典function twentynineteen_scripts() {
wp_enqueue_style( 'twentynineteen-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
wp_enqueue_script( 'twentynineteen-customControl', get_theme_file_uri( '/js/customize-controls.js' ), array('jquery'), '1.1', true );
wp_localize_script( 'customize-controls-script', 'php_obj',
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' )
)
);
}
add_action( 'wp_enqueue_scripts', 'twentynineteen_scripts' );
add_action( 'wp_ajax_calendar_events', 'calendar_events_callback' );
add_action( 'wp_ajax_nopriv_calendar_events', 'calendar_events_callback' );
function calendar_events_callback() {
exit();
}
的列表,该字典具有相同的两个键,每个键在每个字典中具有不同的值。我希望它们以单一字典lst
的形式出现
其中两个键的所有值都列在列表中。请记住,键应从变量dict
和dimLst
中获取,否则它们是未知的。有什么想法吗?
metricLst
我希望输出像:
lst = [
{'Location': 'ANNEX', 'ToTal_Dwell': 60.0},
{'Location': 'CHENNAI', 'ToTal_Dwell': 86.0},
{'Location': 'DADRI', 'ToTal_Dwell': 108.0},
{'Location': 'JNPT', 'ToTal_Dwell': 39.0},
{'Location': 'KOLKATA', 'ToTal_Dwell': 67.0},
{'Location': 'MUNDRA', 'ToTal_Dwell': 82.0}
]
dimLst = ['Location']
metricsLst = ['ToTal_Dwell']
答案 0 :(得分:2)
使用字典理解:
>>> {k: [d[k] for d in lst] for k in lst[0]}
{'Location': ['ANNEX', 'CHENNAI', 'DADRI', 'JNPT', 'KOLKATA', 'MUNDRA'], 'ToTal_Dwell': [60.0, 86.0, 108.0, 39.0, 67.0, 82.0]}
答案 1 :(得分:1)
这是除字典理解之外的另一种解决方案
lst = [
{'Location': 'ANNEX', 'ToTal_Dwell': 60.0},
{'Location': 'CHENNAI', 'ToTal_Dwell': 86.0},
{'Location': 'DADRI', 'ToTal_Dwell': 108.0},
{'Location': 'JNPT', 'ToTal_Dwell': 39.0},
{'Location': 'KOLKATA', 'ToTal_Dwell': 67.0},
{'Location': 'MUNDRA', 'ToTal_Dwell': 82.0}
]
locationList = []
dwellList = []
locDict = {}
for i in lst:
locationList.append(i['Location'])
dwellList.append(i['ToTal_Dwell'])
locDict['Location'] = locationList
locDict['ToTal_Dwell'] = dwellList
print(locDict)