简体   繁体   中英

list from findall to a dict

I have some data that looks like this:

<subject="execute_and_get_modifiers"\><command="send_key"\><normalkey="b"\><modifier="0"\>

I have a regex that nicely finds items.. like this

p = re.compile('<([\w]+)="([\w\d]+)"')
self.data =  dict(p.findall(request))

which looks like:

 {'modifier': '0', 'normalkey': 'b', 'command': 'send_key', 'subject': 'execute_and_get_modifiers'}

what this is really is key: value pairs. So how do I access say self.data['modifier'] ?

I tried doing for key, value in self.data : but I get "ValueError: too many values to unpack" - but its two items each dict item isn't it?

Go easy now - I know I'm being a fool but can't work out why..

您的for声明应为:

for key, value in self.data.items()

Even better:

for key, value in self.data.iteritems()

When you're iterating over the pairs, you should use iteritems() instead of items() for the memory savings. items() will build up the list of (key, value) pairs in memory, while iteritems() will return a view of the key value pairs, which you can iterate over in constant memory space.

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