| | |
Removing duplicates from list in slightly different case
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 9
Reputation:
Solved Threads: 0
For an application, I need to parse a string which contains urls and their titles.
For example:
'name="My Mobile Blog" url="http://caydab565.blogspot.com/" name="Creative Disaster" url="http://kevinlara.blogspot.com/" ...'
name means title of url here.
I want list of strings which contain both title and url.
For example:
['name="My Mobile Blog" url="http://caydab565.blogspot.com/"','name="Creative Disaster" url="http://kevinlara.blogspot.com/"'] for the above string
This is very simple and I know how to do it using re module.
I want to get list of strings like above but their titles are unique.
For example:
'name="Creative Disaster" url="http://abc122.blogspot.com/" name="My Mobile Blog" url="http://caydab565.blogspot.com/" name="Creative Disaster" url="http://kevinlara.blogspot.com/" ...'
From the above string I want list of strings like below:
['name="Creative Disaster" url="http://abc122.blogspot.com/" ,'name="My Mobile Blog" url="http://caydab565.blogspot.com/"']
Can any one help in this?
Thanks in advance.
Dilip Kumar Kola
For example:
'name="My Mobile Blog" url="http://caydab565.blogspot.com/" name="Creative Disaster" url="http://kevinlara.blogspot.com/" ...'
name means title of url here.
I want list of strings which contain both title and url.
For example:
['name="My Mobile Blog" url="http://caydab565.blogspot.com/"','name="Creative Disaster" url="http://kevinlara.blogspot.com/"'] for the above string
This is very simple and I know how to do it using re module.
I want to get list of strings like above but their titles are unique.
For example:
'name="Creative Disaster" url="http://abc122.blogspot.com/" name="My Mobile Blog" url="http://caydab565.blogspot.com/" name="Creative Disaster" url="http://kevinlara.blogspot.com/" ...'
From the above string I want list of strings like below:
['name="Creative Disaster" url="http://abc122.blogspot.com/" ,'name="My Mobile Blog" url="http://caydab565.blogspot.com/"']
Can any one help in this?
Thanks in advance.
Dilip Kumar Kola
I think this function should help you
python Syntax (Toggle Plain Text)
import re keyPatt = re.compile(r"\b\w+=") testData='name="My Mobile Blog" url="http://caydab565.blogspot.com/" name="Creative Disaster" url="http://kevinlara.blogspot.com/" ...' def gen_pairs(dataString): key, pos = None, 0 for match in keyPatt.finditer(dataString): startPos, endPos = match.span() if key is not None: value = dataString[pos:startPos].strip() yield (key, value) key, pos = dataString[startPos:endPos-1], endPos if key is not None: value = dataString[pos:].strip() yield (key, value) for item in gen_pairs(testData): print item
•
•
Join Date: Mar 2009
Posts: 9
Reputation:
Solved Threads: 0
Thank for replying Gribouillis,
I find your solution little difficult to understand.
I find a solution on myself:
I find your solution little difficult to understand.
I find a solution on myself:
Python Syntax (Toggle Plain Text)
#lets say I already got list of string from a big string containing so many urls and titles strings = ['name="Creative Disaster" url="http://kevinlara121.blogspot.com/"','name="My Mobile Blog" url="http://caydab565.blogspot.com/"','name="Creative Disaster" url="http://kevinlara.blogspot.com/"'] d={}; f={} for string in strings: index=url.find('url="') d[url[6:index-2]]=url[index+5:] for t,u in d.items(): f[u]=t strings=f.items() """ strings = [('Creative Disaster, 'http://kevinlara.blogspot.com/'),('My Mobile Blog' , 'http://caydab565.blogspot.com/')] """
Last edited by dilipkk; Mar 29th, 2009 at 3:02 am.
I see. In fact I wrote a function which can handle general data having the form
It only supposes that the values don't contain the '=' sign and that the keys are made of one or more alphanumeric characters.
python Syntax (Toggle Plain Text)
'key1=value1 key2=value2 key3=value3'
![]() |
Other Threads in the Python Forum
- Previous Thread: argument passing :/
- Next Thread: Pig Latin translator..
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied apache application argv beginner book change code color dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook port prime program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse rpg scrolledtext server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython





