Spe*_*XCD 17 python dictionary
我试图根据字符串中有1的数目将某些数字的二进制字符串组合在一起。
这不起作用:
s = "0 1 3 7 8 9 11 15"
numbers = map(int, s.split())
binaries = [bin(x)[2:].rjust(4, '0') for x in numbers]
one_groups = dict.fromkeys(range(5), [])
for x in binaries:
one_groups[x.count('1')] += [x]
Run Code Online (Sandbox Code Playgroud)
预期的字典one_groups
需要是
{0: ['0000'],
1: ['0001', '1000'],
2: ['0011', '1001'],
3: ['0111', '1011'],
4: ['1111']}
Run Code Online (Sandbox Code Playgroud)
但是我明白了
{0: ['0000', '0001', '0011', '0111', '1000', '1001', '1011', '1111'],
1: ['0000', '0001', '0011', '0111', '1000', '1001', '1011', '1111'],
2: ['0000', '0001', '0011', '0111', '1000', '1001', '1011', '1111'],
3: ['0000', '0001', '0011', '0111', '1000', '1001', '1011', '1111'],
4: ['0000', '0001', '0011', '0111', '1000', '1001', '1011', '1111']}
Run Code Online (Sandbox Code Playgroud)
到目前为止,唯一起作用的是如果我使用one_groups[x.count('1')] = one_groups.get(x.count('1')) + [x]
而不是one_groups[x.count('1')] += [x]
但是为什么会这样呢?如果我没记错的话,不dict[key]
应该返回该字典的值,类似于如何dict.get(key)
工作?我看过这个线程,为什么用dict.get(key)而不是dict [key]?但这并没有回答我针对此特定情况的问题,因为我确定该程序并非旨在获取KeyError
我也尝试过,one_groups[x.count('1')].append(x)
但这也不起作用。
h4z*_*4z3 24
问题是可变性:
one_groups = dict.fromkeys(range(5), [])
-这会将与值相同的列表传递给所有键。因此,如果更改一个值,则全部更改。
基本上与说相同:
tmp = []
one_groups = dict.fromkeys(range(5), tmp)
del tmp
Run Code Online (Sandbox Code Playgroud)
如果要使用新列表,则需要循环执行-显式for
循环或dict理解:
one_groups = {key: [] for key in range(5)}
Run Code Online (Sandbox Code Playgroud)
这个东西将为每个键“执行” []
(等于list()
),从而使值具有不同的列表。
为什么get
起作用?因为您显式获取当前列表,但+
创建了一个新的结果列表。它不会不管它是否one_groups[x.count('1')] = one_groups.get(x.count('1')) + [x]
还是one_groups[x.count('1')] = one_groups[x.count('1')] + [x]
-要紧的是,有+
。
我知道大家都怎么说的a+=b
只是a=a+b
,但实现可以有利于优化不同-在列表中的情况下,+=
仅仅是.extend
因为我们知道我们希望在当前的变量的结果,那么创建新的清单将是浪费内存。
小智 8
问题正在使用 one_groups = dict.fromkeys(range(5), [])
(这会将与值相同的列表传递给所有键。因此,如果更改一个值,则全部更改)
您可以改用以下方法: one_groups = {i:[] for i in range(5)}
(此操作将为每个键“执行” [](等于list()),从而使值具有不同的列表。)