帮助Python方法中的内循环
我无法理解为什么我的方法中的内部循环没有产生我期望的行为,我希望有人可以帮助我理解这个问题。
我的方法采用一系列参数(*args
),如果参数是整数,我想在整数周围添加美元符号(例如$5$
)。
def t_row(*args):
columns = 5
if len(args) == columns:
count = 0
for value in args:
if type(value) is int:
value = ''.join(('$', str(value), '$'))
count += 1
if count < len(args):
penult_args = args[:-1]
line_prefix = [''.join((str(value), " & ")) for value in penult_args]
elif count == len(args):
line_suffix = [''.join((str(value), " \\\\", "\n"))]
count += 1
line_list = line_prefix + line_suffix
line = ''.join(item for item in line_list)
return(line)
上面的代码是这样使用的:
>>> s = q.t_row('data1', 'data2', 3, 'data4', 5)
>>> print s
data1 & data2 & 3 & data4 & $5$ \\
为什么我在整数 3 周围没有得到美元符号?我该如何修复我的代码来纠正这个问题?
I'm having trouble understanding why an inner loop in my method isn't producing the desired behavior I'm expecting and I'm hoping someone can help me understand the problem.
My method takes a series of arguments (*args
) and if the argument is an integer I want to add dollar signs around the integer (eg. $5$
).
def t_row(*args):
columns = 5
if len(args) == columns:
count = 0
for value in args:
if type(value) is int:
value = ''.join(('
The above code is used like this:
>>> s = q.t_row('data1', 'data2', 3, 'data4', 5)
>>> print s
data1 & data2 & 3 & data4 & $5$ \\
Why don't I get dollar signs around the integer 3? How can I fix my code to correct this problem?
, str(value), '
The above code is used like this:
Why don't I get dollar signs around the integer 3? How can I fix my code to correct this problem?
))
count += 1
if count < len(args):
penult_args = args[:-1]
line_prefix = [''.join((str(value), " & ")) for value in penult_args]
elif count == len(args):
line_suffix = [''.join((str(value), " \\\\", "\n"))]
count += 1
line_list = line_prefix + line_suffix
line = ''.join(item for item in line_list)
return(line)
The above code is used like this:
Why don't I get dollar signs around the integer 3? How can I fix my code to correct this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为在这一行上:
您从原始列表中提取值(减去最后一项),而在这一行上:
您添加了
$
但从未将值存储回列表中。5
仅获取$
因为它是最后一项,因此您可以直接在以下位置引用它:完成这一切的更好方法是:
作为单行代码,它将是
但这实际上并不是一个好主意。
% value if isinstance(value, int) else value for value in args) + r' \\' if len(args) == self.columns else None)但这实际上并不是一个好主意。
, str(value), '您添加了
$
但从未将值存储回列表中。5
仅获取$
因为它是最后一项,因此您可以直接在以下位置引用它:完成这一切的更好方法是:
作为单行代码,它将是
但这实际上并不是一个好主意。
))您添加了
$
但从未将值存储回列表中。5
仅获取$
因为它是最后一项,因此您可以直接在以下位置引用它:完成这一切的更好方法是:
作为单行代码,它将是
但这实际上并不是一个好主意。
% value) else: result.append(value) return ' $ '.join(result) + r' \\'作为单行代码,它将是
但这实际上并不是一个好主意。
, str(value), '您添加了
$
但从未将值存储回列表中。5
仅获取$
因为它是最后一项,因此您可以直接在以下位置引用它:完成这一切的更好方法是:
作为单行代码,它将是
但这实际上并不是一个好主意。
))您添加了
$
但从未将值存储回列表中。5
仅获取$
因为它是最后一项,因此您可以直接在以下位置引用它:完成这一切的更好方法是:
作为单行代码,它将是
但这实际上并不是一个好主意。
Because on this line:
you pull the values out of the original list (minus the last item), while on this line:
You added
$
but never stored the value back into the list.The
5
only gets$
because it's the last item, so you reference it directly in:A better way to do all this is:
As a one-liner, it would be
but that's not actually a good idea.
% value if isinstance(value, int) else value for value in args) + r' \\' if len(args) == self.columns else None)but that's not actually a good idea.
, str(value), 'You added
$
but never stored the value back into the list.The
5
only gets$
because it's the last item, so you reference it directly in:A better way to do all this is:
As a one-liner, it would be
but that's not actually a good idea.
))You added
$
but never stored the value back into the list.The
5
only gets$
because it's the last item, so you reference it directly in:A better way to do all this is:
As a one-liner, it would be
but that's not actually a good idea.
% value) else: result.append(value) return ' $ '.join(result) + r' \\'As a one-liner, it would be
but that's not actually a good idea.
, str(value), 'You added
$
but never stored the value back into the list.The
5
only gets$
because it's the last item, so you reference it directly in:A better way to do all this is:
As a one-liner, it would be
but that's not actually a good idea.
))You added
$
but never stored the value back into the list.The
5
only gets$
because it's the last item, so you reference it directly in:A better way to do all this is:
As a one-liner, it would be
but that's not actually a good idea.
问题是因为在这一行中:
您正在用原始(非美元符号)值覆盖
value
。它仅适用于最后一个参数,因为上面的行不是为 args[-1] 调用的。
为循环使用不同的变量名称。
(Python 的作用域仅在函数和类内,
for
循环和if
语句没有独立的作用域。)The problem is because in this line:
You are overwriting
value
with the original (non-dollar-signed) value.It works for the last argument only because the above line is not called for args[-1].
Use a different variable name for your loop.
(Python's scoping is only within functions and classes,
for
loops andif
statements are not independently scoped.)