I have key,value data where each record is in a Python string. An example record looks like this:

record = {
    'first_name': 'john',
    'last_name': 'doe',
    'age': '50',

To encode this into a neural net, I would like to firs have this data as a string vector. A sample output is like this:

"first_name john last_name doe age 50"

How can I make this transformation?

  • $\begingroup$ Does first_name, last_name, age come in order as you wrote or it can be in any order, like "last_name doe age 50 first_name john"? $\endgroup$
    – Ankit Seth
    Jul 15 '18 at 13:41
  • $\begingroup$ @AnkitSeth the representation which I wrote is one of the possible views of the record since Python dictionary's key/value pairs are unordered. $\endgroup$ Jul 15 '18 at 13:44

If you want output in any order, do this-

>>> string_vector = ''
>>> for k,v in record.items():
        string_vector += k + ' ' + v + ' '

>>> string_vector = string_vector.rstrip(' ')
>>> string_vector 
'first_name john last_name doe age 50'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.