【发布时间】:2014-07-08 11:00:03
【问题描述】:
我可以对字符串列表进行切片吗?如果有可能,任何人都可以告诉我该怎么做,以便我能够打印出一个特定的字符串,而不是组成列表的五个字符串。 干杯。
eg.
mylist = ['apples' 'oranges' 'lemons' 'cucumbers' 'bananas']
print 'orange'
** 我使用的编程语言是 python。每次我将它编码为 mylist[2] 时,它都会出现错误。我正在使用的列表是从 html rss 提要中提取字符串。每个字符串都是一个新的新闻标题。然而,即使它不断更新,列表中总是有 5 个字符串,它告诉我列表索引超出范围。但是,如果我只打印整个列表,它就可以正常工作**
#URLS for RSS Feeds
url_national = 'http://feeds.news.com.au/public/rss/2.0/news_national_3354.xml'
url_sport = 'http://feeds.news.com.au/public/rss/2.0/news_sport_3168.xml'
url_world = 'http://feeds.news.com.au/public/rss/2.0/news_theworld_3356.xml'
url_technology = 'http://feeds.news.com.au/public/rss/2.0/news_tech_506.xml'
def headlines (url):
web_page = urlopen(url)
html_code = web_page.read()
web_page.close()
return findall(r'<item><title>([^<]*)</title>', html_code)
#headlines list
list_national = [headlines(url_national)]
list_sport = [headlines(url_sport)]
list_world = [headlines(url_world)]
list_technology = [headlines(url_technology)]
def change_category():
if label_colour.get() == 'n':
changeable_label['text'] = list_national #here I would slice it but it doesn't work
elif label_colour.get() == 's':
changeable_label['text'] = list_sport
elif label_colour.get() =='w':
changeable_label['text'] = list_world
else:
changeable_label['text'] = list_technology
我需要将它分割成单独的标题的原因是,当为我的 GUI 按下单选按钮时,它会将它们打印在标签上的编号列表中,而不仅仅是在它们旁边的一行上运行 - 抱歉,我希望这会让感觉
【问题讨论】:
-
请提及/标记相关语言。也许你正在寻找这个? stackoverflow.com/questions/509211/pythons-slice-notation
-
您的示例列出了一个包含字符串的元素。 mylist[2] 在这种情况下永远不会起作用。
-
可能吧,抱歉我累了
标签: python string list rss slice