-1

I am trying to insert another key:value list of the data while print/storing in another result. Here is what I did till now:

>>> data = list()
>>> data.append({'data':'a','name':'alpha'})
>>> data.append({'data':'b','name':'beta'})
>>> data.append({'data':'c','name':'charlie'})
>>> data
[{'data': 'a', 'name': 'alpha'}, {'data': 'b', 'name': 'beta'}, {'data': 'c', 'name': 'charlie'}]
>>> names = []
>>> count = 0
>>> for i in data:
...     names.append(data[count]['name'])
...     count = count + 1
...
>>> names
['alpha', 'beta', 'charlie']
>>> txt = 'alpha'
>>> res = process.extract(txt,names)
>>> res
[('alpha', 100), ('charlie', 33), ('beta', 22)]
>>> for name, score in res:
...     print(data[names.index(name)])
...
{'data': 'a', 'name': 'alpha'}
{'data': 'c', 'name': 'charlie'}
{'data': 'b', 'name': 'beta'}
>>> for name, score in res:
...     print(data[names.index(name)]['score'] = score)
...
  File "<stdin>", line 2
SyntaxError: keyword can't be an expression

Kindly, let me know what I need to do, do that I get the result with added key:value. I am trying to get the output like this:

{'data': 'a', 'name': 'alpha', 'score': 100}
{'data': 'c', 'name': 'charlie', 'score': 33}
{'data': 'b', 'name': 'beta', 'score': 22}
8
  • 1
    you cannot print and assign at the same time. Just assign, and print result later. Commented Jul 21, 2017 at 11:43
  • @Jean-FrançoisFabre I tried like this result = (data[names.index(name)]['score'] = score) still not working. Please can you share something workable for me. please. Commented Jul 21, 2017 at 11:45
  • data[names.index(name)]['score'] = score that assign. Commented Jul 21, 2017 at 11:46
  • I am getting this error >>> for name, score in res: ... (data[names.index(name)]['score'] = score) File "<stdin>", line 2 (data[names.index(name)]['score'] = score) ^ SyntaxError: invalid syntax now what I need to do. Commented Jul 21, 2017 at 11:49
  • 1
    assignments cannot be in parentheses. Remove them. Commented Jul 21, 2017 at 11:51

2 Answers 2

2

In Python, assignment (here data[names.index(name)]['score'] = score) is a statement, so you cannot use it where an expression is expected. You have to first do the assignment, then print the result, ie replace this:

for name, score in res:
    print(data[names.index(name)]['score'] = score)

with:

for name, score in res:
    index = names.index(name)
    data[index]['score'] = score
    print data[index]

As a side note:

names = []
count = 0
for i in data:
    names.append(data[count]['name'])
    count = count + 1

is an incredibly complicated way to write:

names = [item["name"] for item in data]
Sign up to request clarification or add additional context in comments.

2 Comments

you should explain why OP is wrong, not just dump working code.
Indeed... Done.
0

Replace

for name, score in res:
    print(data[names.index(name)]['score'] = score)

with

for name, score in res:
    for d in data:
        if d['name']==name:
            d['score'] = score

Though it might be a better approach to use classes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.