python - Exclude the last delimited item from my list -


i want remove last string in list i.e. library name (delimited '\'). text string have contains path of libraries used @ compilation time. these libraries delimited spaces. want retain each path not till library name, 1 root before it.

example:

text = " /opt/gcc/4.4.4/snos/lib/gcc/x86_64-suse-linux/4.4.4/crtbegint.o /opt/gcc/4.4.4/snos/lib/gcc/x86_64-suse-linux/4.4.4/crtfastmath.o /opt/cray/cce/8.2.5/craylibs/x86-64/no_mmap.o /opt/cray/cce/8.2.5/craylibs/x86-64/libcraymath.a /opt/cray/cce/8.2.5/craylibs/x86-64/libcraymp.a /opt/cray/atp/1.7.1/lib/libatpsighandler.a /opt/cray/atp/1.7.1/lib/libatpsighcommdata.a " 

i want output -

output_list = [/opt/gcc/4.4.4/snos/lib/gcc/x86_64-suse-linux/4.4.4, /opt/gcc/4.4.4/snos/lib/gcc/x86_64-suse-linux/4.4.4, /opt/cray/cce/8.2.5/craylibs/x86-64, /opt/cray/cce/8.2.5/craylibs/x86-64,  /opt/cray/cce/8.2.5/craylibs/x86-64,  /opt/cray/atp/1.7.1/lib, /opt/cray/atp/1.7.1/lib] 

and want remove duplicates in output_list list looks like.

new_output_list = [/opt/gcc/4.4.4/snos/lib/gcc/x86_64-suse-linux/4.4.4, /opt/cray/cce/8.2.5/craylibs/x86-64, /opt/cray/atp/1.7.1/lib] 

i getting results using split() function struggling discard library names path.

any appreciated.

you seem want (don't try , string operations paths, it's bound end badly):

import os  new_output_list = list(set(os.path.dirname(pt) pt in text.split())) 

os.path.dirname splits path it's gets directory name path. every item in text, split list based on white-space. done every item in series. remove duplicates, convert set , list.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -