user1636419
user1636419

Reputation: 239

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']

Upvotes: 1

Views: 61

Answers (2)

Surace
Surace

Reputation: 711

$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);

Upvotes: -1

DSM
DSM

Reputation: 353079

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.

Upvotes: 6

Related Questions