简体   繁体   中英

Slicing a List of Strings

Am I able to slice a list of strings? If it is possible could anyone please tell me how to do it so that I am able to print out a particular string instead of the five that make up the list. Cheers.

eg.
mylist = ['apples' 'oranges' 'lemons' 'cucumbers' 'bananas']
print 'orange'

** The programming language i am using is python. Every time I code it mylist[2] it comes out as an error. The list I am using is extracting the strings from a html rss feed. Each string is a new news heading. However, even when it updates constantly there are always 5 strings in the list and it tells me list index out of range. But if I just print the entire list it works fine**

#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

the reason I need to slice it into individual heading is so when the radio button is pressed for my GUI it prints them in a numbered list on the label not all just running on one line next to them - sorry i hope that makes sense

What language are you using here? Usually you can use an index to access a particular entry in a list. For example:

print myList[1]

Commas are missing in your list creation. You have to do it like this:

 mylist = ['apples', 'oranges', 'lemons', 'cucumbers', 'bananas']

And you will be able to work with your list

mylist[0] # 'apples'
mylist[-1] # 'bananas'
mylist[2] # 'lemons'

I think the error you are getting is something like this:

mylist = ['apples' 'oranges' 'lemons' 'cucumbers' 'bananas']
print mylist[5]
IndexError: list index out of range

The reason is the elements in a list are indexed from 0 not 1 . The mylist has 5 elements starting from 0 to 4 . So when you call print mylist[5] it will definitely give an error as there is no 6th element in the list.

Here is the official doc regarding list please have a look. I hope it was helpful!

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