Reputation: 127
I have a list of drawingnumbers
, I am attempting to split these strings and then append to a number of lists.
I am hoping to end up with a number of lists, which contains each relevant piece of the original string.
At the minute my definition is iterating through the list, but overwriting the variables, not appending them. So I have a single entry for each variable and these correspond to the final entry of the list.
Could anybody please help?
# 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)
Upvotes: 2
Views: 71
Reputation: 446
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])
Upvotes: 0
Reputation: 113
Just change
for i in drawingnumber:
building, originator, discipline, level, scope, drawingtype,drawingsubtype, numbera, numberb = i.split("-")
to:
for i in drawingnumber:
building_, originator_, discipline_, level_, scope_, drawingtype_,drawingsubtype_, numbera_, numberb_ = i.split("-")
building.append(building_)
originator.append(originator_)
...etc...
splitted valeus redefine your variables each time what you want to do here is basically append those to lists you created, also pick plural names for list like: buildings and append singular variables to them
Upvotes: 0
Reputation: 215037
You can use zip
after splitting each element in the list to transpose your lists as:
zip(*[i.split("-") for i in drawingnumber])
And assign them to lists names:
building, originator, discipline, level, scope, \
drawingtype, drawingsubtype, numbera, numberb = zip(*[i.split("-") for i in drawingnumber])
Example output:
building
# ('AAA601', 'AAA601', 'AAA601', 'AAA601')
originator
# ('XXX', 'XXX', 'XXX', 'XXX')
numberb
# ('001', '001', '001', 'XXX')
Upvotes: 2