省略字符串标识符'';用python
上述代码针对“is”的含义对给定句子进行了重新表述 因为is只处理它前面的单词主语,所以我只处理句子.split()[0]并再次评估它的含义 问题是,如果我接受句子.split()[0],我最终会将'there'作为字符串,但我想再次让python将其作为一个变量来读取,该变量由另一个元组或一组字符串组成,例如there='distance place或location',并检查其中是否存在字符串'place或location' 附加问题1)以一种高级的方式,我想组成一些属于所谓的place组的单词组,例如place={there,here},然后让程序检查句子.split()[0]是否真的属于这个组 我怎么能这样做 附加问题2)当我引用一个有其含义的单词时,例如is={exist,parity}。如果我按照数学符号记下它,就没有给出“顺序”,但是如果我把它作为一个列表或元组,比如is=(存在,奇偶校验)或is=[exist,奇偶校验],就不可避免地给出了“顺序” python中的哪个数据结构不具有“顺序”省略字符串标识符'';用python,python,string,list,data-structures,Python,String,List,Data Structures,上述代码针对“is”的含义对给定句子进行了重新表述 因为is只处理它前面的单词主语,所以我只处理句子.split()[0]并再次评估它的含义 问题是,如果我接受句子.split()[0],我最终会将'there'作为字符串,但我想再次让python将其作为一个变量来读取,该变量由另一个元组或一组字符串组成,例如there='distance place或location',并检查其中是否存在字符串'place或location' 附加问题1)以一种高级的方式,我想组成一些属于所谓的place组的
我可以检查的任何建议或参考?您将“there”作为字符串,因为您将字符串(句子)拆分为一个字符串列表
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')
您要寻找的是将字符串转换为变量,您可以在这里找到答案
问题2:在这种情况下,您希望使用第一个字符串作为键创建dict,并创建句子(字符串)其余部分的元组并将其用作值
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')
您可能想研究使用Python的结构,尽管我不完全清楚您希望输出的内容。我正在考虑对给定的句子进行改写@我希望你能看到我的答案
string = 'there is a boy'
l = string.split()
d = {l[0]: tuple(l[1:])}