2017-05-18 30 views
0

我得到的main_watch_images列表中的兩個項目,只是要打印的第一個。我試圖在findall行之後添加[0],但是它會垂直打印每個字母。我想這是因爲for循環在新行上打印所有內容,但是我無法設法將它全部打印在一行上。Python 2.7版 - 讓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 

注意這不是和其他問題相同的問題。我是一個絕對的初學者,無法理解其他答案中使用的技巧和操作符等。我需要一個具體的答案來解決我的問題。

+0

這是什麼'的findAll()'返回? Cound你展示一個示例輸出? – Nurjan

+0

[關於同一行的Python打印]的可能的複製(http://stackoverflow.com/questions/5598181/python-print-on-same-line) – DeepSpace

+0

作爲對重複的說,使用'打印圖像的答案中的一個, '(注意',')。 – DeepSpace

回答

2

我在main_watch_images列表中獲取了兩個項目,只是想打印第一個項目 。

你可能會尋找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 

編輯

是啊,在這種情況下工作的,但是在這裏我要打印第二個有可能是一個實例所以希望打印由它們位於其中在列表中的項目,即[0],[1]等

嘗試使用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]) 
+0

是啊,在這種情況下工作,但可能有這樣的情況:我想打印第二個這樣希望通過打印在列表中的項目他們在哪裏,即[0],[1]等 – user88720

+0

那麼你應該在做任何事情之前將這些'圖像'存儲在一個集合中,即'list'。 'image_list.append('https://denissov.ru'+ images)'然後你可以簡單地執行'print image_list [0]' – Eduard

+0

是的main_watch_images是一個列表......但是我也需要if語句域添加到缺少它的圖像,然後我知道如何if語句是在for循環 – user88720