我正在尝试从字节列表创建一个键:value1,value2字典并且遇到问题。
这是我的清单
[b'Expected in April 2018',
b'Murder At Koh E Fiza',
b'34',
b'06 April 2018',
b'Subedar Joginder Singh',
b'0',
b'06 April 2018',
b'Blackmail',
b'86',
b'06 April 2018',
b'Missing',
b'0',
b'13 April 2018',
b'October',
b'59',
b'13 April 2018',
b'Mercury',
b'0',
b'20 April 2018',
b'Omerta',
b'50']
我尝试过以下代码:
b = dict(zip(list[1::3],(list[2::3]+list[0::3])))
但我没有获得第三个值作为键值对。
我也试过
b = dict(zip(list[1::3],list[2::3]+list[0::3]))
同样的问题我正在使用这两个语句跟随输出
{b'Murder At Koh E Fiza': b'34', b'Subedar Joginder Singh': b'0',
b'Blackmail': b'86', b'Missing': b'0', b'October': b'59', b'Mercury': b'0',
b'Omerta': b'50'}
我正在寻找以下输出
b'Murder At Koh E Fiza': b'34',b'Expected in April 2018',
b'Subedar Joginder Singh': b'0',b'06 April 2018',
请告诉我
答案 0 :(得分:2)
我认为您希望将列表或元组与字典中的每个键相关联。所以这样的事情应该有效:
<form [formGroup]=”form” (submit)=”submit(form.value)”>
<div *ngFor=”let dom of domains.controls; let i=index”>
<input type=”checkbox” [formControl]=”domain”/> {{domains.catergory}}
</div>
<button type=”submit”>Submit</button>
</form>
结果是
dict( zip(list[1::3], zip( list[2::3], list[0::3] ) ))
答案 1 :(得分:1)
您可以将zip
与dict comprehension
a = [b'Expected in April 2018',
b'Murder At Koh E Fiza',
b'34',
b'06 April 2018',
b'Subedar Joginder Singh',
b'0',
b'06 April 2018',
b'Blackmail',
b'86',
b'06 April 2018',
b'Missing',
b'0',
b'13 April 2018',
b'October',
b'59',
b'13 April 2018',
b'Mercury',
b'0',
b'20 April 2018',
b'Omerta',
b'50']
final = {v: [m, k] for k, v, m in zip(a, a[1:], a[2:])}
print(final.get(b'Murder At Koh E Fiza')
print(final.get(b'Subedar Joginder Singh'))
输出:
[b'34', b'Expected in April 2018']
[b'0', b'06 April 2018']
答案 2 :(得分:0)
你也可以在没有拉链的情况下找到它:
my_list = ['Expected in April 2018', 'Murder At Koh E Fiza', '34', '06
April 2018', 'Subedar Joginder Singh', '0', '06 April 2018',
'Blackmail', '86', '06 April 2018', 'Missing', '0', '13 April 2018',
'October', '59', '13 April 2018', 'Mercury', '0', '20 April 2018',
'Omerta', '50']
n = 3
composite_list = [my_list[x:x+n] for x in range(0, len(my_list),n)]
d = {n[1]: [n[0], n[2]] for n in composite_list}