列表的分类问题
a=[[“50063964”, “3.00”, “UNS -16402-021”, “25AZ7026”], [“50063964”, “1.00”, “UNS -23050-045”, “1DBK2223”],
[“50063965”, “3.00”, “UVS -16023-112”, “21QB3011”], [“50063965”, “1.00”, “FDN-25363-010”, “2FRC3011”],
[“50063966”,“1.00”,‘null’, ‘null’],[“50063966”,“3.00”,‘null’,‘null’],
[“500639677”,“3.00”,‘null’,‘null’],
[“500898979”,“5.0”,‘000’,‘00’]]
如何把二位列表,不使用其他库,仅仅使用逻辑来分类,按照第一个元素代码分类数据,分类变成三维列表:
b = [[[“50063964”, “3.00”, “UNS -16402-021”, “25AZ7026”], [“50063964”, “1.00”, “UNS -23050-045”, “1DBK2223”]],[[“50063965”, “3.00”, “UVS -16023-112”, “21QB3011”], [“50063965”, “1.00”, “FDN-25363-010”, “2FRC3011”]],[[“50063966”,“1.00”,‘null’, ‘null’[“50063966”,“3.00”,‘null’,‘null’]],[
[“500639677”,“3.00”,‘null’,‘null’]],[
[“500898979”,“5.0”,‘000’,‘00’]
]
]
社区支持 markdown 的
xh = list(set([i[0] for i in a]))
print(xh)
b = []
for x in xh:
c = []
for i in a:
if x in i:
c.append(i)
res = []
for i in c:
if i not in res:
res.append(i)
b.append(res)
print(b)
要在加一个去重,你这个有相同列表了
xh = set([i[0] for i in a])
b = []
for x in xh:
’ ’c = []
’ ’for i in a:
’ ’if x in i:
’ ’c.append(i)
’ ’b.append(c)
print(b)
#缩进太难打了 😥