制作“如果”语句单行没有“其他”有没有办法在一个单一的线要做到这一点,但没有使用“无”?:在Python 3
folders = ['Project 1', 'Project 2']
files = os.listdir('/home/stavros/Documents/Python')
for i in files:
files.remove(i) if i in folders else None
一个能'跳过''其他'的声明?
在此先感谢。
制作“如果”语句单行没有“其他”有没有办法在一个单一的线要做到这一点,但没有使用“无”?:在Python 3
folders = ['Project 1', 'Project 2']
files = os.listdir('/home/stavros/Documents/Python')
for i in files:
files.remove(i) if i in folders else None
一个能'跳过''其他'的声明?
在此先感谢。
你应该做'文件= [_ _为在文件中,如果不_在文件夹]',Python的风格。 – zyxue
@zyxue:'_'仅适用于未使用的变量。 – Daniel
@zyxue不要使用'_',除非它是一次性变量,事实并非如此。 –