0

I need to create a long text string out of 3 lists. This text string has to be in a very specific format where there's an element from each list separated by a space, and each group has a semicolon afterwards. The below script should run:

import math

reclassInt = [1, 2, 3, 4, 5, 6]
minValueRange = [512, 550.0, 600.0, 650.0, 700.0, 750.0]
maxValueRange = [550.0, 600.0, 650.0, 700.0, 750.0, 755]

for numbers in reclassInt:
    newNums = numbers-1
    longString = str(minValueRange[newNums]) + " " + str(maxValueRange[newNums]) + " " + str(reclassInt[newNums]) + ";" 
    print longString

This will return 6 lines, the first being:

512 550.0 1;

Instead of printing these strings, I want to set a variable to each of them. I want this variable to have a unique name for each iteration of the loop( based on reclassInt items).

Ideally this could create variables like line1, line2, ect to equal the string outputs from each iteration. Then I could string these together outside the loop to get my desired string.

To be clear my end goal is this exact string:

"512 550.0 1;550.0 600.0 2;600.0 650.0 3;650.0 700.0 4;700.0 750.0 5;750.0 755 6;"

Thanks All,...

3
  • 5
    dynamic naming is almost always a bad idea. Usually it's much better to use a dict. (also, your code could be cleaned up a bit by using zip :-) Commented Dec 9, 2013 at 7:46
  • What is that bs supposed to be doing there? Commented Dec 9, 2013 at 7:57
  • Looks like a list might be better here. I don't think the items really need to be named based on reclassInt; it looks like all that's really needed is that they be in order and accessible by index. Commented Dec 9, 2013 at 8:11

2 Answers 2

2

How about this:

>>> d = {i+1:v for i,v in enumerate(zip(minValueRange, maxValueRange))}
>>> d
{1: (512, 550.0),
 2: (550.0, 600.0),
 3: (600.0, 650.0),
 4: (650.0, 700.0),
 5: (700.0, 750.0),
 6: (750.0, 755)}

Then to print your string:

>>> for k,v in d.iteritems():
...    print('{} {} {};'.format(v[0], v[1], k))
...
512 550.0 1;
550.0 600.0 2;
600.0 650.0 3;
650.0 700.0 4;
700.0 750.0 5;
750.0 755 6;

To get the string in one line, as per your update:

>>> ''.join('{} {} {};'.format(v[0], v[1], k) for k,v in d.iteritems())
'512 550.0 1;550.0 600.0 2;600.0 650.0 3;650.0 700.0 4;700.0 750.0 5;750.0 755 6;'

Also if that's your only requirement you don't need to generate the dictionary and can combine everything into this:

>>> ''.join('{} {} {};'.format(v[0], v[1], k+1) for k,v in enumerate(zip(minValueRange, maxValueRange)))
'512 550.0 1;550.0 600.0 2;600.0 650.0 3;650.0 700.0 4;700.0 750.0 5;750.0 755 6;'
Sign up to request clarification or add additional context in comments.

2 Comments

I added to the original question showing the exact syntax I need. I did not get this to work. Thanks...
Thanks- your 1 line does it all.
2

To make things much simpler, and better, you can use a dictionary:

longStrings= {}
for numbers in reclassInt:
    newNums = numbers-1
    d["longString_%d"%numbers] = str(minValueRange[newNums]) + " " + str(maxValueRange[newNums]) + " " + str(reclassInt[newNums]) + ";" 
    bs = "ls" + str(numbers)

1 Comment

I tried this but when I added the command to print longStrings and it was still empty- {}

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.