I wanna make a table about positions of characters in a text. Example:
Text = "an apple and a banana"
Char:Positions(from 0 to 20) and how many times used in this positions
Characters--> A:4101010...0-B:100000...0-C:000000...0-D:001000...0-E:000010...0-...-Z:000000...0
What is wrong?
position_list = []
i = 0
for char in range(29):
position_list.append([])
for position in range(20):
position_list[i].append(0)
i += 1
alphabet = ["a", "b", "c", "ç", "d", "e", "f", "g", "ğ", "h", "ı", "i", "j", "k", "l", "m", "n", "o", "ö", "p", "r", "s", "ş", "t", "u", "ü", "v", "y", "z"]
alphabet_index = 0
text = ["sample", "text"]
for word in text:
x = 0
for char in alphabet:
start = 0
while len(word) > start:
char_pos = word.find(char, start)
start += 1
if char_pos == -1:
break
else:
position_list[x][char_pos] += 1
x += 1
print(position_list)
This solves your problem:
alphabet = ["a", "b", "c", "ç", "d", "e", "f", "g", "ğ", "h", "ı", "i", "j", "k", "l",
"m", "n", "o", "ö", "p", "r", "s", "ş", "t", "u", "ü", "v", "y", "z"]
position_list = [[0]*20 for i in range(len(alphabet))]
text = "an apple and a banana"
for word in text.split():
for i, c in enumerate(word):
position_list[alphabet.index(c)][i] += 1
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments