Im trying to compare 'words' with 'if_contains', when they match they should be added into save list. Expected output - ['one', 'two eight nine']. The output im getting - ['one']
if_contains = ['one', 'two', 'three']
save = []
words = ['one', 'five', 'six', 'two eight nine']
for word in words:
if word in if_contains:
save.append(words)
print(save)
The first thing is that you save the whole words list instead of single elements. You probably wanted to do
save.append(word)
instead of
save.append(words)
Secondly, if you want to save two eight nine
as matching with the if_contains
list, then instead of performing if word in if_contains
, you should ask whether for any element of if_contains
, this element is in word
(which isn't the best choice for the variable name as it represents a few words occasionally). Final solution:
if_contains = ['one', 'two', 'three']
save = []
words = ['one', 'five', 'six', 'two eight nine']
for word in words:
for el in if_contains:
if el in word:
save.append(word)
print(save)
You're appending the whole list to the save
list variable. Use save.append(word)
instead of save.append(words)
if_contains = ['one', 'two', 'three']
save = []
words = ['one', 'five', 'six', 'two eight nine']
for word in words:
if word in if_contains:
save.append(word)
print(save)
Please take a look. There was mistake with words and you need to use nested loop with that.
if_contains = ['one', 'two', 'three']
save = []
words = ['one', 'five', 'six', 'two eight nine']
for if_c in if_contains:
for word in words:
if if_c in word:
save.append(word)
print(save)
Use list comprehension
if_contains = ['one', 'two', 'three']
save = []
words = ['one', 'five', 'six', 'two eight nine']
[word for word in words if word in if_contains]
['one']
Ehy, but in case I want to get just the whole-word and not a substring?
if_contains = ['napoli', 'salvini', 'one']
save = []
words = ['napoli', 'pierosalvini', 'stellone', 'pietrone']
for word in words:
for el in if_contains:
if el in word:
save.append(word)
print(save)
In this case I've all the wods with the substrings in them. But I'd like to have just napoli, because it's 100% matched. All the characters have to be the same.
Thanks
Trying to solve:
p_calcio01=open('parole_calcio.txt')
p_calcio = [line.rstrip('\n') for line in p_calcio01]
for row in frame:
for word in p_calcio:
for word1 in frame["Cleaned Text into list"]: #words list in a dataframe
if word1 in word:
frame["Cluster"] = "Calcio"
frame
The parole_calcio.txt is a simple word(s) list every row. Dataframe is: enter image description here
frame["Cluster"]
is a new Column
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.