tags:

views:

105

answers:

5

I have extracted some url list and want to manipulate this list. Following is extracted list sample:

http://help.naver.com/service/svc_index.jsp?selected_nodeId=NODE0000000235
http://www.naver.com/rules/service.html
http://news.naver.com/main/principle.nhn
http://www.naver.com/rules/privacy.html
http://www.naver.com/rules/disclaimer.html
http://help.naver.com/claim_main.asp
http://news.naver.com/main/ombudsman/guidecenter.nhn?mid=omb
http://www.nhncorp.com/
http://www.nhncorp.com/

I want to extract only URLs that start with 'http://www.naver.com', so finally what I want list is following

http://www.naver.com/rules/privacy.html
http://www.naver.com/rules/disclaimer.html
http://www.naver.com/rules/service.html

How can I only extract what I want?

+6  A: 

If your old list is contains all urls as strings you can use a list comprehension to filter them.

new = [url for url in old if url.startswith('http://www.naver.com')]

You could write it as a explicit loop, but it adds nothing but lines of code:

new = []
for url in old:
   if url.startswith('http://www.naver.com'):
       new.append( url )

If you planned on removing items from the original list while looping over it: Don't ever do that, it won't work. You can modify the original list instead with the same LC:

old[:] = [url for url in old if url.startswith('http://www.naver.com')]
THC4k
If the OP really is a beginner show him the non-list comprehension equivalent too. (Not that it's hard to derive but I think it's good to actually *see* the difference.)
quark
hello, old[:] = [url for url in old if url.startswith('http://www.naver.com')]this one is working well for me .thanks again!
paul
+2  A: 

You can do this with a List Comprehension. These are a very powerful way to work with lists with Python.

By adding add an if to the list comprehension you can filter the list.

Assuming your URLs are stored in the variable myurls:

filteredurls = [url for url in myurls if url.startswith('http://www.naver.com')]
Dave Webb
Naming your variable "list" in Python is a very bad practice :]
yk4ever
@yk4ever - Good point; have changed it.
Dave Webb
A: 
urlList = [ ... ] # your list of urls
extractedList = [url for url in urlList if url.startswith('http://www.naver.com')]
Wookai
You define `urlList` to be a dict, not a list...
gnud
@gnud: and then it's not being used ;)
SilentGhost
Lol, I guess I was a bit in a hurry. Thanks anyway.
Wookai
A: 
result = []
for url in myListOfUrls:
    if 'http://www.naver.com' in url:
        result.append(url)
inspectorG4dget
This is the non-list comprehension version. Does it work by coincidence? Could `http://www.naver.com` appear later on in a url?
quamrana
@quamrana: `'http://www.naver.com'` could be anywhere in `url` according to this code
SilentGhost
I did this to accommodate for leading whitespaces and other markups. The URL itself should "technically" not appear anywhere but at the start, so this code should not error.
inspectorG4dget
@inspectorG4dget: Could `'http://naver.com'` appear after a `?` in one of the urls?
quamrana
I don't see why it would, unless we're talking about something like PHP scripts. But in that case, it would be allowed by my code
inspectorG4dget
Hello all,really thanks for many people's help!i will try all method and will answer soonthanks :)
paul
A: 

Someone suggested this alternative answer based on filter() but deleted it, I'll post it here again for completeness:

newList = filter(lambda url: url.startswith('http://www.naver.com'), oldList)

The list comprehension method seems faster though (and in my opinion, more readable):

$ python -m timeit -c "filter(lambda url: url.startswith('1'), map(str, range(100)))"
10000 loops, best of 3: 143 usec per loop

$ python -m timeit -c "[ url for url in map(str, range(100)) if url.startswith('1') ]"
10000 loops, best of 3: 117 usec per loop
Wim