【问题标题】:How to turn a text file with number ranges into a list variable?如何将具有数字范围的文本文件转换为列表变量?
【发布时间】:2022-11-10 22:04:47
【问题描述】:

我有一个文本文件:

8-9, 12, 14-16, 19, 27-28, 33, 41, 43, 45-46, 48-49, 51,54-60, 62-74, 76-82, 84-100, 102- 105、107-108

它基本上是文本文件中的整数列表。使用 Python,我想把它变成一个列表,其中每个变量都单独存储。但问题是数字之间的破折号代表一个范围,暗示 62-74 实际上是 62,63,64,65,66,67,68,69,70,71,72,73,74。

所以我的程序应该能够读取文本,如果遇到任何破折号,它应该在列表中附加该范围内的数字。

你知道怎么做吗?

我试图在文本文件中创建一个包含整数的列表。

【问题讨论】:

标签: python


【解决方案1】:

尝试这个:

num_list = ["8-9", "12", "14-16", "19", "27-28", "33", "41", "43", "45-46", "48-49", "51","54-60", "62-74", "76-82", "84-100", "102-105", "107-108"]
output_list = []

for number in num_list:
    if "-" in number: # Checks if the string contains "-"
        num1, num2 = number.split(sep="-") # Splits the numbers
        num1 = int(num1) # Setting the number from string to integer
        num2 = int(num2) # Setting the number from string to integer
        while num1 <= num2:
            output_list.append(num1) # append to output list
            num1 += 1
    else:
        output_list.append(int(number)) # append to output list

print(output_list)

输出:

[8, 9, 12, 14, 15, 16, 19, 27, 28, 33, 41, 43, 45, 46, 48, 49, 51, 54, 55, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 102, 103, 104, 105, 107, 108]

【讨论】:

    【解决方案2】:

    您只想检查字符串中是否有"-"。如果存在,请使用内置的range 函数生成列表。这可能对你有用。

    vals = [
        "8-9", "12", "14-16", 
        "19", "27-28", "33", 
        "41", "43", "45-46", 
        "48-49", "51", "54-60", 
        "62-74", "76-82", "84-100", 
        "102-105", "107-108"
    ]
    
    output = []
    for val in vals:
        if "-" not in val:
            output.append(int(val))
        else:
            bounds = val.split("-")
            output += [i for i in range(int(bounds[0]), int(bounds[1])+1)]
    
    print(output)
    # >>> [8, 9, 12, 14, 15, 16, 19, 27, 28, 33, 41, 43, 45, 46, 48, 49, 51, 54, 55, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 102, 103, 104, 105, 107, 108]
    

    【讨论】:

      【解决方案3】:
      with open("list.txt","f") as f:
         content = f.read()
         result = [x for rng in content.split(",") for x in range(*map(int, rng.split("-")))]
      

      将是一个很酷的解决方案,但您的范围是包容性的,python 的范围不是:/

      所以:

      def inclusive_range(start, stop=None):
          if stop:
             return range(start, stop+1)
          else:
             return range(start)
      
      
      with open("list.txt","f") as f:
         content = f.read()
         result = [x for rng in content.split(",") for x in inclusive_range(*map(int, rng.split("-")))]
      

      【讨论】:

        猜你喜欢
        • 2017-10-02
        • 2022-01-01
        • 2022-11-16
        • 1970-01-01
        • 2017-10-26
        • 1970-01-01
        • 1970-01-01
        • 2012-04-08
        • 1970-01-01
        相关资源
        最近更新 更多