[英]What's wrong with my use of generator expression?
I have the following code, in which I try to convert a string representation of ranges to a list of numbers. 我有以下代码,其中我尝试将范围的字符串表示形式转换为数字列表。 For example, if the input is
'0-0,3-5,7-10'
the expected output is [0,3,4,5,7,8,9,10]
. 例如,如果输入为
'0-0,3-5,7-10'
则预期输出为[0,3,4,5,7,8,9,10]
。 However, I got an error at: 但是,我在以下地方遇到错误:
for l,h in r.split('-')
which it says not enough values to unpack. 它表示没有足够的值可解压。 My reasoning is that,
r
should have the form 'x-y'
, so if I split it by '-'
, I should get two values back. 我的推理是,
r
的形式应为'x-y'
,因此,如果将其除以'-'
,则应该返回两个值。 What's wrong with that? 怎么了
def parse_ranges(ranges):
"""Return a list of numbers corresponding to number ranges in a string"""
g = (
(l,h)
for r in ranges.split(',')
for l,h in r.split('-')
)
return (x
for range_tuple in g
for l,h in range_tuple
for x in range(int(l), int(h)+1)
)
parse_ranges('0-5,8-10,11-13')
You've got a loop too many. 您的循环太多了。 Since you're trying to keep the two numbers together you don't need a second nested loop.
由于您试图将两个数字保持在一起,因此不需要第二个嵌套循环。 The second one is flattening the results:
第二个是将结果展平:
>>> [x for r in s.split(',') for x in r.split('-')]
['0', '0', '3', '5', '7', '10']
Use one to keep the numbers paired: 使用一个使数字保持配对:
>>> [r.split('-') for r in s.split(',')]
[['0', '0'], ['3', '5'], ['7', '10']]
Then with the second comprehension you can combine the first two for
loops by unpacking l
and h
in place. 然后,通过第二个理解,可以通过将
l
和h
解包到适当的位置来组合前两个for
循环。
return (x
for l,h in g
for x in range(int(l), int(h)+1)
)
There is the intspan module which does just that. 有intspan模块可以做到这一点。
import intspan
s = '0-0,3-5,7-10'
>>> list(intspan.intspan(s))
[0, 3, 4, 5, 7, 8, 9, 10]
If your goal is to parse strings with that format into a list of ints, then it may be worth using a regex to parse it 如果您的目标是将具有该格式的字符串解析为int列表,则可能值得使用正则表达式对其进行解析
def parse_ranges(ranges):
"""Return a list of numbers corresponding to number ranges in a string"""
return re.split(',|-', ranges)
parse_ranges('0-5,8-10,11-13')
Outputs: 输出:
['0', '5', '8', '10', '11', '13']
To get them as ints, rather than strings, you could do the following: 要将它们获取为整数而不是字符串,可以执行以下操作:
def parse_ranges(ranges):
"""Return a list of numbers corresponding to number ranges in a string"""
return (int(x) for x in re.split(',|-', ranges))
list(parse_ranges('0-5,8-10,11-13'))
Outputs: 输出:
[0, 5, 8, 10, 11, 13]
I've been unable to figure-out how to avoid splitting each range string twice, but with that slight inefficiency, here's how it can be made into a single expression: 我一直无法弄清楚如何避免将每个范围字符串分割两次,但是由于效率低下,可以将其变成单个表达式:
def parse_ranges(ranges):
return tuple(x for r in
(range(int(pair.split('-')[0]), int(pair.split('-')[1])+1)
for pair in
ranges.split(',')) for x in r)
print(parse_ranges('0-0,3-5,7-10')) # -> (0, 3, 4, 5, 7, 8, 9, 10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.