我有以下问题;我有一个名为split_content
的元组:
(['@Book{Clark2003,',
'author , {Eve Clark},',
'title , {First Language Acquisition},',
'publisher , {Cambridge University Press},',
'address , {Cambridge, UK},',
'year , 2003}',
'volume , 10}}'],
['',
'@techreport{arrow1948,',
'author , {Arrow, Kenneth J.},',
'title , {The possibility of a universal social welfare function},',
'institution , {RAND Corporation},',
'year , {1948},',
'number , {P-41},',
'type , {Report}'])
的p,如您所见,已经被分成两个子清单(在[",'@techreport
之前)。
我需要能够使用字符串(intext2
)并在这两个列表中的任何一个中找到匹配项。例如。 if intext2[0] = 'Clark'
。我希望它仅返回第一个子列表,即以'volume, 10}}']
结尾的子列表。
现在,为了达到这一目的,我已经编写了以下代码,但是它并非一直有效:
def split_file():
split_content = split_up()
intext= u_input[u_input.find("(")+1:u_input.find(")")]
intext2 = re.split('(\d+)',intext)
global split_content_fin
if [intext2[0] in split_content[0]]:
split_content_fin = split_content[0]
if [intext2[0] in split_content[1]]:
split_content_fin = split_content[1]
return split_content_fin
尽管与intext2[0]
根本不匹配,但通常只返回第二个子字符串。我已经尝试过列表推导,而不是[intext2[0]
中的split_content[0/1]]
,但无济于事。不过,我仍然认为问题就在这里,但是,我找不到解决方案。还是与它成为元组有关?
答案 0 :(得分:0)
像这样吗?
split_content = (['@Book{Clark2003,',
'author , {Eve Clark},',
'title , {First Language Acquisition},',
'publisher , {Cambridge University Press},',
'address , {Cambridge, UK},',
'year , 2003}',
'volume , 10}}'],
['',
'@techreport{arrow1948,',
'author , {Arrow, Kenneth J.},',
'title , {The possibility of a universal social welfare function},',
'institution , {RAND Corporation},',
'year , {1948},',
'number , {P-41},',
'type , {Report}'])
asString_1 = ','.join(map(str,split_content[0]))
asString_2 = ','.join(map(str,split_content[1]))
aWord = 'Clark'
if(aWord in asString_1):
print("in the first element")
elif(aWord in asString_2):
print("in the second element")
#elif(...)
# ...