dictionary

Storing Functions in Dictionary [Python]

爷,独闯天下 提交于 2021-02-08 12:46:48
问题 I'm currently building an application where I need to iterate over a series of steps that do largely the same thing, save a very small amount of code (~15 lines). The number of steps will vary depending on how the project is configured, so it seems kind of silly for me to create a separate function for each potential instance. In JavaScript, I would do something like this: var switches = [true, true, false, true]; var holder = { 0: function() { /* do step0 */ } 1: function() { /* do step1 */

Dictionary that covert a string to OTAN phonetic alphabet

你离开我真会死。 提交于 2021-02-08 12:11:25
问题 The user enter a word that which is converted to OTAN phonetic alphabet. I have to use a dictionary and I put this code: def otan(): dictionary = {'A':'Alpha', 'B':'Bravo','C':'Charlie', 'D':'Delta', 'E':'Echo', 'F':'Foxtrot', 'G':'Golf', 'I':'India', 'J':'Juliet', 'K':'Kilo', 'L':'Lima', 'M':'Mike', 'N':'November', 'O':'Oscar', 'P':'Papa', 'Q':'Quebec', 'R':'Romeo', 'S':'Sierra', 'T':'Tango', 'U':'Uniform', 'V':'Victor', 'W':'Whiskey', 'X':'Xray', 'Y':'Yankee', 'Z':'Zulu'} input('Enter a

Dictionary that covert a string to OTAN phonetic alphabet

 ̄綄美尐妖づ 提交于 2021-02-08 12:11:03
问题 The user enter a word that which is converted to OTAN phonetic alphabet. I have to use a dictionary and I put this code: def otan(): dictionary = {'A':'Alpha', 'B':'Bravo','C':'Charlie', 'D':'Delta', 'E':'Echo', 'F':'Foxtrot', 'G':'Golf', 'I':'India', 'J':'Juliet', 'K':'Kilo', 'L':'Lima', 'M':'Mike', 'N':'November', 'O':'Oscar', 'P':'Papa', 'Q':'Quebec', 'R':'Romeo', 'S':'Sierra', 'T':'Tango', 'U':'Uniform', 'V':'Victor', 'W':'Whiskey', 'X':'Xray', 'Y':'Yankee', 'Z':'Zulu'} input('Enter a

How do I append a value to dict key? (AttributeError: 'str' object has no attribute 'append')

天大地大妈咪最大 提交于 2021-02-08 10:13:58
问题 Say I have a dictionary with one key (and a value): dict = {'key': '500'}. Now I want to add a new value '1000' to the same key. However, dict[key].append('1000') just gives me "AttributeError: 'str' object has no attribute 'append'". If I do dict[key] = '1000' it replaces the previous value. I'm guessing I have to create a list as a value and somehow append that list as the key's value but I'm not sure how I would go about this. Thanks for any help! 回答1: I suggest the usage of a defaultdict

Split dictionary based on values

﹥>﹥吖頭↗ 提交于 2021-02-08 10:13:50
问题 I have a dictionary: data = {'cluster': 'A', 'node': 'B', 'mount': ['C', 'D', 'E']} I'm trying to split the dictionary data into number of dictionaries based on values in key mount . I tried using: for value in data.items(): print(data) But I get this: data = {'cluster': 'A', 'node': 'B', 'mount': ['C', 'D', 'E']} data = {'cluster': 'A', 'node': 'B', 'mount': ['C', 'D', 'E']} data = {'cluster': 'A', 'node': 'B', 'mount': ['C', 'D', 'E']} Actually, I would like to get: data = {'cluster': 'A',

How to find combinations of lists from nested dictionaries

☆樱花仙子☆ 提交于 2021-02-08 09:23:08
问题 Consider a nested dictionary: d1 = {'key': {'r1': [1,2,3], 'r2': [5,6]} } I want to extract a list of dictionaries from the above dict which takes all combinations of the nested dictionary's list items. For example, for the above dict, I would want this: ans = [ {'root': 'key', 'r1': 1, 'r2':5}, {'root': 'key', 'r1': 1, 'r2':6}, {'root': 'key', 'r1': 2, 'r2':5}, {'root': 'key', 'r1': 2, 'r2':6}, {'root': 'key', 'r1': 3, 'r2':5}, {'root': 'key', 'r1': 3, 'r2':6} ] I can do this manually for

Querying a Dictionary with a Lambda Expression

本小妞迷上赌 提交于 2021-02-08 09:16:07
问题 I've created some sample code below, and am trying to use a lambda expression to query against the SoftwareComponents Dictionary. The problem is that the query returns a var of type IGrouping, when what I need to do is further refine the query so that it returns a type of IGrouping where the first string is the SoftwareComponent.ComponentName, and the second string is the SoftwareComponent.ComponentDescription. Anyone know how to do this? I was hoping the data returned would look something

How to add a Map column to Spark dataset?

做~自己de王妃 提交于 2021-02-08 09:15:43
问题 I have a Java Map variable, say Map<String, String> singleColMap . I want to add this Map variable to a dataset as a new column value in Spark 2.2 (Java 1.8). I tried the below code but it is not working: ds.withColumn("cMap", lit(singleColMap).cast(MapType(StringType, StringType))) Can some one help on this? 回答1: You can use typedLit that was introducted in Spark 2.2.0 , from the documentation: The difference between this function and lit is that this function can handle parameterized scala

Querying a Dictionary with a Lambda Expression

点点圈 提交于 2021-02-08 09:13:07
问题 I've created some sample code below, and am trying to use a lambda expression to query against the SoftwareComponents Dictionary. The problem is that the query returns a var of type IGrouping, when what I need to do is further refine the query so that it returns a type of IGrouping where the first string is the SoftwareComponent.ComponentName, and the second string is the SoftwareComponent.ComponentDescription. Anyone know how to do this? I was hoping the data returned would look something

Expression表达式目录树

…衆ロ難τιáo~ 提交于 2021-02-08 08:59:43
一、初识Expression 源码 1、在上一篇我们讲到了 委托 (忘记了可以在看看,点赞在看养成习惯),今天要讲的Expression也和委托有一点点关系吧(没有直接关系,只是想要大家看看我其他的文章),Expression是.NET准备为Linq to Sql准备的,它的命名空间是System.Linq.Expressions 2、不知道大家有没有用户ORM(对象映射实体)的数据访问层框架,使用过的小伙伴我相信对下面的伪代码不会陌生,我们在Where中传入的就是Expression<Func<TSource, bool>> predicate 3、我们进入Expression一看究竟,我们可以看到Expression<Func<TSource, bool>>里面有一些方法(后面会慢慢道来),最终继承LambdaExpression 4、我们继续进入LambdaExpression,我们看到了一些属性(这些就是我们lambda的组成的方法和属性),但是最终还是看到继承了Expression   5、继续一鼓作气进入Expression,到这里我们看到了最终的基类它里面也有很多方法,要说的话这两天都说不完,我们就简单的介绍一些常用的 二、循序渐进 1、大家可能看了上面还有一点点蒙,不急我们继续,我们看下面的实际操作,我们可以看到我们创建一个Expression和一个委托