There are 9 texts called text1, text2, ... text9. A function is defined as follow.
def lexical_diversity(text):
return len(set(text))/len(text)
I want to call the function for all 9 texts with following code. But the outpu is wrong.
for i in range(1,10):
a='text'+str(i)
print(lexical_diversity(a))
My output is
0.8
0.8
...
0.8
If applying the function to text1, I get following result.
>>>lexical_diversity(text1)
Out[37]:0.07406285585022564
So which part goes wrong?
'text'+str(i)gives"text1","text2", ... the actual strings. So you get the lexical diversity of"text1","text2", ... If you have multiple texts you want to access, you probably want to store them in an array, not in separated variables otherwise you cannot loop on them.python list tutorialon your search engine of choice, you'll get a better understanding of how to handle lists of things :)