从此列表中
List = ['/asd/dfg/ert.py','/wer/cde/xcv.img']
得到了这个
List = ['ert.py','xcv.img']
答案 0 :(得分:6)
基于split
的低级方法:
>>> a = ['/asd/dfg/ert.py','/wer/cde/xcv.img']
>>> b = [elem.split("/")[-1] for elem in a]
>>> b
['ert.py', 'xcv.img']
或更高级别,更具描述性的方法,可能更强大:
>>> import os
>>> b = [os.path.basename(filename) for filename in a]
>>> b
['ert.py', 'xcv.img']
当然,这假设我猜对了你想要的东西;你的例子有点不明确。
答案 1 :(得分:-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);