我的代码如下:
for user,score in data:
if user in res.keys():
res[user] += [score]
else:
res[user] = [score]
Run Code Online (Sandbox Code Playgroud)
其中 data 是按如下方式排列的列表的列表:
data = [["a",100],["b",200],["a",50]]
Run Code Online (Sandbox Code Playgroud)
我想要的结果是:
res = {"a":[100,50],"b":[200]}
Run Code Online (Sandbox Code Playgroud)
是否可以通过单个字典理解来做到这一点?
python dictionary list-comprehension dictionary-comprehension
看看我这个
languages = ['English', 'German', 'English', 'Italian', 'Italian', 'English', 'German', 'French']
Run Code Online (Sandbox Code Playgroud)
我想从中生成一个频率表。所以我做了
freq = {}
for language in languages:
if language in freq:
freq[language] += 1
else:
freq[language] = 1
Run Code Online (Sandbox Code Playgroud)
哪个是对的
但我想在字典理解中使用 count() 来解决它。我试了好几次都错了。
我想把一个列表('a','b','c')
变成{'1':'a','2':'b','3':'c'}
d = {key: value for key, value in targets}
Run Code Online (Sandbox Code Playgroud)
将键返回为整数,我需要将其存储为字符串,以便我可以为每个键附加一个字符串,这样最终我将最终得到
{'column1','a','column2','b','column3'}
Run Code Online (Sandbox Code Playgroud) 我有这样的数据(是的,这些元组保证有5个元素):
ts = ([('a','b','c','d','e'), ('v','w','x','y','z'),
('f','g','h','i','j'), ('a','foo','bar',1,2),
('f','g','baz',1,3), ('f','g','baz',3,4)])
Run Code Online (Sandbox Code Playgroud)
我正在尝试将其解析为嵌套字典结构,如下所示:
d = {
'a': {
'b': {
'c': [('d','e')]
},
'foo': {
'bar': [(1,2)]
}
},
'f': {
'g': {
'h': [('i', 'j')],
'baz': [(1,3), (3,4)]
}
},
'v': {
'w': {
'x': [('y', 'z')]
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所拥有的; 它似乎工作正常:
>>> d = {}
>>> for t in ts:
... if t[0] not in d:
... d[t[0]] = {t[1]: {t[2]: [(t[3], t[4])]}}
... elif t[1] not in d[t[0]]: …
Run Code Online (Sandbox Code Playgroud) 我想转换成floats
的string
值(应该已经被表示为浮动原本)在以下字典:
{'a': '1.3', 'b': '4'}
Run Code Online (Sandbox Code Playgroud)
如果我尝试词典理解:
{k:float(v) for v in d.values()}
Run Code Online (Sandbox Code Playgroud)
我最终得到了dict中的第二项:
In [191]: {k:float(v) for v in d.values()}
Out[191]: {'b': 4.0}
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
python dictionary key-value python-2.7 dictionary-comprehension
说我有这个代码:
someDict = {}
for line in open("example.txt"):
key, val = line.strip().split(",")
someDict[key] = val
Run Code Online (Sandbox Code Playgroud)
example.txt是每行上的两个数据,用逗号分隔,例如:
one,un
two,deux
three,trois
Run Code Online (Sandbox Code Playgroud)
等等
这适用于制作字典,但是我很好奇这是否可以在一行中完成(例如,使用列表/字典理解).这可能,或者更简单/更简单的方法吗?
python dictionary list-comprehension dictionary-comprehension
我想key, value
使用字典理解来反转字典对,但是如果新字典对于一个键有多个值,那么它将被替换为最后一个值.
如果使用理解重复键,是否可以附加到新词典中的值?
输入:
test_di = {'a':'1', 'b':'2', 'c':'3', 'd':'2'}
Run Code Online (Sandbox Code Playgroud)
码:
{v:k for k,v in test_di.items()}
Run Code Online (Sandbox Code Playgroud)
输出此代码:
{'1': 'a', '3': 'c', '2': 'd'}
Run Code Online (Sandbox Code Playgroud)
期望的输出:
{'1': ['a'], '3': ['c'], '2': ['b','d']}
Run Code Online (Sandbox Code Playgroud) 假设 S = "Tea Lemon CoffEE cAke".lower()
{ x:y.count('aeoiu') for x in S.split() for y in 'aeoiu' if y in 'aeoiu' }
Run Code Online (Sandbox Code Playgroud)
这个输出是:
{'cake': 0, 'tea': 0, 'lemon': 0, 'coffee': 0}
Run Code Online (Sandbox Code Playgroud)
为什么它给我0而不是每个单词中的元音数量?我是python的新手,我会很感激一些提示.完全不寻找直接的答案.
使用Python,我可以做类似的事情
listOfLists = [('a', -1), ('b', 0), ('c', 1)]
my_dict = {foo: bar for foo, bar in listOfLists}
my_dict == {'a': -1, 'b': 0, 'c': 1} => True
Run Code Online (Sandbox Code Playgroud)
我知道这是字典理解.当我使用Scala查找此操作时,我发现这个难以理解的文档(双关语).
有没有惯用的方法来使用Scala做到这一点?
额外的问题:我可以过滤这个操作my_dict = {foo: bar for foo, bar in listOfLists if bar > 0}
吗?
是否可以在dict文字中使用"可选"键,而不是将它们添加到if
语句中?
像这样:
a = True
b = False
c = True
d = False
obj = {
"do_a": "args for a" if a,
"do_b": "args for b" if b,
"do_c": "args for c" if c,
"do_d": "args for d" if d,
}
#expect:
obj == {
"do_a": "args for a",
"do_c": "args for c",
}
Run Code Online (Sandbox Code Playgroud)
编辑上下文:我知道如何做逻辑:)我只是想避免if
语句,因为我的对象是一个代表声明性逻辑的大块数据,所以移动的东西有点像"意大利面条编码"的东西不是根本就是程序性的.我希望对象的值"看起来像它意味着什么"作为查询.
它实际上是一个弹性搜索查询,所以它看起来像这样:
{
"query": {
"bool": {
"must": [
<FILTER A>,
<FILTER B>, # would like to make this filter optional …
Run Code Online (Sandbox Code Playgroud)