0

I have list of assembly class instances. The total number of assemblies in the list can change.

ASM_list = [ASM1,ASM2,ASM3...etc]

each assembly has a list of parts (I have filtered the assemblies to all have the same parts)

ASM1.parts = ['hood','fender','door']
ASM2.parts = ['hood','fender','door']

each part has properties

ASM1.parts[0].color = green
ASM2.parts[0].color = blue

I would like to print out part properties for each assembly. Example output:

part, ASM1, ASM2, ASM3
hood, green, blue, red
fender, black, red, yellow
door, yellow, green, orange

I'm having trouble using zip or map to do this.

An example of what I'm trying below.

parts_list = []
for ASM in ASM_list:
  parts_list.append([p.color for p in ASM.parts])
print zip(*parts_list)

The "Python cookbook" and several other stack overflow posts show how to iterate through multiple list at the same time, the problem I have is that the number of assemblies changes so I can't write:

for a,b,c in ASM_lists:
  a.part.color, b.part.color, c.part.color

Thanks!

0

1 Answer 1

1

Assuming you have a list of part_names - which, if your part objects objects have a name attribute as well as a color attribute, you could get by:

part_names = []
for part in ASM_list[0].parts:
    part_names.append(part.name)

Then you can get the output you want with:

headers = ["part"]
for i, ASM in enumerate(ASM_list):
    headers.append("ASM%d" % (i+1))
print(", ".join(headers))

for i, part_colors in enumerate(zip(*parts_list)):
    part_name = part_names[i]
    print(part_name + ", " + ", ".join(part_colors))
Sign up to request clarification or add additional context in comments.

3 Comments

I greatly appreciate your insight. I don't fully grasp all of it yet, as of now I'm getting an "expected string, float found" error on the final print statement. I tried str(part_name) to no avail.
This works... The failures comes from me using this pattern on part properties that are floats (part dimensions). Maybe with some tuple formatting it will be reusable?
" ".join(map(str, a)) converts to a string for join. THANKS!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.