By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

simple question on list manipulation from a newbie

P: n/a
Dear all,

I am working with wordnet and I am a python newbie. I'd like to know
how can I transfer a list below

In [69]: dog
Out[69]:
[{noun: dog, domestic_dog, Canis_familiaris},
{noun: frump, dog},
{noun: dog},
{noun: cad, bounder, blackguard, dog, hound, heel},
{noun: frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst,
weenie},
{noun: pawl, detent, click, dog},
{noun: andiron, firedog, dog, dog-iron}]

to a list like this with python:

[dog, domestic_dog, Canis_familiaris,
frump, dog,
dog,
cad, bounder, blackguard, dog, hound, heel,
frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst,
weenie},
pawl, detent, click, dog},
andiron, firedog, dog, dog-iron]

Thank you.

Sengly

Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Sengly wrote:
Dear all,

I am working with wordnet and I am a python newbie. I'd like to know
how can I transfer a list below

In [69]: dog
Out[69]:
[{noun: dog, domestic_dog, Canis_familiaris},
{noun: frump, dog},
{noun: dog},
{noun: cad, bounder, blackguard, dog, hound, heel},
{noun: frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst,
weenie},
{noun: pawl, detent, click, dog},
{noun: andiron, firedog, dog, dog-iron}]

to a list like this with python:

[dog, domestic_dog, Canis_familiaris,
frump, dog,
dog,
cad, bounder, blackguard, dog, hound, heel,
frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst,
weenie},
pawl, detent, click, dog},
andiron, firedog, dog, dog-iron]

Thank you.

Sengly
You should at least tell us what you have tried and where this information is
coming from and in what format.

Assumptions:

1) is a string containing what is shown (not a list of dictionaries)
2) the result you want is also a string (not a list)

out = '[{noun: dog, domestic_dog, Canis_familiaris},{noun: frump, dog},' \
'{noun: dog},{noun: cad, bounder, blackguard, dog, hound, heel},' \
'{noun: frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst, ' \
'weenie}, {noun: pawl, detent, click, dog}, {noun: andiron, firedog, ' \
'dog, dog-iron}]'

l = out.replace('}', '')[1:-1].split('{noun: ')[1:]

results = []
for entry in l:
words = [x.strip() for x in entry.split(',')]
results.extend(words)

print results
Note: this is very specific to the contents you show. If you need a general
solution you will either need to use regexes or look a pyparsing.

-Larry
Jun 27 '08 #2

P: n/a
On Jun 8, 5:14 am, Sengly <Sengly.H...@gmail.comwrote:
Dear all,

I am working with wordnet and I am a python newbie. I'd like to know
how can I transfer a list below

In [69]: dog
Out[69]:
[{noun: dog, domestic_dog, Canis_familiaris},
{noun: frump, dog},
{noun: dog},
{noun: cad, bounder, blackguard, dog, hound, heel},
{noun: frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst,
weenie},
{noun: pawl, detent, click, dog},
{noun: andiron, firedog, dog, dog-iron}]
Doesn't look like a Python list to me. Is domestic_dog a variable? If
so, what's that "-" in "dog-iron"?
>
to a list like this with python:

[dog, domestic_dog, Canis_familiaris,
frump, dog,
dog,
cad, bounder, blackguard, dog, hound, heel,
frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst,
weenie},
Do you really want the "}" in the above line and the next line?
pawl, detent, click, dog},
andiron, firedog, dog, dog-iron]

Thank you.

Sengly
Jun 27 '08 #3

P: n/a
Sengly wrote:
Dear all,

I am working with wordnet and I am a python newbie. I'd like to know
how can I transfer a list below

In [69]: dog
Out[69]:
[{noun: dog, domestic_dog, Canis_familiaris},
{noun: frump, dog},
{noun: dog},
{noun: cad, bounder, blackguard, dog, hound, heel},
{noun: frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst,
weenie},
{noun: pawl, detent, click, dog},
{noun: andiron, firedog, dog, dog-iron}]

to a list like this with python:

[dog, domestic_dog, Canis_familiaris,
frump, dog,
dog,
cad, bounder, blackguard, dog, hound, heel,
frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst,
weenie},
pawl, detent, click, dog},
andiron, firedog, dog, dog-iron]
I can't help you with the formatting, but here's a solution using Python
data structures:
>>alist = [
{'noun': ('dog', 'domestic_dog', 'Canis_familiaris')},
{'noun': ('frump', 'dog')},
{'noun': ('dog',)},
{'noun': ('cad', 'bounder', 'blackguard', 'dog', 'hound', 'heel')},
{'noun': ('frank', 'frankfurter', 'hotdog', 'hot_dog', 'dog',
'wiener', 'wienerwurst', 'weenie')},
{'noun': ('pawl', 'detent', 'click', 'dog')},
{'noun': ('andiron', 'firedog', 'dog', 'dog-iron')},
]
>>merged = {}
for d in alist:
for key, value in d.iteritems():
merged.setdefault(key, []).extend(value)
>>merged
{'noun': ['dog', 'domestic_dog', 'Canis_familiaris', 'frump', 'dog',
'dog', 'cad', 'bounder', 'blackguard', 'dog', 'hound', 'heel', 'frank',
'frankfurter', 'hotdog', 'hot_dog', 'dog', 'wiener', 'wienerwurst',
'weenie', 'pawl', 'detent', 'click', 'dog', 'andiron', 'firedog', 'dog',
'dog-iron']}
Jun 27 '08 #4

P: n/a
On Jun 8, 6:38*am, Sam Denton <stden...@sbcglobal.netwrote:
Sengly wrote:
Dear all,
I am working with wordnet and I am a python newbie. I'd like to know
how can I transfer a list below
In [69]: dog
Out[69]:
[{noun: dog, domestic_dog, Canis_familiaris},
*{noun: frump, dog},
*{noun: dog},
*{noun: cad, bounder, blackguard, dog, hound, heel},
*{noun: frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst,
weenie},
*{noun: pawl, detent, click, dog},
*{noun: andiron, firedog, dog, dog-iron}]
to a list like this with python:
[dog, domestic_dog, Canis_familiaris,
frump, dog,
dog,
cad, bounder, blackguard, dog, hound, heel,
frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst,
weenie},
pawl, detent, click, dog},
andiron, firedog, dog, dog-iron]

I can't help you with the formatting, but here's a solution using Python
data structures:

*>>alist = [
* * *{'noun': ('dog', 'domestic_dog', 'Canis_familiaris')},
* * *{'noun': ('frump', 'dog')},
* * *{'noun': ('dog',)},
* * *{'noun': ('cad', 'bounder', 'blackguard', 'dog', 'hound', 'heel')},
* * *{'noun': ('frank', 'frankfurter', 'hotdog', 'hot_dog', 'dog',
'wiener', 'wienerwurst', 'weenie')},
* * *{'noun': ('pawl', 'detent', 'click', 'dog')},
* * *{'noun': ('andiron', 'firedog', 'dog', 'dog-iron')},
* * *]

*>>merged = {}
*>>for d in alist:
* * * * *for key, value in d.iteritems():
* * * * * * *merged.setdefault(key, []).extend(value)

*>>merged
{'noun': ['dog', 'domestic_dog', 'Canis_familiaris', 'frump', 'dog',
'dog', 'cad', 'bounder', 'blackguard', 'dog', 'hound', 'heel', 'frank',
'frankfurter', 'hotdog', 'hot_dog', 'dog', 'wiener', 'wienerwurst',
'weenie', 'pawl', 'detent', 'click', 'dog', 'andiron', 'firedog', 'dog',
'dog-iron']}
Thank you all for your help. I found a solution as the following:

alist = [
{'noun': 'dog', 'domestic_dog', 'Canis_familiaris'},
{'noun': 'frump', 'dog'},
{'noun': 'dog',},
{'noun': 'cad', 'bounder', 'blackguard', 'dog', 'hound', 'heel'},
{'noun': 'frank', 'frankfurter', 'hotdog', 'hot_dog', 'dog',
'wiener', 'wienerwurst', 'weenie'},
{'noun': 'pawl', 'detent', 'click', 'dog'},
{'noun': 'andiron', 'firedog', 'dog', 'dog-iron'},
]

def getAll(alist):
list=[]
for i in range(0,len(alist)):
list.extend(alist[i][:])
return list

Kind regards,

Sengly
Jun 27 '08 #5

P: n/a
On Jun 8, 4:42 pm, Sengly <Sengly.H...@gmail.comwrote:
[snip]
Thank you all for your help. I found a solution as the following:
The following is a solution to what?
>
alist = [
{'noun': 'dog', 'domestic_dog', 'Canis_familiaris'},
That is not yet valid Python. You will get a syntax error pointing to
the comma after 'domestic_dog'. Do you mean:
(1) alist = [
{'noun': ('dog', 'domestic_dog', 'Canis_familiaris')},
{'noun': ('frump', 'dog')},
# etc
]
or (2)
alist = [
('dog', 'domestic_dog', 'Canis_familiaris'),
('frump', 'dog'),
# etc
]
or (3) something else?
{'noun': 'frump', 'dog'},
{'noun': 'dog',},
{'noun': 'cad', 'bounder', 'blackguard', 'dog', 'hound', 'heel'},
{'noun': 'frank', 'frankfurter', 'hotdog', 'hot_dog', 'dog',
'wiener', 'wienerwurst', 'weenie'},
{'noun': 'pawl', 'detent', 'click', 'dog'},
{'noun': 'andiron', 'firedog', 'dog', 'dog-iron'},
]

def getAll(alist):
list=[]
for i in range(0,len(alist)):
list.extend(alist[i][:])
Note: the use of [:] to copy the contents of each element indicates
that you think that each element is a sequence (list/tuple/whatever)
i.e. option (2) above.
return list
Whatever that is solving, it could be written much more clearly as:
answer = [] # don't shadow the builtin list function
# and use meaningful names
for sublist in alist:
answer.extend(sublist[:])
return answer

Aside: Why do you think you need to copy sublist? Does the plot
involve later mutation of alist?

And it can be done even more clearly using a list comprehension:
[element for sublist in alist for element in sublist]

HTH,
John
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.