位字段并集的大小,其成员数多于其大小(Size of bit-field union which has more members than its size)
#include <stdio.h> union mix { unsigned char a1:1; unsigned char a2:4; unsigned char a3:4; unsigned char a4:1; unsigned char a5:4; unsigned char a6:4; unsigned char a7:1; unsigned char a8:4; unsigned char a9:4; unsigned char a10:1; unsigned char a11:4; unsigned char a12:4; }; int main() { printf("Sizeof mix = %d bytes\n", sizeof(union mix)); return 0; }
输出为1个字节 。
超过8位的位域成员会发生什么? 显然,我仍然可以为任何位字段成员设置并获取正确的值。
UPDATE
谢谢你清除我的困惑。 一个问题:这些位以什么顺序存储在内存中? 假设它是小端存储器,那么0xabcd将作为0xd,0xc,0xb,0xa存储在存储器中。
- m.a1会成为0xd OR oxa的一部分吗?
- 它是0xd(或0xa)的第3位还是第0位?
#include <stdio.h> union mix { unsigned char a1:1; unsigned char a2:4; unsigned char a3:4; unsigned char a4:1; unsigned char a5:4; unsigned char a6:4; unsigned char a7:1; unsigned char a8:4; unsigned char a9:4; unsigned char a10:1; unsigned char a11:4; unsigned char a12:4; }; int main() { printf("Sizeof mix = %d bytes\n", sizeof(union mix)); return 0; }
The output is 1 byte.
What happens to bit-field members which exceed 8 bits ? Apparently I can still set and get correct values for any bit-field member.
UPDATE
Thank you for clearing my confusion. One side question: in what order do the bits get stored in memory ? Assuming it is little endian memory so 0xabcd will be stored in memory as 0xd, 0xc, 0xb, 0xa.
- Will m.a1 be part of 0xd OR oxa?
- Will it be bit 3 or bit 0 of 0xd(or 0xa)?
原文:https://stackoverflow.com/questions/40006207
更新时间:2024-05-03 14:05
最满意答案
这应该工作(
meds
是带有dicts的列表):tmp = {} for med in meds: name = med.pop('medication_name') tmp.setdefault(name, []).append(med) res = [{'medication_name': k, 'trend': v} for k, v in tmp.items()]
结果:
>>> res [{'medication_name': 'Avonex', 'trend': [{'timeline': '2015-Jun', 'total_prescriptions': 1}, {'timeline': '2015-Jul', 'total_prescriptions': 0}]}, {'medication_name': 'Actemra IV', 'trend': [{'timeline': '2015-Jun', 'total_prescriptions': 1}, {'timeline': '2015-Jul', 'total_prescriptions': 0}]}, {'medication_name': 'Adempas', 'trend': [{'timeline': '2015-Jun', 'total_prescriptions': 0}, {'timeline': '2015-Jul', 'total_prescriptions': 0}, {'timeline': '2015-Aug', 'total_prescriptions': 0}]}]
注意
这会修改原始数据。 您可以使用
copy.deepcopy()
来避免这种情况:import copy tmp = {} for med in copy.deepcopy(meds): ...
This should work (
meds
is the list with dicts):tmp = {} for med in meds: name = med.pop('medication_name') tmp.setdefault(name, []).append(med) res = [{'medication_name': k, 'trend': v} for k, v in tmp.items()]
Result:
>>> res [{'medication_name': 'Avonex', 'trend': [{'timeline': '2015-Jun', 'total_prescriptions': 1}, {'timeline': '2015-Jul', 'total_prescriptions': 0}]}, {'medication_name': 'Actemra IV', 'trend': [{'timeline': '2015-Jun', 'total_prescriptions': 1}, {'timeline': '2015-Jul', 'total_prescriptions': 0}]}, {'medication_name': 'Adempas', 'trend': [{'timeline': '2015-Jun', 'total_prescriptions': 0}, {'timeline': '2015-Jul', 'total_prescriptions': 0}, {'timeline': '2015-Aug', 'total_prescriptions': 0}]}]
Note
This modifies the original data. You can use
copy.deepcopy()
to avoid this:import copy tmp = {} for med in copy.deepcopy(meds): ...
相关问答
更多-
这应该工作( meds是带有dicts的列表): tmp = {} for med in meds: name = med.pop('medication_name') tmp.setdefault(name, []).append(med) res = [{'medication_name': k, 'trend': v} for k, v in tmp.items()] 结果: >>> res [{'medication_name': 'Avonex', 'trend': [{' ...
-
要删除dicts列表中的重复项: list_of_unique_dicts = [] for dict_ in list_of_dicts: if dict_ not in list_of_unique_dicts: list_of_unique_dicts.append(dict_) To remove duplicates from a list of dicts: list_of_unique_dicts = [] for dict_ in list_of_dicts: ...
-
其中res是: [{'location': 'C1', 'name': 'A1', 'url': 'B1'}, {'location': 'C2', 'name': 'A1', 'url': 'B2'}, {'location': 'C3', 'name': 'A2', 'url': 'B3'}, {'location': 'C4', 'name': 'A2', 'url': 'B4'}] 您可以使用defaultdict数据并将结果解defaultdict到列表解析中: from collecti ...
-
从dicts列表中删除具有接近重复值的dicts - Python(Deleting dicts with near-duplicate values from a list of dicts - Python)[2021-02-11]
可能有更多的pythonic方式,但这是基本的伪代码: def is_duplicate(a,b): if a['name'] == b['name'] and a['cost'] == b['cost'] and abs(int(a['cost']-b['cost'])) < 2: return True return False newlist = [] for a in oldlist: isdupe = False for b in newlist: if is_d ... -
您实际上应该使用groupby基于name和total_year而不是apply (作为第二步),并且在groupby中您可以创建所需的列表。 示例 - df = pd.DataFrame(list_yearly).merge(pd.DataFrame(list_monthly)) def func(group): result = [] for idx, row in group.iterrows(): result.append({'month':row['month' ...
-
您可以先合并两个数据框 In [144]: df = pd.DataFrame(list1).merge(pd.DataFrame(list2)) 看起来像, In [145]: df Out[145]: gender grade name chemistry physics 0 male third john 89 95 1 female second cathy 69 78 然后创建一个标记列作为字典 In ...
-
可能不是最有效的,但是在列表基础上将格式转换为邻接矩阵的简单方法可能如下所示: g = {1:{2:.5, 3:.2}, 2:{4:.7}, 4:{5:.6, 3:.3}} hubs = g.items() # list of nodes and outgoing vertices size=max(map(lambda hub: max(hub[0], max(hub[1].keys())), hubs))+1 # matrix dimension is highest known node index ...
-
这是一个使用collections.Counter()线性函数: >>> from collections import Counter >> C2 = Counter(daily) >>> C1 = Counter(master) >>> >>> {k:reduce(lambda x,y : Counter(x)+Counter(y), v) if isinstance(v, list) and k in (C1.viewkeys() & C2) else v for k, v in (C1 + C2) ...
-
以下函数将字典列表转换为新格式: def convert(d): res = {} for x in d: key = (x['user'], x['id']) if key in res: res[key]['key'].append(x['key']) res[key]['value'].append(x['value']) else: x['key'] = [x['k ...
-
为什么没有iter.remove在python dicts中实现?(Why hasn't iter.remove been implemented in python dicts?)[2021-07-09]
在你的比较中,你犯了两大错误。 首先,你忽略了甚至考虑惯用语“不删除任何东西,复制半个字典”选项。 其次,您没有意识到在2/3加载时删除哈希表中的一半条目会使您在1/3加载时使用完全相同大小的哈希表。 所以,让我们比较实际的选择(我将忽略2/3负载与你的n / 2测量一致)。 对于每一个,都有峰值空间,最终空间和时间: 2.0n,1.0n,1.5n:复制,删除原件的一半 2.0n,1.0n,1.5n:复制,删除一半副本 1.5n,1.0n,1.5n:构建删除集然后删除 1.0n,1.0n,0.5n:原地删除 ...