首页 \ 问答 \ pythonic从nest字典中调用特定键/值的方法(pythonic way of calling specific keys/values from a nest dictionary)

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
更新时间:2024-01-27 16:01

最满意答案

对于每个变量,请执行以下操作:

$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.

相关问答

更多
  • 您需要引用您的值,例如而不是 $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 ...
  • PHP中的单引号不会评估嵌入式变量 - 您需要使用双引号才能完成此操作。 (有关详细信息,请参阅PHP 字符串手册页面的“单引号”部分。) 即: $sql = "SELECT name FROM chiled WHERE 'im' LIKE $id " ; 还是更好... $sql = 'SELECT name FROM chiled WHERE im="' . mysql_real_escape_string($id) . '"'; (因为你没有在你喜欢的地方使用% ,你可能不会试图做任何形式的模式匹配 ...
  • “这似乎是工作的两倍,因为我仍然需要获取非连接值来执行其他计算,例如获取所有行的总和。” 和你的直觉一起去吧。 为什么在你可以像普通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 ...
  • 您需要使用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)标记数据库问题通常很有帮助,并包含适用的表模式。 我假设 ...
  • 也许这有帮助,如果返回结果,以便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, ...
  • 左边连接应该在这里做 select * from LOGS l left join PEOPLE p on p.peopleid = l.field and l.join = 'people' 我不确定我是否在LOGS和PEOPLE之间使用了正确的关系字段,但是通过包含一个连接子句, 其中日志类型是人,那么你可以看到有条件地返回PEOPLE条目。 当你想要从不同的表中有条件地返回时,事情变得更加复杂,因为你需要确保实体表引入的额外字段是相同的(或至少被识别为相同)。 在这种情况下,您被 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)