0
$\begingroup$

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?

$\endgroup$
2
  • $\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
    Commented Jul 15, 2018 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$ Commented Jul 15, 2018 at 13:44

1 Answer 1

0
$\begingroup$

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'
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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