简体   繁体   中英

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:

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

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:

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

print(object)

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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