simple question on list manipulation from a newbie

S

Sengly

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
 
J

John Machin

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
 
S

Sam Denton

Sengly said:
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:
{'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')},
]
for key, value in d.iteritems():
merged.setdefault(key, []).extend(value)
{'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']}
 
S

Sengly

Sengly said:
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[:])
return list

Kind regards,

Sengly
 
J

John Machin

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[:])


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top