0

I have the following object being returned using response.json within the Requests module:

{u'statColumns': [u'apps', u'subOn', u'minsPlayed', u'tackleWonTotal', u'challengeLost', 
u'tackleTotalAttempted'], u'paging': {u'firstRecordIndex': 1, u'resultsPerPage': 1,     u'lastRecordIndex':
1, u'totalPages': 431, u'currentPage': 1, u'totalResults': 431}, u'playerTableStats': 
[{u'positionText': u'Defender', u'rating': 8.37, u'weight': 77, u'playerId': 22079, u'height': 188, 
u'teamId': 32, u'playedPositions': u'-DC-', u'challengeLost': 0.0, u'isManOfTheMatch': False, u'isOpta':
True, u'subOn': 0, u'tackleWonTotal': 2.0, u'tournamentShortName': u'EPL', u'apps': 1, u'teamName':
u'Manchester United', u'tournamentRegionId': 252, u'regionCode': u'gb-nir', u'tournamentRegionCode':
u'gb-eng', u'playedPositionsShort': u'D(C)', u'seasonId': 4311, u'ranking': 1, u'minsPlayed': 90, 
u'tournamentName': u'Premier League', u'isActive': True, u'name': u'Jonny Evans', u'firstName': 
u'Jonny', u'lastName': u'Evans', u'age': 26, u'seasonName': u'2014/2015', u'tournamentId': 2,
u'tackleTotalAttempted': 2.0}]}

This object has two elements, playerTableStats and statColumns. These can accessed with the following code:

playerTableStats = response[u'playerTableStats']
statColumns = response[u'statColumns']

If I convert the contents of statColumns into a string object, I can use it as part of a .format() statement to map playerTableStats. I could do this like so:

var1 = ['one','two','three','four']
var1 = 'u"{',var1[0],'}','{',var1[1],'}','{',var1[2],'}','{',var1[3],'}"'
var1 = ''.join(var1)

var2 = (var1.format(**responser))

print var2

However that doesn't seem very Pythonic and doesn't take into account the fact that both statColumns and playerTableStats will vary in their number of elements when I am returning different .json objects from the same site.

Is there a cleaner way to get all elements of statColumns into the format:

u"{one}{two}{three}{four}"

Thanks

1 Answer 1

1

Using str.join with generator expression:

var2 = ''.join('{' + x + '}' for x in var1)
Sign up to request clarification or add additional context in comments.

3 Comments

@falsetru...ok that worked great, thanks. could you explain what you mean by a generator expression though please?
@gdogg371, Generator expression is similar to list comprehension, except it results in generator instead of list. I added a link to the Python tutorial page for generator expression. Follow the link for more detail.
@gdogg371, If you are not clear, try the following list comprehension: ['{' + x + '}' for x in var1]. You will get a list of ['one', 'two', ...]. If you use generator expression, it will return a generator which yields each element lazily.

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.