python - pythonic way to optimize the logic to filter/extract data from list -


i have list below:

['1 (uid 3234 flags (seen \\seen))', '2 (uid 3235 flags (\\seen))',  '3 (uid 3236 flags (\\deleted))', '4 (uid 3237 flags (-flags \\seen +flags))',  '5 (uid 3241 flags (-flags \\seen +flags))', '6 (uid 3242 flags (\\seen))',   '7 (uid 3243 flags (\\seen))', '8 (uid 3244 flags (\\seen))',   '9 (uid 3245 flags (\\seen))', '10 (uid 3247 flags (\\seen))',  '11 (uid 3252 flags (\\seen))', '12 (uid 3253 flags (\\deleted))',  '13 (uid 3254 flags ())', '14 (uid 3256 flags (\\seen))', '15 (uid 3304 flags ())',  '16 (uid 3318 flags (\\seen))', '17 (uid 3430 flags (\\seen))',  '18 (uid 3431 flags ())', '19 (uid 3434 flags (\\seen))',  '20 (uid 3447 flags (-flags \\seen +flags))', '21 (uid 3478 flags ())',  '22 (uid 3479 flags ())', '23 (uid 3480 flags ())', '24 (uid 3481 flags ())'] 

from list want 3 different list result. want result using single iteration on list.

  1. list of all uids i.e [3234,3235,3236,3237,3241 .....]
  2. list of seen uids i.e [3234,3235 ...] <-- uid of item has \seen flag
  3. list of deleted uids i.e [3236,3253] <-- uid of item has \deleted flag

the best thing turn data dict mapping uid flags, searching easy. data this:

{'3254': '', '3304': '', '3236': '\\deleted', '3237': '-flags \\seen +flags', '3234': 'seen \\seen', '3235': '\\seen', '3430': '\\seen', '3431': '', '3252': '\\seen', '3253':'\\deleted', '3478': '', '3479': '', '3256': '\\seen', '3481': '', '3480': '', '3318': '\\seen', '3434': '\\seen', '3243': '\\seen', '3242': '\\seen', '3241': '-flags \\seen +flags', '3247': '\\seen', '3245': '\\seen', '3244': '\\seen', '3447': '-flags \\seen +flags'} 

you can using regular expression match each entry in list. if regexp return 2 groups in match can build dict.

so end this:

items = ['1 (uid 3234 flags (seen \\seen))', '2 (uid 3235 flags (\\seen))', '3 (uid 3236 flags (\\deleted))', '4 (uid 3237 flags (-flags \\seen +flags))', '5 (uid 3241 flags (-flags \\seen +flags))', '6 (uid 3242 flags (\\seen))',  '7 (uid 3243 flags (\\seen))', '8 (uid 3244 flags (\\seen))',  '9 (uid 3245 flags (\\seen))', '10 (uid 3247 flags (\\seen))', '11 (uid 3252 flags (\\seen))', '12 (uid 3253 flags (\\deleted))', '13 (uid 3254 flags ())', '14 (uid 3256 flags (\\seen))', '15 (uid 3304 flags ())', '16 (uid 3318 flags (\\seen))', '17 (uid 3430 flags (\\seen))', '18 (uid 3431 flags ())', '19 (uid 3434 flags (\\seen))', '20 (uid 3447 flags (-flags \\seen +flags))', '21 (uid 3478 flags ())', '22 (uid 3479 flags ())', '23 (uid 3480 flags ())', '24 (uid 3481 flags ())']  import re pattern = re.compile(r"\d+ \(uid (\d+) flags \(([^)]*)\)\)") values = dict(pattern.match(item).groups() item in items) 

we can query items in values want:

print "all uids:",values.keys() print "seen uids:",[uid uid,flags in values.iteritems() if r"\seen" in flags] print "deleted uids:",[uid uid,flags in values.iteritems() if r"\deleted" in flags] 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -