拆分字符串时如何保持顺序?
How do I maintain the order when splitting a string?
我正在尝试从拆分字符串创建有序字典。如何维护拆分字符串的顺序?抱歉,我原来的例子令人困惑并且与有序字典的想法相矛盾。这是一个不同的问题,但我不确定如何拆分字符串。
我的示例文件"practice_split.txt"如下:
§1 text for chapter 1 §2 text for chapter 2 §3 text for chapter 3
我希望我订购的字典看起来像:
OrderedDict([('§1', 'text for chapter 1'), ('§2', 'text for chapter 2'), ('§3', 'text for chapter 3')])
而不是:
OrderedDict([('1 text for chapter 1 ', '\xc2\xa7'), ('\xc2\xa7', '3 text for chapter 3'), ('2 text for chapter 2 ', '\xc2\xa7')])
这是我的代码:
# -*- coding: utf-8 -*
import codecs
import collections
import re
with codecs.open('practice_split.txt', mode='r', encoding='utf-8') as document:
o_dict = collections.OrderedDict()
for line in document:
conv = line.encode('utf-8')
a = re.split('(§)', conv)
a = a[1:len(a)]
for i in range(1, len(a) - 1):
o_dict[a[i]] = a[i+1]
print o_dict
谢谢!
问题不在于 OrderedDict,而在于您的 for
循环。您的循环执行以下操作:
将“第 1 章的文本”指向“\xc2\xa7”# 在字典中按原样
将“\xc2\xa7”指向“第 2 章的文本”# 这将被覆盖
将“第 2 章的文本”指向“\xc2\xa7”# 在字典中按原样
将“\xc2\xa7”指向“第 3 章的文本”# 这将覆盖第二个条目,因此它最终排在第二位
不确定您真正希望字典包含什么,或者我会建议一些代码....
根据我对您代码的理解,您的循环不正确。您想要第一个带有第一个文本条目的 §
。您还想跳过 §
元素作为字典的键,因此循环需要 2 步。最后,您可能希望去掉文本 start/end 中的空格。
for i in range(1, len(a), 2):
o_dict["{}{}".format(a[i - 1], i / 2 + 1)] = a[i].strip()
print o_dict
for k, v in o_dict.iteritems():
print k.decode('utf-8'), v
输出:
OrderedDict([('\xc2\xa71', 'text for chapter 1'), ('\xc2\xa72', 'text for chapter 2'), ('\xc2\xa73', 'text for chapter 3')])
§1 text for chapter 1
§2 text for chapter 2
§3 text for chapter 3
编辑:我更改了我的代码以反映对 OP 问题的编辑。
我正在尝试从拆分字符串创建有序字典。如何维护拆分字符串的顺序?抱歉,我原来的例子令人困惑并且与有序字典的想法相矛盾。这是一个不同的问题,但我不确定如何拆分字符串。
我的示例文件"practice_split.txt"如下:
§1 text for chapter 1 §2 text for chapter 2 §3 text for chapter 3
我希望我订购的字典看起来像:
OrderedDict([('§1', 'text for chapter 1'), ('§2', 'text for chapter 2'), ('§3', 'text for chapter 3')])
而不是:
OrderedDict([('1 text for chapter 1 ', '\xc2\xa7'), ('\xc2\xa7', '3 text for chapter 3'), ('2 text for chapter 2 ', '\xc2\xa7')])
这是我的代码:
# -*- coding: utf-8 -*
import codecs
import collections
import re
with codecs.open('practice_split.txt', mode='r', encoding='utf-8') as document:
o_dict = collections.OrderedDict()
for line in document:
conv = line.encode('utf-8')
a = re.split('(§)', conv)
a = a[1:len(a)]
for i in range(1, len(a) - 1):
o_dict[a[i]] = a[i+1]
print o_dict
谢谢!
问题不在于 OrderedDict,而在于您的 for
循环。您的循环执行以下操作:
将“第 1 章的文本”指向“\xc2\xa7”# 在字典中按原样
将“\xc2\xa7”指向“第 2 章的文本”# 这将被覆盖
将“第 2 章的文本”指向“\xc2\xa7”# 在字典中按原样
将“\xc2\xa7”指向“第 3 章的文本”# 这将覆盖第二个条目,因此它最终排在第二位
不确定您真正希望字典包含什么,或者我会建议一些代码....
根据我对您代码的理解,您的循环不正确。您想要第一个带有第一个文本条目的 §
。您还想跳过 §
元素作为字典的键,因此循环需要 2 步。最后,您可能希望去掉文本 start/end 中的空格。
for i in range(1, len(a), 2):
o_dict["{}{}".format(a[i - 1], i / 2 + 1)] = a[i].strip()
print o_dict
for k, v in o_dict.iteritems():
print k.decode('utf-8'), v
输出:
OrderedDict([('\xc2\xa71', 'text for chapter 1'), ('\xc2\xa72', 'text for chapter 2'), ('\xc2\xa73', 'text for chapter 3')])
§1 text for chapter 1
§2 text for chapter 2
§3 text for chapter 3
编辑:我更改了我的代码以反映对 OP 问题的编辑。