0
cat = "["
for row in res:
    cat = cat  + (str((row['weeks'])) + ',')

cat = (cat + "]").replace(',]', ']')

The above bit of code gives a result string as:

[30,31,32,33,34,35,36,37,38,39,40]

However what I want is:

[W30,W31,W32,W33,W34,W35,W36,W37,W38,W39,W40]

I have been unsuccessful in concatenating the W before each number. How could I do this? This is the unsuccessful code that I tried:

cat = cat  + (str('W'+ (row['weeks'])) + ',')
7
  • Are you trying to make an actual list when this is done, or do you actually want a string? Commented Jul 10, 2015 at 11:50
  • I want the result to be a string but in the above mentioned format. Commented Jul 10, 2015 at 11:52
  • What's wrong with 'W{},'.format(row[weeks])? Commented Jul 10, 2015 at 11:52
  • @dhke: It gives W[30, 31, ..., 40],. Commented Jul 10, 2015 at 11:55
  • Ah! Please add an example of the contents of row['weeks']. This will probably stop other guesses. Commented Jul 10, 2015 at 12:01

6 Answers 6

2

This gives the desired string:

cat = '[{}]'.format(','.join('W{}'.format(i) for i in row['weeks']))
#                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#                            this gives 'W30', 'W31', etc.
#                   ^^^^^^^^^
#                   this joins them with commas: 'W30,W31,...,W40'
#     ^^^^^^^^^^^^^^
#     this puts [ ] around
Sign up to request clarification or add additional context in comments.

Comments

1

You have done this

cat = cat  + (str('W'+ (row['weeks'])) + ',')

It should be

cat = cat  + ('W'+ str(row['weeks']) + ',')

if you want it to be a list you could do this

cat = ["W"+str(row['weeks']) for row in res]

3 Comments

Your initial answer was great, thank you so much. I later realised that I need the final answer in the form of a JSON Object hence the final edit was: cat = cat + ('"W'+ str((row['weeks'])) + '",') cat = (cat + "]").replace(',]', ']')
you are welcome but do see others way it might be efficient and help full for you :)
So my final list was ["W30","W31","W32","W33","W34","W35","W36","W37","W38","W39","W40"]
1

code:

cat = '[W' + ',W'.join(str(element) for element in row['weeks']) + ']'

Comments

1

Instead of getting an answer from here (since there are many ways to do it), you should understand what went wrong with your own code.

(str('W'+ (row['weeks'])) + ',')

What is the order of operations here?

  1. We find row['weeks'].

  2. We attempt 'W' + that.

  3. We attempt str on that.

  4. We attempt that + ','.

Presumably, row['weeks'] is an integer, so the addition with 'W' will fail. You must do this after applying str to the integer.

Comments

0

If you want a list made of strings do

cat = [ 'W'+str(row['weeks']) for row in res]

and, if you want a single string, turn the list into a string

string_cat = str(cat)

Comments

0
cat = "[" + ",".join('W' + str(row['weeks']) for row in res) + "]"

For each row in res convert row['weeks'] to string, prefix it with 'W', and join all such strings with the ',' character. Then just add opening [ and closing ].

Comments

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.