简体   繁体   中英

Python(flask)--how to get a value from a data without 'for loop'

I'm new to Python. I've got the question.

here's the data:

languages =[
{'name': 'java','version': '2.0'}, 
{'name': 'python',"version": "3"}, 
{'name': 'ruby','version': '2.3'}
]

python code:

@app.route('/test3/<name>', methods=['GET']) 
def test3(name):
    for language in languages:
        if language['name'] == name: 
            return jsonify([language])
    return ("err")

so if I want to get the ruby data, my url will be http://localhost:5000/test3/ruby and the result will be:

[
  {
    "name": "ruby", 
    "version": "2.3"
  }
]

but how do I get the ruby data without 'for loop'?

I tried another example, if my data is:

data = {'1':'java', '2':'python', '3':'ruby'}

python code:

@app.route('/data/<userid>', methods=['GET']) 
def returnOneofData(userid):
    if userid in data:
        r = jsonify({userid:data[userid]})          
    else:
        r = (userid + ' is not found')
    return r

I use 'if else', not 'for loop'.

So that i can get ruby with the url: http://localhost:5000/data/3 and no need to use for loop.

I know these two datas are different type, is there any way that I can get ruby from the first data(languages) without for loop?

In many cases, You should put your data in database, and use database to save/query your data. Sqlite3 is a good database for newbies. If your data is small, and you really don't want to use database, you can save your data in a configure file (eg. a json file) and not save your data to your code directly.

As your question, I think 'for loop' is not a bad choice for small size data. If you don't like it, you can change your data struct like your second data:

languages ={
  'java':{'version': '2.0'}, 
  'python':{'version': '3'}, 
  'ruby':{'version': '2.3'}
}

and use your 'if else' to do this.

You can also use another 'for loop' format.

[language for language in language if language['name']=='ruby' 

Assuming your data follows the same format, you can transform the data to a dictionary of language-version pairs for simple, efficient lookup. The dict.get function can be utilized to access the value via key, and a default error value can be provided if the language is not found:

languages = [{'name': 'java', 'version': '2.0'}, {'name': 'python', 'version': '3'}, {'name': 'ruby', 'version': '2.3'}]
new_languages = dict(i.values() for i in languages)
#{'java': '2.0', 'python': '3', 'ruby': '2.3'}

@app.route('/test3/<name>', methods=['GET']) 
def test3(name):
   return flask.jsonify({'lang':name, 'result':new_languages.get(name, 'ERR')})

Try this:

@app.route('/test3/<name>', methods=['GET']) 
def test3(name):
   return list(filter(lambda language: language.get('name')==name , languages))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM