2017-07-16 2 views
1

にリスト内にすでに存在しているときはFalse取得私は、文字列かの条件のためそれは文字列のpython

item1="Act the way that the people around you are acting." 

とリスト

list1=['Act the way that the people around you are acting.', "This phrase might come in handy when you're traveling abroad notice that people do things differently than you're used to."] 

を持って、

if item1 in list1: 
     print(item1) 

常に失敗しますが、文字列はリストに存在します。これはリスト1にITEM1がTrueになっていません参照してください

Actual code: 

    df1, 
    Proverb    | Meaning 

    When in Rome, do as the Romans | Act the way that the people around you are acting. This phrase might come in handy when you're traveling abroad notice that people do things differently than you're used to. 



    df2, 
    Meaning 
    Act the way that the people around you are acting. | 


df1_list=df1['Meaning'].values.tolist() 
df2_list=df2['Meaning'].values.tolist() 
df3=pd.DataFrame(columns=["Proverb","Meaning"]) 
for item in df2_list: 
     new_items = nltk.sent_tokenize(item) 
     for item1 in new_items: 
      for item2 in df1_list: 
       list1=nltk.sent_tokenize(item2) 
       index=None 
       if item1 in list1: 
        index=df1_list.index(item2) 
        data=df1.iloc[[index]] 
        df3=df3.append(data) 
print(df3) 

output 

df3, 
Empty DataFrame 
Columns: [Proverb, Meaning] 
Index: [] 

を助け、

+0

最後のループの前に 'list1'を、そしてループの中に' item1'を印刷して、一致しない理由を比較してください。 – PRMoureu

答えて

0

を助けてくださいしてください私は

item1="Act the way that the people around you are acting." 
list1=['Act the way that the people around you are acting.', 
    "This phrase might come in handy when you're traveling abroad " + 
    "notice that people do things differently than you're used to."] 
if item1 in list1: 
    print(item1) 

、次のコード(長い行を参照するように編集)を試してみました、それが

を印刷します

あなたの周りの人々が行動しているように行動します。

したがって、問題は他の場所にあります。

コードの最初の部分df1,は有効なPythonコード(問題が発生している可能性があります)ではありません。投稿を編集して、最小限で完全で検証可能な例(https://stackoverflow.com/help/mcve)にしてください。

+0

私のコード全体を実行しましたか? – krish

0

item1は確かにlist1にありません。それ以外の場合はifになります。私はitem1list1の値を印刷して何が間違っているかを見ていきます。 2つのデータフレームの正確な値を私に教えてもらえれば助けてくれます。コードは非常にひどく書かれています

+1

こんにちは、ありがとう、それは働いた。問題は私の入力です。ご協力いただきありがとうございます – krish

関連する問題