简体   繁体   English

如何在循环语句之外打印正确的 output

[英]How to print a correct output outside of a loop statement

When I run the following code, I get the output below, which is correct:当我运行以下代码时,我得到下面的 output,这是正确的:

for object in buck:
    if object.url.startswith("https://....01fhekh01") \
    or object.url.startswith("https://...01fkejrig05"):
        print(object)

Output: Output:

<Object url='https://....01fhekh01-fkjflekjl' etag='"ewjhwehroie30923r"'>
<Object url='https://.......01fkejrig05-fjkjefojeof' etag='"265af8a7a0b69d3c173d40916fc2c5eb"'>

However, when I print my output outside of the loop statement, I get a different output, which is wrong:但是,当我在循环语句之外打印 output 时,我得到一个不同的 output,这是错误的:

for object in buck:
    if object.url.startswith("https://....01fhekh01") \
    or object.url.startswith("https://...01fkejrig05"):
        print(object)

print(object)

Output: Output:

*correct output from the print inside the loop statement*

wrong output from the print outside of the loop statement:
<Object url='https://....kjwekjweijtoeirjtoeri' etag='"sfakjlakfjsf"'>

How can I print the correct output out of the loop statement?如何在循环语句外打印正确的 output?

hi you can try this:嗨,你可以试试这个:

 ls=[]
 for object in buck:
    if object.url.startswith("https://....01fhekh01") \
    or object.url.startswith("https://...01fkejrig05"):
       ls.append(object)
print(ls)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM