简体   繁体   中英

Retrieve items from a list

I retrieve elements from a list with code:

re.findall(r"[\w\.]+",str(dirs))
# data dirs
dirs = ['',
     'Applications              cores                     sbin',
     'Library                   dev                       tmp',
     'Network                   etc                       usr',
     'System                    home                      var',
     'Users                     installer.failurerequests vm',
     'Volumes                   net',
     'bin                       private',
     '']

It output:

['Applications', 'cores', 'sbin', 'Library', 'dev', 'tmp', 'Network',    'etc', 'usr', 'System', 'home', 'var', 'Users', 'installer.failurerequests', 'vm', 'Volumes', 'net', 'bin', 'private']

Is it a best practice to convert a list to str while using 're'?

data = [string for value in dirs for string in value.split()]
>>>data
['Applications',
 'cores',
 'sbin',
 'Library',
 'dev',
 'tmp',
 'Network',
 'etc',
 'usr',
 'System',
 'home',
 'var',
 'Users',
 'installer.failurerequests',
 'vm',
 'Volumes',
 'net',
 'bin',
 'private']

I collect the methods of string for your reference.

    string_dict ={
    'manipulate':{'format': ['format', 'format_map'],
                 'split': ['join','split', 'rsplit','splitlines', 'partition', 'rpartition'],
                 'alter': ['replace', 'lstrip', 'rstrip', 'strip']},
    'search': ['endswith', 'startswith', 'count', 'index', 'find','rindex', 'rfind'],
    'text':{'font': ['lower', 'upper', 'capitalize', 'title', 'swapcase'],
            'condition': ['isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isnumeric','isidentifier',
                          'islower','istitle', 'isupper','isprintable', 'isspace',],
            'text': ['center', 'ljust', 'rjust', 'zfill', 'expandtabs','casefold'],
            'encode': ['translate', 'maketrans', 'encode']}
    }

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