pythonic从nest字典中调用特定键/值的方法(pythonic way of calling specific keys/values from a nest dictionary)
什么是从嵌套字典中获取特定键/值的pythonic方法?
例如,我想从这个嵌套字典中获取奇数的所有dict值:
nested_dict = {'bulldog': {'type': 3}, 'cat': {'type': 4}, 'yorkie': {'type': 11}, 'pitbull': {'type': 8}}
输出应该如下所示:
new_dict = {'bulldog': {'type': 3}, 'yorkie': {'type': 11}}
另外,如果我想只拉出具有奇数值的嵌套键,在这种情况下输出将如下所示:
new_dict2 = {'type': 3, 'type': 11}
What's a pythonic way of getting specific keys/values from a nested dictionary?
For instance, I want to get all the dict values that are odd from this nested dictionary:
nested_dict = {'bulldog': {'type': 3}, 'cat': {'type': 4}, 'yorkie': {'type': 11}, 'pitbull': {'type': 8}}
The output should look something like this:
new_dict = {'bulldog': {'type': 3}, 'yorkie': {'type': 11}}
Additionally, what if I wanted pull only the nested keys with an odd value, in which case the output would look like this:
new_dict2 = {'type': 3, 'type': 11}
原文:https://stackoverflow.com/questions/48738876
最满意答案
对于每个变量,请执行以下操作:
$minMonth = isset($_GET["minMonth"]) ? intval($_GET["minMonth"]) : 1; ... "WHERE sem.MonthNum >= " .minMonth. "
intavl调用将使$ _GET值转换为整数(如果尚未调整),这将保护您免受SQL注入安全问题的影响。 如果你需要用字符串做类似的事情,请改用mysql_escape_string()。
For each of your variables do this:
$minMonth = isset($_GET["minMonth"]) ? intval($_GET["minMonth"]) : 1; ... "WHERE sem.MonthNum >= " .minMonth. "
The intavl() call will make convert the $_GET value to an integer if it is not already, which will protect you from SQL injection security issues. If you need to do something similar with strings, use something like mysql_escape_string() instead.
相关问答
更多-
PHP SQL连接(PHP SQL concatenation)[2023-04-27]
您需要引用您的值,例如而不是 $qry[] = "RIGHT JOIN (SELECT post_id FROM wp_postmeta WHERE meta_key = '_paid_date' AND meta_value > " .$_POST['debut'] . " AND meta_value < " . $_POST['fin'] . ") AS m"; 使用 $qry[] = "RIGHT JOIN (SELECT post_id FROM wp_postmeta WHERE meta_k ... -
与SQL查询的PHP(php with SQL query)[2022-01-04]
PHP中的单引号不会评估嵌入式变量 - 您需要使用双引号才能完成此操作。 (有关详细信息,请参阅PHP 字符串手册页面的“单引号”部分。) 即: $sql = "SELECT name FROM chiled WHERE 'im' LIKE $id " ; 还是更好... $sql = 'SELECT name FROM chiled WHERE im="' . mysql_real_escape_string($id) . '"'; (因为你没有在你喜欢的地方使用% ,你可能不会试图做任何形式的模式匹配 ... -
货币美元符号:最好在SQL查询中连接,还是使用PHP?(Currency Dollar Sign: Better to concatenate in an SQL query, or use PHP?)[2022-06-12]
“这似乎是工作的两倍,因为我仍然需要获取非连接值来执行其他计算,例如获取所有行的总和。” 和你的直觉一起去吧。 为什么在你可以像普通HTML一样回显“$”时让数据库服务器工作? 我唯一可能错误的第二种方法是,无论是否有$ row ['total']产生的实际值,你都会打印出“$”... "It seems like double the work, because I will still need to fetch the non-concatenated value(s) to perform othe ... -
不要在事后添加安全性,只需切换到PDO或mysqli并使用预准备语句,这样您就不必再担心这些值了。 如果是表名或列名,则需要使用白名单。 问题的原因是您引用了表名和字段名。 如果你需要逃脱它们,请使用反引号: $store_info_query = "SELECT * FROM `".$table_name."` WHERE `column1` = '".$store_number."'"; Don't add security as an afterthought, just switch to PDO ...
-
对于每个变量,请执行以下操作: $minMonth = isset($_GET["minMonth"]) ? intval($_GET["minMonth"]) : 1; ... "WHERE sem.MonthNum >= " .minMonth. " intavl调用将使$ _GET值转换为整数(如果尚未调整),这将保护您免受SQL注入安全问题的影响。 如果你需要用字符串做类似的事情,请改用mysql_escape_string()。 For each of your variables do th ...
-
条件级联T-SQL(Conditional Concatenation T-SQL)[2023-02-05]
您需要使用CAST或CONVERT将数字数据更改为字符串: Cast( R.Quantity as VarChar(10) ) + 'x' + SM.StockDescription + ', ' + Cast( R.Quantity as VarChar(10) ) + 'x' + SM.StockDescription + ',' as New_Concat_Column 旁白:使用数据库软件和版本(例如SQL Server 2008R2)标记数据库问题通常很有帮助,并包含适用的表模式。 我假设 ... -
SQL连接查询(SQL Concatenation Query)[2023-06-09]
也许这有帮助,如果返回结果,以便PV始终在开始并删除尾随-s: select case when I_Tags like 'PV%' then I_Tags when I_Tags like '____PV%' then 'PV-'+left(I_Tags, 3) + substring(I_Tags, 7, len(I_Tags) -5 -patindex('%[^-]%', reverse(I_Tags))) end as test from ... -
在向查询添加其他字段时,您省略了逗号,并且在WHERE之前应该有一个空格(在这种情况下不是绝对必要的,因为所有的赋值都以引号结尾,但这是个好主意)。 你试图连接到mysql结果资源,而不是SQL字符串。 $update = "UPDATE e_users SET email = '$email'"; if(!empty($pass_post)) { $update .= ", pass = '$pass', salt = '$random_salt'"; } if(!empty($l ...
-
$data = mysqli_num_rows($result); 没用 在查询中使用SELECT COUNT(*)时,不应使用mysqli_num_rows函数。 你可以使用这个: $sql = "SELECT COUNT(*) FROM room_nights WHERE apartmentID= '$apartment' AND dates >= '$booking_from' AND dates < '$booking_to'"; $result = mysqli_query($conn, ...
-
SQL条件加入?(SQL Conditional Join?)[2023-05-05]
左边连接应该在这里做 select * from LOGS l left join PEOPLE p on p.peopleid = l.field and l.join = 'people' 我不确定我是否在LOGS和PEOPLE之间使用了正确的关系字段,但是通过包含一个连接子句, 其中日志类型是人,那么你可以看到有条件地返回PEOPLE条目。 当你想要从不同的表中有条件地返回时,事情变得更加复杂,因为你需要确保实体表引入的额外字段是相同的(或至少被识别为相同)。 在这种情况下,您被 ...