我想拆分我的文本并存储它以生成有序的字典
For example:
1.This is my text.
2.This is 2nd Text.
我想拆分数字和文本并将其存储在有序的字典中,例如
Ordered Dict
"1":"This is my text"
"2":"This is 2nd text"
我试过. 分裂,但它对我不起作用。怎么做?
d = OrderedDict()
text_data = [ "1.This is my text.","2.This is 2nd text"]
for i, f in enumerate(text_data):
id = f.split('.')
d[id] = text_data[i]
print(i, " :: ", id, " =>n", d[id], "n" + "*" * 100 + "n")
我哪里出错了?制作有序字典
你离得很近。按点拆分字符串后,使用索引访问元素。
前任:
from collections import OrderedDict
d = OrderedDict()
text_data = [ "1.This is my text.","2.This is 2nd text"]
for i, f in enumerate(text_data):
val = f.split('.') #str.split
d[val[0]] = val[1] #Use Index.
for k, v in d.items():
print(k, v)
我会推荐以下内容:
from collections import OrderedDict
d = OrderedDict()
text_data = [ "1.This is my text.", "2.This is 2nd text"]
for sentence in text_data: # Note 1
num, text = sentence.rstrip('.').split('.', 1) # Notes 2 & 3
d[num] = text
笔记:
- 您确实需要使用
enumerate
中的i
,因此请将其删除。 rstrip
在你split
之前.如您所见,每个句子的末尾都有一个点('.'
(,可能会干扰split
。但是,如果您想保留最后一个点(如果存在(,只需删除.rstrip('.')
部分即可。- 将第二个参数传递给
split
告诉它应该进行多少次削减。'3. A sentence. With a dot in between.'
想想这个案例。
以上产生:
for k, v in d.items():
print('{!r}: {!r}'.format(k, v))
# '1': 'This is my text'
# '2': 'This is 2nd text'
或者也许:
from collections import OrderedDict
news='.'.join(s.split('. ')).split('.')
d=OrderedDict(list(dict(news[i:i+2] for i in range(len(news)-2)).items())[::2])
for k,v in d.items():
print('"%s": "%s"'%(k,v))
输出:
"1": "This is my text"
"2": "This is 2nd Text"