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 5 1023
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
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
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']}
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brian van den Broek |
last post by:
Hi all,
There have been a few posts over the last month or so expressing a bit
of exasperation with the "rising tide of newbie's". (Or, more
accurately, the rising tide of questions from...
|
by: Joseph Barron |
last post by:
Here is a SIMPLE problem that I'm trying to solve.
It works in Netscape 6.2, but IE6 gives ""No such interface
supported."
Below are page1.htm and page2.htm .
In page1.htm, there are two...
|
by: Dom |
last post by:
I have a frameset with a left right columns, called 'index' and
'content'.
The left column of the frameset ('index') loads an index.htm file
which have these hrefs (summarized the code a bit)...
|
by: Steve Lambert |
last post by:
Hi,
I've knocked up a number of small routines to create and manipulate a linked
list of any structure. If anyone could take a look at this code and give me
their opinion and details of any...
|
by: Pietje puk |
last post by:
Hello,
Since im quite new to ASP.NET i wanted to ask you folks what the best
way is to create a WebForm for modifying 1 field from a record. The
manipulation of this field can be done by using...
|
by: Ville Vainio |
last post by:
I tried to clear a list today (which I do rather rarely, considering
that just doing l = works most of the time) and was shocked, SHOCKED
to notice that there is no clear() method. Dicts have it,...
|
by: Rob |
last post by:
In order to create a date in CCYY-MM-DD fashion, I used to be able to use
the Format(date,"yyyy")&"-"&Format(month,"mm")... syntax...
This no longer appears to work in vb.net. I know how I...
|
by: rogynskyy |
last post by:
Hi guys,
I'm running MSDE 2000 A on Win XP
I've got a database with several tables, all of the tables display data in query manager.
I wrote this simple query:
Select
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |