FAQ
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

Search Discussions

  • Larry Bates at Jun 7, 2008 at 8:06 pm

    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
  • John Machin at Jun 7, 2008 at 9:13 pm

    On Jun 8, 5:14 am, 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}]
    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
  • Sam Denton at Jun 7, 2008 at 9:38 pm

    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']}
  • Sengly at Jun 8, 2008 at 6:42 am

    On Jun 8, 6:38?am, Sam Denton wrote:
    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
  • John Machin at Jun 8, 2008 at 8:36 am
    On Jun 8, 4:42 pm, Sengly wrote:
    [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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 7, '08 at 7:14p
activeJun 8, '08 at 8:36a
posts6
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase