如何按方括号之间的内容进行数字排序
How to sort numerically by what's between square brackets
我有以下情况,其中文本文件具有类似以下输出的内容:
DecodingIndex[ 1] PresentationIndex[ 2]
DecodingIndex[ 2] PresentationIndex[ 3]
DecodingIndex[ 3] PresentationIndex[ 1]
etc...
因为它显示了 DecodingIndex 中的数字顺序,我希望它按 PresentationIndex 排序。像下面这样:
DecodingIndex[ 3] PresentationIndex[ 1]
DecodingIndex[ 1] PresentationIndex[ 2]
DecodingIndex[ 2] PresentationIndex[ 3]
在 Python 中有没有简单的方法来做到这一点?这些数字一直到数万。对于小于 10 的数字,方括号之间的距离总是有一个间隙,然后拥抱数字,例如 DecodingIndex[32100]
希望说得通,感谢您的帮助!
=======
这是我尝试过的:
1)我遍历文件中的每一行并存储到 lines[] 列表中
2) 遍历 lines[] 列表中的每个项目,同时使用以下正则表达式模式 re.compile(r'PresentationIndex\[(.*?)\]')
3)然后我使用 group() 从结果中获取匹配项并将这些值存储在一个新列表中
4)然后我通过首先将项目转换为 int,然后排序,然后将其转换回这样的字符串来对列表进行数字排序
5) 现在我遍历该列表并插入单词 PresentationIndex 和方括号
6) 使用现在排序的 PresentationIndex 列表,我遍历每一个。对于每次迭代,我遍历整个输出文本文件以搜索关联的行并将其附加到最终列表。这样我就可以按照我想要的顺序得到我的输出。
我从一个大约 32,000 行的文件开始。做这个大约花了 3 个小时...
这可能不是最理想的,但应该可以解决问题:
import re
from collections import OrderedDict
my_string = '''DecodingIndex[ 1] PresentationIndex[ 2]
DecodingIndex[ 2] PresentationIndex[ 3]
DecodingIndex[ 3] PresentationIndex[ 1]'''
my_list = list(my_string.split("\n"))
my_dict = {}
for x in my_list:
match = re.search("\[\s*(\d+)\s*\]$", x)
my_dict[match.group(1)] = x
ordered_dict = OrderedDict(sorted(my_dict.items(), key=lambda t: t[0]))
print(ordered_dict)
读入文件对您来说可能比较慢?这应该都 运行 非常快。我从一个字符串开始假设你可以把文件变成一个字符串。我在 \n
上拆分了字符串,但您也可以只读取文件,因此每一行都是列表中的一个项目。
然后我循环它并使用正则表达式匹配您要排序的数字。使该值成为 dict
中的 key
。然后使用 collections
按键对字典进行排序。全部做完!希望对您有所帮助。
您可以在文件上调用 sorted()
(因为打开的文本文件在迭代时就像一个行列表)使用一个函数,该函数接受一行并将括号之间的内容提取为 key=
参数
import re
def extract_presentation_index(line):
return int(re.search("\[\s*(\d+)\s*\]$", line).group(1))
# alternatively, you don't have to use regex
#return int(line.split('[')[2].split(']', 1)[0].strip())
with open('/path/to/your/file') as f:
sorted_lines = sorted(f, key=extract_presentation_index)
print(''.join(sorted_lines), end='')
end=''
只是为了避免在末尾添加额外的换行符 (\n
)。
我有以下情况,其中文本文件具有类似以下输出的内容:
DecodingIndex[ 1] PresentationIndex[ 2]
DecodingIndex[ 2] PresentationIndex[ 3]
DecodingIndex[ 3] PresentationIndex[ 1]
etc...
因为它显示了 DecodingIndex 中的数字顺序,我希望它按 PresentationIndex 排序。像下面这样:
DecodingIndex[ 3] PresentationIndex[ 1]
DecodingIndex[ 1] PresentationIndex[ 2]
DecodingIndex[ 2] PresentationIndex[ 3]
在 Python 中有没有简单的方法来做到这一点?这些数字一直到数万。对于小于 10 的数字,方括号之间的距离总是有一个间隙,然后拥抱数字,例如 DecodingIndex[32100]
希望说得通,感谢您的帮助!
=======
这是我尝试过的:
1)我遍历文件中的每一行并存储到 lines[] 列表中
2) 遍历 lines[] 列表中的每个项目,同时使用以下正则表达式模式 re.compile(r'PresentationIndex\[(.*?)\]')
3)然后我使用 group() 从结果中获取匹配项并将这些值存储在一个新列表中
4)然后我通过首先将项目转换为 int,然后排序,然后将其转换回这样的字符串来对列表进行数字排序
5) 现在我遍历该列表并插入单词 PresentationIndex 和方括号
6) 使用现在排序的 PresentationIndex 列表,我遍历每一个。对于每次迭代,我遍历整个输出文本文件以搜索关联的行并将其附加到最终列表。这样我就可以按照我想要的顺序得到我的输出。
我从一个大约 32,000 行的文件开始。做这个大约花了 3 个小时...
这可能不是最理想的,但应该可以解决问题:
import re
from collections import OrderedDict
my_string = '''DecodingIndex[ 1] PresentationIndex[ 2]
DecodingIndex[ 2] PresentationIndex[ 3]
DecodingIndex[ 3] PresentationIndex[ 1]'''
my_list = list(my_string.split("\n"))
my_dict = {}
for x in my_list:
match = re.search("\[\s*(\d+)\s*\]$", x)
my_dict[match.group(1)] = x
ordered_dict = OrderedDict(sorted(my_dict.items(), key=lambda t: t[0]))
print(ordered_dict)
读入文件对您来说可能比较慢?这应该都 运行 非常快。我从一个字符串开始假设你可以把文件变成一个字符串。我在 \n
上拆分了字符串,但您也可以只读取文件,因此每一行都是列表中的一个项目。
然后我循环它并使用正则表达式匹配您要排序的数字。使该值成为 dict
中的 key
。然后使用 collections
按键对字典进行排序。全部做完!希望对您有所帮助。
您可以在文件上调用 sorted()
(因为打开的文本文件在迭代时就像一个行列表)使用一个函数,该函数接受一行并将括号之间的内容提取为 key=
参数
import re
def extract_presentation_index(line):
return int(re.search("\[\s*(\d+)\s*\]$", line).group(1))
# alternatively, you don't have to use regex
#return int(line.split('[')[2].split(']', 1)[0].strip())
with open('/path/to/your/file') as f:
sorted_lines = sorted(f, key=extract_presentation_index)
print(''.join(sorted_lines), end='')
end=''
只是为了避免在末尾添加额外的换行符 (\n
)。