0

I want to add value of entity_id with attribute for type which is String. Below are the details of code:

entities =  [{'id': 'Room1',
             'temperature': {'value': '10', 'type': 'String'}, 
             'pressure':    {'value': '12', 'type': 'Number'}, 
             'type': 'Room',
             'time_index': '2020-08-24T06:23:37.346348'}, 

            {'id': 'Room2',
             'temperature': {'value': '10', 'type': 'Number'},
             'pressure':    {'value': '12', 'type': 'Number'}, 
             'type': 'Room',
             'time_index': '2020-08-24T06:23:37.346664'},

            {'id': 'Room3',
             'temperature': {'value': '10', 'type': 'Number'},
             'pressure':    {'value': '12', 'type': 'Number'}, 
             'type': 'Array',
             'time_index': '2020-08-24T06:23:37.346664'}]

attr = ['temperature','pressure']

self.logger.warning(msg.format( attr, entity_id)

How can i add value of id where type is String with warning msg.format in above code.. New to python so any help on it will be great.Thanks

7
  • 1
    Please add a minimal example. Commented Aug 25, 2020 at 7:07
  • Does your problem have anything to do with this TODO in the code you pasted: # TODO we should support type name different from NGSI types but mapping to NGSI types? Commented Aug 25, 2020 at 7:12
  • 1
    The title of your question does not match its content at all. Please change the title to summarize what you are asking about, currently it is totally misleading. Also, please add the imports of your code, especially any 3rd party libraries that are related to this NGSI code. Commented Aug 25, 2020 at 7:14
  • Please don't expect us to write your code. You have asked a similar question one days ago stackoverflow.com/questions/63556254/… . But it is not clear where are those NGSI_TYPE, TIME_INDEX_NAME variables coming from, what is the whole class doing, and why are those types so important. So I'm afraid we can't give you a meaningful answer. Commented Aug 25, 2020 at 7:17
  • @ArpadHorvath i dint get expected answer there so i asked once again with more clearer view . I think i should remove code if its not clear.Only ask with example code is only for refrence . I am asking for help not to write code. I am stuck in one problem so aksing help for that. Hope its understood Commented Aug 25, 2020 at 7:33

1 Answer 1

2

To get the value of a dictionary key you do the following.

dict = {'a':1, 'b':2}
print(dict['a'])

This will print the result 1

If you have a dictionary inside a dictionary then do the following

dict = {1: {'a': 'abc', 'b': 'xyz'}}
print(dict[1][a])

This will print abc

Another alternative to putting dictionaries inside dictionaries would be to make a particular key hold a tuple, look at the following example

dict = {'a': ('abc', 'xyz'), 'b':('qwe', 12)}

for i in dict:
    print(dict[i][1])

This will iterate through your dictionary and print xyz and 12. You could also give a condition to print it only if it meets a particular age by following this.

You can also iterate through your nested dictionaries if you desire that.

Depending on your need you can make it hold a dictionary or a tuple however, I think a tuple makes it easier to read and understand code (this is my preference).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for response. dict = {1: {'name': 'John', 'age': '27', 'gender': 'Male'}, 2: {'name': 'Marie', 'age': '22', 'gender: 'Female'} I want to print here, name and gender where age is '27'.... how can i do in this. Thanks
@damini chopra I have updated my answer and added some more detail.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.