如何在多行中编写 Python 条件?
How Can I Write a Python Condition in Multiline?
我有一个 Python 条件低于我需要在不同的行中写它我该怎么做?
if image_finder[0].find_all('img')[0]['src'].replace('//','https://') == 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png' or image_finder[0].find_all('img')[0]['src'].replace('//','https://') =='https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png':
print(x)
您可以,而且 this Whosebug answer 向您展示了所有(许多)分割线的方法。
但在这种情况下我会这样做:
found_image = image_finder[0].find_all('img')[0]['src'].replace('//','https://')
link_one = 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png'
link_two = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png'
if found_image == link_one or found_image == link_two:
print(x)
我们将创建一些变量来保持一切有序。
fixedLink = image_finder[0].find_all('img')[0]['src'].replace('//','https://')
option1 = 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png'
option2 = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png'
第一个选项(我的推荐)
if fixedLink in (option1, option2):
print(x)
第二个选项
if fixedLink == option1 or fixedLink == option2:
print(x)
url_one = 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png'
url_two = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png'
imagefinder = image_finder[0].find_all('img')[0]['src'].replace('//','https://')
if imagefinder == url_one or imagefinder == url_two:
print(x)
如果这样修改代码会怎样;
img_src_list = [
'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png',
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png']
image_link = image_finder[0].find_all('img')[0]['src'].replace('//', 'https://')
if image_link in img_src_list:
print(x)
我有一个 Python 条件低于我需要在不同的行中写它我该怎么做?
if image_finder[0].find_all('img')[0]['src'].replace('//','https://') == 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png' or image_finder[0].find_all('img')[0]['src'].replace('//','https://') =='https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png':
print(x)
您可以,而且 this Whosebug answer 向您展示了所有(许多)分割线的方法。
但在这种情况下我会这样做:
found_image = image_finder[0].find_all('img')[0]['src'].replace('//','https://')
link_one = 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png'
link_two = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png'
if found_image == link_one or found_image == link_two:
print(x)
我们将创建一些变量来保持一切有序。
fixedLink = image_finder[0].find_all('img')[0]['src'].replace('//','https://')
option1 = 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png'
option2 = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png'
第一个选项(我的推荐)
if fixedLink in (option1, option2):
print(x)
第二个选项
if fixedLink == option1 or fixedLink == option2:
print(x)
url_one = 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png'
url_two = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png'
imagefinder = image_finder[0].find_all('img')[0]['src'].replace('//','https://')
if imagefinder == url_one or imagefinder == url_two:
print(x)
如果这样修改代码会怎样;
img_src_list = [
'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png',
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png']
image_link = image_finder[0].find_all('img')[0]['src'].replace('//', 'https://')
if image_link in img_src_list:
print(x)