我有一个drawingnumbers
列表,我试图拆分这些字符串,然后附加到多个列表中。
我希望最终得到一些列表,其中包含原始字符串的每个相关部分。
当我的定义在列表中迭代时,但是覆盖变量,而不是附加变量。所以我为每个变量都有一个条目,这些条目对应于列表的最后一个条目。
有人可以帮忙吗?
# drawingnumber split
drawingnumber = ["AAA601-XXX-A-L00-1028-DR-GA-200-001",
"AAA601-XXX-A-L10-1028-DR-GA-200-001",
"AAA601-XXX-A-L00-1029-DR-GA-200-001",
"AAA601-XXX-A-L00-1029-DR-GA-200-XXX"]
building = []
buildinglist = []
originator = []
discipline = []
level = []
scope = []
drawingtype = []
drawingsubtype = []
numbera = []
numberb = []
for i in drawingnumber:
building, originator, discipline, level, scope, \
drawingtype,drawingsubtype, numbera, numberb = i.split("-")
print("building:", building)
print("originator: ", originator)
print("discipline: ", discipline)
print("level: ", level)
print("scope: ", scope)
print("drawingtype: ", drawingtype)
print("drawingsubtype", drawingsubtype)
print("drawingident", numbera, "-", numberb)
答案 0 :(得分:2)
分割列表中的每个元素后,您可以使用zip
将列表转置为:
zip(*[i.split("-") for i in drawingnumber])
并将它们分配给列表名称:
building, originator, discipline, level, scope, \
drawingtype, drawingsubtype, numbera, numberb = zip(*[i.split("-") for i in drawingnumber])
示例输出:
building
# ('AAA601', 'AAA601', 'AAA601', 'AAA601')
originator
# ('XXX', 'XXX', 'XXX', 'XXX')
numberb
# ('001', '001', '001', 'XXX')
答案 1 :(得分:0)
只需更改
for i in drawingnumber:
building, originator, discipline, level, scope, drawingtype,drawingsubtype, numbera, numberb = i.split("-")
为:
for i in drawingnumber:
building_, originator_, discipline_, level_, scope_, drawingtype_,drawingsubtype_, numbera_, numberb_ = i.split("-")
building.append(building_)
originator.append(originator_)
...etc...
拆分的valeus重新定义你的变量,每次你想要做的事情基本上都附加到你创建的列表,也选择列表的复数名称,如:建筑物和附加奇异变量
答案 2 :(得分:0)
drawingnumber = ["AAA601-XX1-A-L00-1028-DR-GA-200-001",
"AAA602-XX2-A-L10-1028-DR-GA-200-001",
"AAA603-XX3-A-L00-1029-DR-GA-200-001",
"AAA604-XX4-A-L00-1029-DR-GA-200-XXX"]
building = []
buildinglist = []
originator = []
discipline = []
level = []
scope = []
drawingtype = []
drawingsubtype = []
numbera = []
numberb = []
for i in drawingnumber:
j = i.split('-')
building.append(j[0])
buildinglist.append(j[1])
for i in range(len(drawingnumber)):
print("building:", building[i])
print("buildinglist:", buildinglist[i])