[英]Convert a string to a list of numbers in python 3
This tells us the way to prepare a list of numbers, including a range of continues numbers or discontinues numbers. 这告诉我们准备数字列表的方法,包括一系列连续数字或非连续数字。
For example, list(range(1,10)) + [20,30,40] + list(range(400,410))
will return [1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 30, 40, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409]
. 例如, list(range(1,10)) + [20,30,40] + list(range(400,410))
将返回[1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 30, 40, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409]
。
But can I make it simpler by only inputting a string like "1-10, 20, 30, 40, 400-410"
? 但是,仅输入"1-10, 20, 30, 40, 400-410"
这样的字符串,我可以使其更简单吗?
I can write a function using a for-loop to go through the numbers. 我可以使用for循环编写一个函数来遍历数字。 Is there a more efficient way to write the function without using a for-loop? 是否有更有效的方式编写函数而不使用for循环?
You can use a nested list comprehension, checking whether the current part contains a -
and using either a range
or creating a one-elemented list accordingly: 您可以使用嵌套列表推导,检查当前零件是否包含-
然后使用range
或相应地创建一个单元素列表:
>>> s = "1-10, 20, 30, 40, 400-410"
>>> [n for part in s.split(", ") for n in (range(*map(int, part.split("-"))) if "-" in part else [int(part)])]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 30, 40, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409]
Maybe split this up, for readability: 出于可读性考虑,也许可以将其拆分:
>>> to_list = lambda part: range(*map(int, part.split("-"))) if "-" in part else [int(part)]
>>> [n for part in s.split(", ") for n in to_list(part)]
Note: As in your first example, this will translate "1-10"
to [1, 2, ..., 9]
, without 10
. 注意:与第一个示例一样,这会将"1-10"
转换为[1, 2, ..., 9]
,而不包含10
。
As noted in comments, this will not work for negative numbers, though, trying to split -3
or -4--2
into pairs of numbers. 如评论中所述,这不适用于负数,但是尝试将-3
或-4--2
为成对的数字。 For this, you could use regular expressions... 为此,您可以使用正则表达式...
>>> def to_list(part):
... m =re.findall(r"(-?\d+)-(-?\d+)", part)
... return range(*map(int, m[0])) if m else [int(part)]
...
>>> s = "-10--5, -4, -2-3"
>>> [n for part in s.split(", ") for n in to_list(part)]
[-10, -9, -8, -7, -6, -4, -2, -1, 0, 1, 2]
... or just use a different delimiter for ranges, eg -10:-5
. ...或仅对范围使用不同的分隔符,例如-10:-5
。
I suggest that you instead write a function that allows to create more complex, piecewise ranges. 我建议您改写一个允许创建更复杂的分段范围的函数。
def piecewise_range(*args):
for arg in args:
yield from range(*arg)
piecewise_range((1, 10), (20, 50, 10), (400, 410))
The function piecewise_range
then outputs a generator, so it allows you to have more complex range patterns and to preserve range
laziness. 然后,函数piecewise_range
输出一个生成器,因此它使您可以使用更复杂的范围模式并保留range
延迟。
Depending on where your input strings come from, you might do with something like this, named whatever you would want to name it: 根据您输入字符串的来源,您可以使用如下所示的名称进行命名:
import re
def gen_nums(input_string):
out = []
for item in re.split(r',\s*', input_string):
if '-' in item:
start, end = item.split('-')
out.extend(range(int(start), int(end)))
else:
out.append(int(item))
return out
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.