简体   繁体   中英

What is an efficient way to parse a list of strings in Python?

I am writing a script to validate a deb package is installing to a specific folder. I am pretty green and new to python. I had a script that worked with python-apt module that returned a list with file paths of all files in the package. Due to some dependency issues I can no longer use the python-apt module, so instead I am trying to call dpkg to collect the information and parse it to a list of file paths. Below is what I am using to get the list of items returned from the dpkg command. I need to parse out everything but the right of the last space. What would be the most efficient way to parse this?

self.lists = commands.getoutput("dpkg -c "+deb).split('\n')

results of this is this list:

list: ['drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:24 ./', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:14 ./opt/', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:14 ./opt/usr/', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:15 ./opt/usr/apps/', '-r--r--r-- ejohnson/ejohnson 179491 2012-03-06 15:15 ./opt/usr/apps/xbean-spring-2.8.jar', '-rw-r--r-- ejohnson/ejohnson    518 2012-03-06 15:15 ./opt/usr/apps/Hello.class', '-r--r--r-- ejohnson/ejohnson 1901653 2012-03-06 15:15 ./opt/usr/apps/spring-1.2.6.jar']

I want to reformat the list so that each item in the list would be the item after the last space for example ['./','./opt/','./opt/usr/','./opt/usr/apps/'...]

Thanks for looking

很简单,只需将您的列表放在l变量中,此代码应该适合您。

[el.split()[-1] for el in l]

Turn each of the strings into a list by using split . Take the last element of each list using relative list indexing.

For extra credit, do it in a one line list comprehension.

str.rpartition may be more efficient than str.split

[x.rpartition(" ")[2] for x in your_list]

For the sample here, it is more than twice as fast

$ python -m timeit -s "L=['drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:24 ./', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:14 ./opt/', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:14 ./opt/usr/', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:15 ./opt/usr/apps/', '-r--r--r-- ejohnson/ejohnson 179491 2012-03-06 15:15 ./opt/usr/apps/xbean-spring-2.8.jar', '-rw-r--r-- ejohnson/ejohnson    518 2012-03-06 15:15 ./opt/usr/apps/Hello.class', '-r--r--r-- ejohnson/ejohnson 1901653 2012-03-06 15:15 ./opt/usr/apps/spring-1.2.6.jar']" \
> "[x.split()[-1] for x in L]"
100000 loops, best of 3: 5.2 usec per loop

$ python -m timeit -s "L=['drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:24 ./', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:14 ./opt/', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:14 ./opt/usr/', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:15 ./opt/usr/apps/', '-r--r--r-- ejohnson/ejohnson 179491 2012-03-06 15:15 ./opt/usr/apps/xbean-spring-2.8.jar', '-rw-r--r-- ejohnson/ejohnson    518 2012-03-06 15:15 ./opt/usr/apps/Hello.class', '-r--r--r-- ejohnson/ejohnson 1901653 2012-03-06 15:15 ./opt/usr/apps/spring-1.2.6.jar']" \
> "[x.rpartition(' ')[2] for x in L]"
100000 loops, best of 3: 2.55 usec per loop

$ python -m timeit -s "L=['drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:24 ./', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:14 ./opt/', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:14 ./opt/usr/', 'drwxr-xr-x ejohnson/ejohnson 0 2012-03-06 15:15 ./opt/usr/apps/', '-r--r--r-- ejohnson/ejohnson 179491 2012-03-06 15:15 ./opt/usr/apps/xbean-spring-2.8.jar', '-rw-r--r-- ejohnson/ejohnson    518 2012-03-06 15:15 ./opt/usr/apps/Hello.class', '-r--r--r-- ejohnson/ejohnson 1901653 2012-03-06 15:15 ./opt/usr/apps/spring-1.2.6.jar']" \
> "[x.rsplit(' ',1)[1] for x in L]"
100000 loops, best of 3: 3.5 usec per loop

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