Python在文件中查找连字符的范围

时间:2019-04-25 13:46:04

标签: python

我的文本文件格式如下

1-8
10-12
14-45
48-50

如何获取每行的范围?

1, 2, 3, 4, 5, 6, 7, 8
10, 11, 12

我尝试过分割文件以将每个数字都放入一个列表中。 1、8、10、12、14、45、48、50,但是我不确定如何将其转换为范围的开始和结束编号。

data = []
with open('file.txt','r') as myfile:
    for line in myfile:
        data.extend(map(int, line.split('-')))
print (data)

6 个答案:

答案 0 :(得分:2)

您可以使用简短列表理解从给定的行中提取数字。由于您列出的范围似乎两端都包含在内,因此我们需要在下面的结束间隔(j)中添加一个。

for line in myfile:
    i, j = [int(n) for n in line.split("-")]
    for x in range(i, j + 1):
        # do things

如果您只是想将两个数字固定在一个范围内,则可以选择以下方法:

range(*[int(n) for n in line.split("-")])

如果要列出该范围内的数字,则需要将表达式包装在list()中。

答案 1 :(得分:1)

您只需要从每行中提取开始和结束索引,然后在range中使用它来创建列表

data = []

with open('file.txt','r') as myfile:
    for line in myfile:
        start,end = [int(item) for item in line.split('-')]
        li = list(range(start ,end+1))
        data.append(li)
print(data)

因此,如果输入为:

1-8
10-12
14-45
48-50

输出将是:

[
[1, 2, 3, 4, 5, 6, 7, 8], 
[10, 11, 12], 
[14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45], 
[48, 49, 50]
]

答案 2 :(得分:1)

您非常接近,只需添加列表range()

data = []
with open("file.txt") as f:
    for line in f:
        start, end = map(int, line.split("-"))
        data.append(list(range(start, end + 1)))

print(data)
# [[1, 2, 3, 4, 5, 6, 7, 8], [10, 11, 12], [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45], [48, 49, 50]]

还要确保end递增1,因为它不包含在range()中。

答案 3 :(得分:1)

如果只想打印范围,则可以使用以下方法:

with open('file.txt', 'r') as myfile:
    for line in myfile:
        nums = [int(i) for i in line.split('-')]
        nums[1] += 1  # more on this in a second
        my_range = range(*nums)  # the * unpacks the two numbers into two arguments
        print([i for i in my_range])
        # [1, 2, 3, 4, 5, 6, 7, 8]

遍历它,for循环的第一行您已经非常了解了。这只是获取两个数字并将它们解释为整数。在此行之后,将nums = [1, 8]用作您的第一行'1-8'。接下来,我们将一个添加到列表的最后一个元素,所以现在nums = [1, 9]。我们这样做是因为内置range(a, b)会生成从ab-1的数字。

接下来,我们将创建一个范围,该范围将生成您想要的range(*nums)数字。该语句中的*将列表解压缩为range()所期望的两个参数。

最后,我们打印范围内的所有项目。由于my_range现在是一个生成器,我们需要将其拆包以进行打印,因此我们使用列表推导对其进行迭代并获取所有数字。

答案 4 :(得分:0)

我相信您可以使用正则表达式使用capturing groups匹配模式并将其添加到列表中。

正则表达式:https://regex101.com/r/f1Ghff/1

(\d+)\-(\d+) 

说明:

1st Capturing Group (\d+)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\- matches the character - literally (case sensitive)
2nd Capturing Group (\d+)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

代码:

import re

str = """
1-8 
10-12 
14-45 
48-50
"""

pattern = r"(\d+)\-(\d+)"
matches = re.findall(pattern, str)

ranges = []

# Convert Group1 and Group2 into integers
for tuple in matches:
    low = int(tuple[0])
    high = int(tuple[1])
    ranges.append(list(range(low, high)))

print (ranges)

输出:

[[1, 2, 3, 4, 5, 6, 7], [10, 11], [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44], [48, 49]]

答案 5 :(得分:0)

这样的事情怎么样?

当然,它没有错误检查,因此您要注意输入数据的质量。

data = []
with open('file.txt','r') as myfile:
    for line in myfile:
        # Split the line and force conversion to int
        start_int, end_int = map(int, line.split('-'))
        # Get a Python 3 range - note this is not a list but a "range" type in Python 3, so we'll have to convert it before appending to our global list
        int_range = range(start_int, end_int+1)
        data.append(list(int_range))