0

I have this list. I want to make a for loop that will use in a function combinations of these files in the list.

I am not sure how to make these combinations that for each 'check' it will take the correct combination.

The function if it wasn't for the loop it would look like this:

erase('check3_dwg_Polyline','check3_dwg_Polyline_feat_to_polyg_feat_to_line','output_name')

What I've tried:

Here's the list.

li=['check3_dwg_Polyline', 'check2_dwg_Polyline', 
'check3_dwg_Polyline_feat_to_polyg',# this will not be needed to extracted
'check2_dwg_Polyline_feat_to_polyg',# >> >>
'check3_dwg_Polyline_feat_to_polyg_feat_to_line', 
 'check2_dwg_Polyline_feat_to_polyg_feat_to_line']

start with this:

a=[li[i:i+3] for i in range(0, len(li), 3)]

where returns:

[['check3_dwg_Polyline',
  'check2_dwg_Polyline',
  'check3_dwg_Polyline_feat_to_polyg'],
 ['check2_dwg_Polyline_feat_to_polyg',
  'check3_dwg_Polyline_feat_to_polyg_feat_to_line',
  'check2_dwg_Polyline_feat_to_polyg_feat_to_line']]

Finally:

for base, base_f, base_line in a:
    print(base, base_line, base + "_output")

gives:

check3_dwg_Polyline  check3_dwg_Polyline_feat_to_polyg  check3_dwg_Polyline_output
check2_dwg_Polyline_feat_to_polyg  check2_dwg_Polyline_feat_to_polyg_feat_to_line  check2_dwg_Polyline_feat_to_polyg_output

Other method:

base = [f for f in li if not f.endswith(("_polyg", "_to_line"))]
base_f = {f.strip("_feat_to_polyg"): f for f in li if f.endswith("_polyg")}
base_line = {f.strip("_feat_to_polyg_feat_to_line"): f for f in li if f.endswith("_to_line")}
[(b, base_f[b], base_line[b]) for b in base]

gives:

KeyError: 'check3_dwg_Polyline'

I have tried sorting the list but it just ruins it in a different way when put through the processes mentioned above.

The ideal result is this

when trying this:

for base, base_f, base_line in a:
    print(base, base_line, base + "_output")

to give this:

check3_dwg_Polyline   check3_dwg_Polyline_feat_to_polyg_feat_to_line  check3_dwg_Polyline_output

check2_dwg_Polyline   check2_dwg_Polyline_feat_to_polyg_feat_to_line  check2_dwg_Polyline_output

where will be put in like this:

erase('check3_dwg_Polyline','check3_dwg_Polyline_feat_to_polyg_feat_to_line','output_name')
  • What's your question? – pacholik Jul 26 '18 at 07:35
  • I am not sure how to make these combinations that for each 'check' it will take the correct combination. – user10110338 Jul 26 '18 at 07:37
  • What's the “correct” combination? – pacholik Jul 26 '18 at 07:38
  • In your *other method*, you're passing a string to `str.strip` method – but the method takes a list of chars that should be stripped (regardless of order). You'd know if you checked your variables. – pacholik Jul 26 '18 at 07:40
  • i updated it to make it more clear. btw correct would as in the form of the function `erase`.Just look the update to understand. – user10110338 Jul 26 '18 at 07:41
  • @pacholik So what is the right way? – user10110338 Jul 26 '18 at 07:47
  • https://stackoverflow.com/q/1038824/1028589 – pacholik Jul 26 '18 at 07:50
  • I still don't know what should be changed. – user10110338 Jul 26 '18 at 07:55
  • Well we still don't know the relation between your list *li* and your result. – pacholik Jul 26 '18 at 08:05
  • `li` is the list that contains the files and I want to make a loop that will take the files and put them in the `erase` function in the exact order that is described in the last part of the question. Hope this is clear now. Thanks – user10110338 Jul 26 '18 at 08:12
  • So why can't you just replace `print` with `erase` in your *for* loop? – pacholik Jul 26 '18 at 08:33
  • Because the files that are created are in the wrong position.That's the question. – user10110338 Jul 26 '18 at 08:44
  • Since the naming is consistant why not simply do this: `X = ["check2_dwg_Polyline", "check3_dwg_Polyline"]` and `for check in X: (check, check + "_feat_to_polyg_feat_to_line", check + "_output")`? – Mathieu Jul 26 '18 at 08:48
  • @Mathieu this is just an example the files are way more.If you can make it work in general by their consistent endings proceed with an answer.Thanks – user10110338 Jul 26 '18 at 08:52
  • Seriously your post is completely unclear. We do not get what you want. It seems like an utterly simple problem, but we just don't get it. Rework it and you'll get an answer in a few minute. – Mathieu Jul 26 '18 at 09:18
  • i.e. states clearly what you have, what you want. A simple I/O describing every possible I/O is enough. – Mathieu Jul 26 '18 at 09:23

1 Answers1

0

zip the list into chunks of check3, check2… Then you can do your for loop.

n = len(li) // 3
a = zip(*[li[i:i+n] for i in range(0, len(li), n)])

(pprint(list(a)) would output

[('check3_dwg_Polyline',
  'check3_dwg_Polyline_feat_to_polyg',
  'check3_dwg_Polyline_feat_to_polyg_feat_to_line'),
 ('check2_dwg_Polyline',
  'check2_dwg_Polyline_feat_to_polyg',
  'check2_dwg_Polyline_feat_to_polyg_feat_to_line')]
pacholik
  • 8,607
  • 9
  • 43
  • 55