1

Хочу сделать, чтобы во время выполнения программы из списка из 100 ссылок выводилась одна случайная, потом другая случайная и так до момента пока список не станет пустым и не повторялась с предыдущей. При этом исходный список уменьшатся

import random




saity=['http://lenta.ru/rss/news','http://www.kommersant.ru/RSS/main.xml','http://www.kommersant.ru/RSS/news.xml','http://rssportal.ru/feed/173256.xml','http://www.levada.ru/rss.xml','http://wsrss.bbc.co.uk/russian/index.xml','http://inosmi.ru/misc/export/xml/rss/translation.xml','http://news.google.ru/news?ned=ru_ru&topic=t&output=atom','http://112.ua/rss','http://thebestvideo.net/feed','http://feeds.feedburner.com/Astrobene']
len_saity =len(saity)


for i in range(len_saity):
    len_saity =len(saity)
    print('len_saity '+str(len_saity))
    random1=int(random.randint(0, len_saity))
    print ('random1 '+str(random1))
    ssylka = str(saity([random1])) #wytaskiwajet
    del random1[random1]
    print ('ssylka '+str(ssylka))
    print ('список выглядит так: '+str(saity))

Ошибка выглядит так:

line 15, in <module>
    ssylka = str(saity([random1])) #wytaskiwajet
TypeError: 'list' object is not callable

2 ответа 2

4
In [26]: saity=['http://lenta.ru/rss/news','http://www.kommersant.ru/RSS/main.xml','http://www.kommersa
    ...: nt.ru/RSS/news.xml','http://rssportal.ru/feed/173256.xml','http://www.levada.ru/rss.xml','http
    ...: ://wsrss.bbc.co.uk/russian/index.xml','http://inosmi.ru/misc/export/xml/rss/translation.xml','
    ...: http://news.google.ru/news?ned=ru_ru&topic=t&output=atom','http://112.ua/rss','http://thebestv
    ...: ideo.net/feed','http://feeds.feedburner.com/Astrobene']
    ...:

In [27]: for i in reversed(range(len(saity))):
    ...:     print(saity.pop(random.randint(0, i)))
    ...:
http://inosmi.ru/misc/export/xml/rss/translation.xml
http://112.ua/rss
http://www.kommersant.ru/RSS/news.xml
http://rssportal.ru/feed/173256.xml
http://news.google.ru/news?ned=ru_ru&topic=t&output=atom
http://feeds.feedburner.com/Astrobene
http://thebestvideo.net/feed
http://www.levada.ru/rss.xml
http://lenta.ru/rss/news
http://wsrss.bbc.co.uk/russian/index.xml
http://www.kommersant.ru/RSS/main.xml

Или:

In [32]: while saity:
    ...:     print(saity.pop(random.randint(0, len(saity) - 1)))

Еще такой вариант, приближенный к вашему:

In [38]: while saity:
    ...:     elem = random.choice(saity)
    ...:     print(elem)
    ...:     saity.remove(elem)
0

При обращении к элементу списка круглые скобки не нужны. Поэтому и ошибка - вы обращаетесь к переменной как к функции. Так же в данном случае не нужно конвертировать в строку, т.к. в списке уже лежат строки. Проблемную строчку кода нужно исправить на

ssylka = saity[random1]

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.