简体   繁体   English

Python 2.7-进行循环打印而无需换行

[英]Python 2.7 - make for loop print without new line

I am getting two items in the main_watch_images list and just want to print the first one. 我在main_watch_images列表中得到两个项目,只想打印第一个。 I have tried to add [0] after the findall line, however it then prints every letter vertically. 我试图在findall行之后添加[0],但是它随后垂直打印每个字母。 I figured this is because the for loop prints everything on a new line, but I can't manage to make it print all on one line. 我认为这是因为for循环将所有内容打印在新行上,但是我无法使其全部打印在一行上。

# Extract the first image (this is always the main image) that is in the
# "LIMITED" category
main_watch_images = findall('<img.* src="([^"]+).*LIMITED"', dennisov_html)
# Convert the URL to a full address
for images in main_watch_images:
    if images.startswith('/'):
        images = 'https://denissov.ru' + images 
    print images

Note this is NOT the same question as the other ones. 请注意,这是与其他问题不同的问题。 I am an absolute beginner and cannot understand the techniques and operators etc. used in the other answers. 我是一个绝对的初学者,无法理解其他答案中使用的技术和运算符等。 I need a specific answer to my problem. 我需要针对我的问题的具体答案。

I am getting two items in the main_watch_images list and just want to print the first one. 我在main_watch_images列表中得到两个项目,只想打印第一个。

You might be looking for break 您可能正在寻找break

# Extract the first image (this is always the main image) that is in the
# "LIMITED" category
main_watch_images = findall('<img.* src="([^"]+).*LIMITED"', dennisov_html)
# Convert the URL to a full address
for images in main_watch_images:
    if images.startswith('/'):
        images = 'https://denissov.ru' + images
        print images
        break
    print images

EDIT 编辑

Ah yes that works in this instance, however there may be an instance where I want to print the second one so was hoping to print the items in the list by where they are located ie [0], [1] etc. 啊,是的,在这种情况下可行,但是在某些情况下,我想打印第二个,因此希望按列表中的项目(即[0],[1]等)打印列表中的项目。

Try using a list 尝试使用list

# Extract the first image (this is always the main image) that is in the
# "LIMITED" category
main_watch_images = findall('<img.* src="([^"]+).*LIMITED"', dennisov_html)
# Convert the URL to a full address
image_list = []
for images in main_watch_images:
    if images.startswith('/'):
        images = 'https://denissov.ru' + images
    image_list.append(images)

number_of_images_i_want_to_show = 1
for ctr in range(0,number_of_images_i_want_to_show):
    print(image_list[ctr])

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

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