我有3个与每个列表相关的列表,我想使用python将它们放入3个不同的列表或字典中,或者任何它能工作的列表。数据列表中的数据是某种主键。
datelist = [1, 2, 2, 11, 11, 11, 11]
inlist = [61345098075, 61453498075, 34353, 23421, 23421, 23421, 23421]
outlist = [61345816236, 61434636236, 43532, 63345816236, 34276816236, 34566816236, 84876816236]我想要的输出如下
inlist_1 = [61345098075]
outlist_1 = [61345816236]
inlist_2 = [61453498075, 34353]
outlist_2 = [61434636236, 43532]
inlist_11 = [23421, 23421, 23421, 23421]
outlist_11 = [63345816236, 34276816236, 34566816236, 84876816236]我想将数据列表中的1,2,11保存为变量,以便将来使用。
提前谢谢你的帮助。
发布于 2020-07-10 04:00:41
如果如您所述,键值是日期项,则应使用该项创建字典。看来你想要两本字典。下面的另一种选择是将in/out的元组存储在同一个字典中。
In [12]: from collections import defaultdict
In [13]: %paste
datelist = [1, 2, 2, 11, 11, 11, 11]
inlist = [61345098075, 61453498075, 34353, 23421, 23421, 23421, 23421]
outlist = [61345816236, 61434636236, 43532, 63345816236, 34276816236, 34566816236, 84876816236]
## -- End pasted text --
In [14]: in_dict=defaultdict(list)
In [15]: out_dict=defaultdict(list)
In [16]: for date, i, j in zip(datelist, inlist, outlist):
...: in_dict[date].append(i)
...: out_dict[date].append(j)
...:
In [17]: in_dict.get(2)
Out[17]: [61453498075, 34353]
In [18]: out_dict.get(11)
Out[18]: [63345816236, 34276816236, 34566816236, 84876816236]
In [19]: https://stackoverflow.com/questions/62826974
复制相似问题