4

I have python list like below.

lst = ['paragraph 1','paragraph 2','paragraph 3']

I'm trying to make corpus of them with Pattern library - http://www.clips.ua.ac.be/pages/pattern-vector

As their example it should be like this ..

d1 = Document('paragraph 1')
d2 = Document('paragraph 2')
d3 = Document('paragraph 3')

corpus = Corpus(documents=[d1,d2,d3])

How can i make corpus liks this with my python list ?

3
  • 1
    You should change list to paragraphs or something else as you are losing access to the built-in list Commented Apr 21, 2012 at 6:15
  • Ah ;) I just showing example there. Not the actual list. Commented Apr 21, 2012 at 6:30
  • 1
    Even for an example it's bad practice but almost everybody does this on every question on this site... Commented Apr 21, 2012 at 6:32

4 Answers 4

6
lst = ['paragraph 1','paragraph 2','paragraph 3']
corpus = Corpus(documents=[Document(x) for x in lst])
Sign up to request clarification or add additional context in comments.

Comments

5

You can use the map method

l = ['paragraph 1','paragraph 2','paragraph 3']
corpus = Corpus(map(Document, l))

Comments

1

Not fully sure if this is what you want but I'm assuming you need a list comprehension.

paragraphs = ['paragraph 1','paragraph 2','paragraph 3']
corpus = Corpus(documents=[Document(d) for d in paragraphs])

Comments

1

The question doesn't specify whether it should use list comprehension or not. In the particular example given in the question, list comprehension is a fine and concise solution. However, in case the op is not asking for list comprehension for about a more general solution to working with lists in Python, here is the more verbose iterative method:

paragraphs = ['paragraph 1','paragraph 2','paragraph 3']
docs = []
for p in paragraphs:
    docs.append(Document(p))
corpus = Corpus(documents=docs)

2 Comments

I don't see any reason why the OP would ever need to do this.
In this example, he shouldn't, as noted in the answer. I am merely putting this out there in case iteration really was what he was looking for, while giving an example in which list comprehension was a better choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.