Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

string - How can I compare the value of an object's attribute in Python?

So, I'm using mongoDB and my queries return an array of objects. The objects look like this:

   {
        '_id': ObjectId('5ff891bc20cb7d29dc1d836c'),
        'Model': 'X1150',
        'Platform': 'Server',
        '# of CPU Cores': 4,
        '# of Threads': 4
    }

Let's say I store this specific document in the variable doc

Now, I want to be able to compare the 'Platform' attribute with a string. In JavaScript, for example, I would do:

if (doc.Platform == 'Server') return 1;

but it doesn't seems to work in python (maybe because the attribute is a string?). How can I make this comparison correctly?

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The object you say it's returned it seems to be a dictionary , you can check it by type(doc). If that's the case then if (doc['Platform'] == 'Server') return 1; should work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...