[Answer]-Find specific words in set containing strings in python

1👍

try this:

>>> str = "set([u'auth.view_group', u'auth.add_user', u'transaction.change_sender', u'myprofile.view_department', u'auth.add_permission', u'transaction.add_receiver', u'myprofile.delete_routineusers', u'transaction.change_receiver', u'sessions.change_session', u'transaction.delete_receiver', u'registration.change_registrationprofile', u'auth.view_user', u'auth.delete_permission', u'transaction.view_sender', u'myprofile.change_hrworker', u'registration.add_registrationprofile', u'admin.view_logentry', u'registration.view_registrationprofile', u'contenttypes.change_contenttype', u'myprofile.view_routineusers', u'sessions.delete_session', u'myprofile.change_department', u'myprofile.delete_hrworker', u'admin.change_logentry', u'myprofile.change_allusers', u'admin.delete_logentry', u'transaction.delete_sender', u'auth.add_group', u'myprofile.delete_department', u'myprofile.add_department', u'contenttypes.delete_contenttype', u'auth.view_permission', u'myprofile.add_allusers', u'myprofile.delete_allusers', u'auth.change_permission', u'transaction.view_receiver', u'auth.change_group', u'registration.delete_registrationprofile', u'contenttypes.view_contenttype', u'sessions.add_session', u'auth.delete_group', u'auth.delete_user', u'myprofile.view_hrworker', u'transaction.add_sender', u'myprofile.add_hrworker', u'admin.add_logentry', u'sessions.view_session', u'auth.change_user', u'myprofile.add_routineusers', u'contenttypes.add_contenttype', u'myprofile.view_allusers', u'myprofile.change_routineusers'])"
>>> import re
>>> pat = re.compile('(?<=u\')([a-z]+).([a-z]+).([a-z]+)')
>>> pat.findall(str)
[('auth', 'view', 'group'), ('auth', 'add', 'user'), ('transaction', 'change', 'sender'), ('myprofile', 'view', 'department'), ('auth', 'add', 'permission'), ('transaction', 'add', 'receiver'), ('myprofile', 'delete', 'routineusers'), ('transaction', 'change', 'receiver'), ('sessions', 'change', 'session'), ('transaction', 'delete', 'receiver'), ('registration', 'change', 'registrationprofile'), ('auth', 'view', 'user'), ('auth', 'delete', 'permission'), ('transaction', 'view', 'sender'), ('myprofile', 'change', 'hrworker'), ('registration', 'add', 'registrationprofile'), ('admin', 'view', 'logentry'), ('registration', 'view', 'registrationprofile'), ('contenttypes', 'change', 'contenttype'), ('myprofile', 'view', 'routineusers'), ('sessions', 'delete', 'session'), ('myprofile', 'change', 'department'), ('myprofile', 'delete', 'hrworker'), ('admin', 'change', 'logentry'), ('myprofile', 'change', 'allusers'), ('admin', 'delete', 'logentry'), ('transaction', 'delete', 'sender'), ('auth', 'add', 'group'), ('myprofile', 'delete', 'department'), ('myprofile', 'add', 'department'), ('contenttypes', 'delete', 'contenttype'), ('auth', 'view', 'permission'), ('myprofile', 'add', 'allusers'), ('myprofile', 'delete', 'allusers'), ('auth', 'change', 'permission'), ('transaction', 'view', 'receiver'), ('auth', 'change', 'group'), ('registration', 'delete', 'registrationprofile'), ('contenttypes', 'view', 'contenttype'), ('sessions', 'add', 'session'), ('auth', 'delete', 'group'), ('auth', 'delete', 'user'), ('myprofile', 'view', 'hrworker'), ('transaction', 'add', 'sender'), ('myprofile', 'add', 'hrworker'), ('admin', 'add', 'logentry'), ('sessions', 'view', 'session'), ('auth', 'change', 'user'), ('myprofile', 'add', 'routineusers'), ('contenttypes', 'add', 'contenttype'), ('myprofile', 'view', 'allusers'), ('myprofile', 'change', 'routineusers')]
👤aelor

0👍

Easy if you use split():

s = u'myprofile.view_department'
temp = s.split('.')
words = [temp[0]] + temp[1].split('_')
print words
# [u'myprofile', u'view', u'department']

Leave a comment