user3112327
user3112327

Reputation: 305

Can't convert 'list'object to str implicitly Python

I am trying to import the alphabet but split it so that each character is in one array but not one string. splitting it works but when I try to use it to find how many characters are in an inputted word I get the error 'TypeError: Can't convert 'list' object to str implicitly'. Does anyone know how I would go around solving this? Any help appreciated. The code is below.

import string
alphabet = string.ascii_letters
print (alphabet)
splitalphabet = list(alphabet)
print (splitalphabet)

x = 1
j = year3wordlist[x].find(splitalphabet)
k = year3studentwordlist[x].find(splitalphabet)
print (j)

EDIT: Sorry, my explanation is kinda bad, I was in a rush. What I am wanting to do is count each individual letter of a word because I am coding a spelling bee program. For example, if the correct word is 'because', and the user who is taking part in the spelling bee has entered 'becuase', I want the program to count the characters and location of the characters of the correct word AND the user's inputted word and compare them to give the student a mark - possibly by using some kind of point system. The problem I have is that I can't simply say if it is right or wrong, I have to award 1 mark if the word is close to being right, which is what I am trying to do. What I have tried to do in the code above is split the alphabet and then use this to try and find which characters have been used in the inputted word (the one in year3studentwordlist) versus the correct word (year3wordlist).

Upvotes: 3

Views: 5597

Answers (7)

logc
logc

Reputation: 3923

There is a much simpler solution if you use the in keyword. You don't even need to split the alphabet in order to check if a given character is in it:

year3wordlist = ['asdf123', 'dsfgsdfg435']
total_sum = 0
for word in year3wordlist:
    word_sum = 0
    for char in word:
        if char in string.ascii_letters:
            word_sum += 1
    total_sum += word_sum

# Length of characters in the ascii letters alphabet:
# total_sum == 12
# Length of all characters in all words:
# sum([len(w) for w in year3wordlist]) == 18

EDIT:

Since the OP comments he is trying to create a spelling bee contest, let me try to answer more specifically. The distance between a correctly spelled word and a similar string can be measured in many different ways. One of the most common ways is called 'edit distance' or 'Levenshtein distance'. This represents the number of insertions, deletions or substitutions that would be needed to rewrite the input string into the 'correct' one.

You can find that distance implemented in the Python-Levenshtein package. You can install it via pip:

$ sudo pip install python-Levenshtein

And then use it like this:

from __future__ import division
import Levenshtein

correct = 'because'
student = 'becuase'
distance = Levenshtein.distance(correct, student)  # distance == 2

mark = ( 1 - distance / len(correct)) * 10  # mark == 7.14

The last line is just a suggestion on how you could derive a grade from the distance between the student's input and the correct answer.

Upvotes: 3

sabbahillel
sabbahillel

Reputation: 4425

While join creates the string from the split, you would not have to do that as you can issue the find on the original string (alphabet). However, I do not think is what you are trying to do. Note that the find that you are trying attempts to find the splitalphabet (actually alphabet) within year3wordlist[x] which will always fail (-1 result)

If what you are trying to do is to get the indices of all the letters of the word list within the alphabet, then you would need to handle it as

for each letter in the word of the word list, determine the index within alphabet.

j = []
for c in word:
  j.append(alphabet.find(c))

print j

On the other hand if you are attempting to find the index of each character within the alphabet within the word, then you need to loop over splitalphabet to get an individual character to find within the word. That is

l = []
for c within splitalphabet:
  j = word.find(c)
  if j != -1:
    l.append((c, j))
print l

This gives the list of tuples showing those characters found and the index.

I just saw that you talk about counting the number of letters. I am not sure what you mean by this as len(word) gives the number of characters in each word while len(set(word)) gives the number of unique characters. On the other hand, are you saying that your word might have non-ascii characters in it and you want to count the number of ascii characters in that word? I think that you need to be more specific in what you want to determine.

If what you are doing is attempting to determine if the characters are all alphabetic, then all you need to do is use the isalpha() method on the word. You can either say word.isalpha() and get True or False or check each character of word to be isalpha()

Upvotes: 0

Hugh Bothwell
Hugh Bothwell

Reputation: 56654

import string

# making letters a set makes "ch in letters" very fast
letters = set(string.ascii_letters)

def letters_in_word(word):
    return sum(ch in letters for ch in word)

Edit: it sounds like you should look at Levenshtein edit distance:

from Levenshtein import distance

distance("because", "becuase")   # => 2

Upvotes: 0

pat
pat

Reputation: 12749

I don't know why half the answers are telling you how to put the split alphabet back together...

To count the number of characters in a word that appear in the splitalphabet, do it the functional way:

count = len([c for c in word if c in splitalphabet])

Upvotes: 0

Aaron Hall
Aaron Hall

Reputation: 395115

join is a class method of str, you can do

''.join(splitalphabet)

or

str.join('', splitalphabet)

Upvotes: 1

Christian Tapia
Christian Tapia

Reputation: 34146

To convert the list splitalphabet to a string, so you can use it with the find() function you can use separator.join(iterable):

"".join(splitalphabet)

Using it in your code:

j = year3wordlist[x].find("".join(splitalphabet))

Upvotes: 0

hivert
hivert

Reputation: 10667

I think what you need is join:

>>> "".join(splitalphabet)
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Upvotes: 1

Related Questions