嗨,我对python和编程很陌生,我将如何将它们结合起来:
if "Web" in source:
source = "WEB"
if ((source == "Blu-ray") and (other == "Remux") and (reso == "1080p")):
reso = "BD Remux"
if "DVD" in name:
reso = "DVD Remux"
if ((source == "Ultra HD Blu-ray") and (other == "Remux") and (reso == "2160p")):
reso = "UHD Remux"
if source == "Ultra HD Blu-ray":
source = "Blu-ray"发布于 2021-01-31 05:11:01
您可以使用elif clause扩展带有额外条件的if语句:
mystring='what will it print?'
if mystring == 'hello':
print('world!')
elif mystring == 'good':
print('bye!')
elif mystring == 'how':
print('are you?')
else:
print('I ran out of ideas!') [out]: I ran out of ideas!对您的示例稍加改写如下:
source='Ultra HD Blu-ray'
name='DVD'
reso='2160p'
other='Remux'
resos={'1080p':'BD Remux','2160p':'UHD Remux'}
if "Web" in source:
source = "WEB"
elif "Blu-ray" in source and other == "Remux":
source = "Blu-ray"
reso = resos.get(reso,'UNDEFINED')
elif "DVD" in name:
reso = "DVD Remux"
print(source, name, reso)[out]: Blu-ray DVD UHD Remux请注意,我使用了resos字典来替换两个if标记,更详细地介绍了这个here。
发布于 2021-01-31 05:57:20
将这么多的语句合并成一行(问题是一行吗?)可能不会是"Pythonic"。括号也是不必要的。
if "Web" in source:
source = "WEB"
elif source == "Blu-ray" and other == "Remux" and reso == "1080p":
reso = "BD Remux"
elif "DVD" in name:
reso = "DVD Remux"
elif source == "Ultra HD Blu-ray" and other == "Remux" and reso == "2160p":
reso = "UHD Remux"
elif source == "Ultra HD Blu-ray":
source = "Blu-ray"
else:
source = ""
reso = ""https://stackoverflow.com/questions/65975866
复制相似问题