Hello I have a python variable with List plus dictionary
>>> print (b[0])
{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}
-----------------------------------------------------------------------
>>> print (b)
[{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}]
>>>
I have tried everything But I couldn't get 'addr'
extracted.
Help Please.
You can just use b[0]['addr']
:
>>> b = [{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}]
>>> b[0]['addr']
'127.0.0.1'
try this:
print (b[0]['addr'])
print(b[0]) gives a dictionary, in dictionary you can fetch the value by its key like dict[key] => returns its associated value.
so
print(b[0]['addr'])
will give you the value ofaddr
Read about python data structure here Data structure
按键打印列表
print(b[0]['addr'])
你可以使用print(b[0]['addr'])
You could use get
method of dict
:
>>> b[0].get('addr')
'127.0.0.1'
From docs :
get
(key[, default])
Return the value for key if key is in the dictionary, else default . If default is not given, it defaults toNone
, so that this method never raises aKeyError
.
You may use get method of dict, which works on the key, and provide the corresponding value. b[0].get('addr')
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.