将dict1的值插入dict2但在dict2中的特定位置

时间:2018-06-18 09:23:47

标签: python python-3.x dictionary-comprehension

我有2个字典,我想将dict1的值插入dict2但是在dict2中的特定位置,即:

dict1

{
'apple': 'hard tasty', 'orange': 'soft tasty', 'banana': 'soft very-tasty'
}

dict2

{
 'apple': '<div class="apple"></div>',
 'orange': '<div class="orange"></div>',
 'banana': '<div class="banana"></div>'
 }

我想将dict1的值插入到&#39; class =&#39;中的dict2中。变量因此变为

所需

{
 'apple': '<div class="apple hard tasty"></div>',
 'orange': '<div class="orange soft tasty"></div>',
 'banana': '<div class="banana soft very-tasty"></div>'
 }

你能帮我解决这个问题吗?我可能必须导入re或使用正则表达式。我一直在使用dict理解来进行迭代

1 个答案:

答案 0 :(得分:3)

不需要Public function availableTimeSlots($start_time, $end_time, $bookedSlots, $timePerSlot) { $start = DateTime::createFromFormat('Y-m-d H:i:s', $start_time);//createdate time objects $end = DateTime::createFromFormat('Y-m-d H:i:s', $end_time); //create date time objects $count = 0; //number of slots $out = array(); //array of slots for ($i = $start; $i <= $end;) { //for loop $avoid = false; //booked slot? $time1 = $i->format('H:i'); //take hour and minute $i->modify("+" . $timePerSlot . " minutes"); //add time per slot minutes $time2 = $i->format('H:i'); //take hour and minute $slot = $time1 . "-" . $time2; //create a format 12:40-13:00 etc for ($k = 0; $k < sizeof($bookedSlots); $k++) { //if booked hour if ($bookedSlots[$k] == $slot) { //check $avoid = true; //yes. booked } } if (!$avoid && $i <= $end) { //if not booked and less than end time $count++; //add count $slots = ['start' => $time1, 'stop' => $time2]; //add count array_push($out, $slots); //add slot to array } } return $out; } 或任何其他导入。请改用str.replace

regex