1

从这个列表

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

明白啦

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

2 回答 2

6

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.

于 2012-09-21T16:28:38.220 回答
-1
$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);
于 2012-09-21T16:31:02.343 回答