我在这里没有想法。 我有:
d1 = {1:30, 2:20, 3:30, 5:80}
d2 = {1:40, 2:50, 3:60, 4:70, 6:90}
我想创建:
d3 = {4:70, 5:80, 6:90}
我意识到这是多么微不足道,因此我感到沮丧。如果您有任何想法,请分享。
答案 0 :(得分:2)
对称差异^
找到唯一键。然后它是一个简单的词典理解:
>>> {k: d1.get(k, d2.get(k)) for k in (set(d1) ^ set(d2))}
{4: 70, 5: 80, 6: 90}
上面的方法是交叉比较代码。如果你不需要处理Python 2,它看起来会更好一些:
>>> from collections import ChainMap
>>> d12 = ChainMap(d1, d2)
>>> {k: d12[k] for k in d1.keys() ^ d2.keys()}
{4: 70, 5: 80, 6: 90}
注意:键"查看"这里的对象也可以在方法d.viewkeys()
下的Python 2中获得。
答案 1 :(得分:1)
<?php $mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "test";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_query("SET NAMES UTF8");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
require_once './Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$query = "SELECT * FROM testing";
$result = mysql_query($query) or die(mysql_error());
//$objPHPExcel = new PHPExcel();
$rowCount = 2;
while($row = mysql_fetch_array($result)){
$objPHPExcel->getActiveSheet()->SetCellValue('A1','ID');
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['id']);
$objPHPExcel->getActiveSheet()->SetCellValue('B1','name');
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['name']);
$objPHPExcel->getActiveSheet()->SetCellValue('C1','file_path');
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['file_path']);
$objPHPExcel->getActiveSheet()->SetCellValue('D1','date');
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $row['date']);
$objPHPExcel->getActiveSheet()->getCell('C'.$rowCount, $row['file_path'])->getHyperlink()->setUrl();
$rowCount++;
}
ob_end_clean();
header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Test_list.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>