Extract some interested part of list value

From this list

List = ['/asd/dfg/ert.py','/wer/cde/xcv.img']

Got this

List = ['ert.py','xcv.img']

Answers


There's a low-level split-based approach:

>>> a = ['/asd/dfg/ert.py','/wer/cde/xcv.img']
>>> b = [elem.split("/")[-1] for elem in a]
>>> b
['ert.py', 'xcv.img']

Or a higher-level, more descriptive approach, which is probably more robust:

>>> import os
>>> b = [os.path.basename(filename) for filename in a]
>>> b
['ert.py', 'xcv.img']

Of course this assumes that I've guessed right about what you wanted; your example is somewhat underspecified.


$List = array('/asd/dfg/ert.py','/wer/cde/xcv.img');
$pattern = "#/.*/#";
foreach ($List AS $key => $str)
  $List[$key] = preg_replace($pattern, '', $str);

print_r($List);

Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.