2017-07-19 87 views
-1
def Interpretation_is(sentence): 
    print(sentence.split()) 
    print(sentence.split()[0]) 

    there = 'distant place' 
    he = 'gender identity' 

    if 'place' in setence.split()[0]: 
     print(sentence.replace('is','exists')) 
    if 'identity' in sentence.split()[0]: 
     print(sentence.replace('is','equal to')) 

Interpretation_is('there is a woman') 
Interpretation_is('he is a boy') 

上面的代碼改寫特別是關於含義給出的句子「是」省略了字符串標識'在Python

由於只照顧其先前字,主題,我只冒了一句。 split()[0]並再次評估其含義

問題是,如果我拿sentence.split()[0],我最終得到「有」作爲一個字符串,但我想再次讓蟒蛇把它讀成一個變量其中包括另一個元組或一組字符串,例如there ='distant place or location',並檢查其中是否存在字符串'place or location'。

其他問題1)先進的方式,我想彌補其落入組所謂地方的一些詞組,如地方= {那裏,這裏}然後讓程序檢查是否句子.split()[0]實際上屬於這個組。

我該怎麼做?

附加問題2)當我引用一個詞的含義時,例如is = {exist,parity}。如果我遵循數學符號注意到它,那麼就沒有給出「順序」,但是如果我將它作爲一個列表或元組,如is =(存在,奇偶性)或is = [存在,奇偶性),那麼不可避免地會出現'order' 。

python中哪個數據結構不包含'order'?

我可以檢查的任何建議或參考?

+0

您可能想使用Python的[詞典]結構(https://docs.python.org/2/tutorial/datastructures.html#dictionaries)結構,雖然我不完全清楚你的東西所需的輸出它。 – asongtoruin

+0

我正在考慮對給定的句子進行修改。 @ason​​gtoruin希望你看到我的答案 – Daschin

回答

0
def Interpretation_is(sentence): 
    print(sentence.split()) 
    print(sentence.split()[0]) 

    place = ('there', 'here') 
    gender_identity = ('he', 'she') 

    if sentence.split()[0] in place: 
      print(sentence.replace('is','exists')) 
    if sentence.split()[0] in gender_identity: 
      print(sentence.replace('is','equal to')) 

Interpretation_is('there is a woman') 
Interpretation_is('he is a boy') 
0

由於將字符串(句子)分割爲字符串列表,因此將'there'作爲字符串得到。

你所尋找的是一個字符串轉換爲一個變量,你可以找到答案這裏To convert string to variable name

Q2:在這種情況下,你要使用的第一個字符串爲重點,以創建一個字典,並創建句子其餘部分的一個元組(字符串)並將其用作值。

string = 'there is a boy' 
l = string.split() 
d = {l[0]: tuple(l[1:])}