帮助Python方法中的内循环

发布于 2024-12-06 05:32:55 字数 1027 浏览 1 评论 0原文

我无法理解为什么我的方法中的内部循环没有产生我期望的行为,我希望有人可以帮助我理解这个问题。

我的方法采用一系列参数(*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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝影如岚 2024-12-13 05:32:55

因为在这一行上:

            line_prefix = [''.join((str(value), " & ")) for value in penult_args]

您从原始列表中提取值(减去最后一项),而在这一行上:

            value = ''.join(('

您添加了 $ 但从未将值存储回列表中。

5 仅获取 $ 因为它是最后一项,因此您可以直接在以下位置引用它:

            line_suffix = [''.join((str(value), " \\\\", "\n"))]

完成这一切的更好方法是:

def t_row(self, *args):
    if len(args) == self.columns:
        result = []
        for value in args:
            if isinstance(value, int):
                result.append('$%d

作为单行代码,它将是

t_row = lambda self, *args: (' $ '.join('$%d

但这实际上并不是一个好主意。

, str(value), '

您添加了 $ 但从未将值存储回列表中。

5 仅获取 $ 因为它是最后一项,因此您可以直接在以下位置引用它:


完成这一切的更好方法是:


作为单行代码,它将是


但这实际上并不是一个好主意。

))

您添加了 $ 但从未将值存储回列表中。

5 仅获取 $ 因为它是最后一项,因此您可以直接在以下位置引用它:


完成这一切的更好方法是:


作为单行代码,它将是


但这实际上并不是一个好主意。

% value) else: result.append(value) return ' $ '.join(result) + r' \\'

作为单行代码,它将是


但这实际上并不是一个好主意。

, str(value), '

您添加了 $ 但从未将值存储回列表中。

5 仅获取 $ 因为它是最后一项,因此您可以直接在以下位置引用它:


完成这一切的更好方法是:


作为单行代码,它将是


但这实际上并不是一个好主意。

))

您添加了 $ 但从未将值存储回列表中。

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:

            line_prefix = [''.join((str(value), " & ")) for value in penult_args]

you pull the values out of the original list (minus the last item), while on this line:

            value = ''.join(('

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:

            line_suffix = [''.join((str(value), " \\\\", "\n"))]

A better way to do all this is:

def t_row(self, *args):
    if len(args) == self.columns:
        result = []
        for value in args:
            if isinstance(value, int):
                result.append('$%d

As a one-liner, it would be

t_row = lambda self, *args: (' $ '.join('$%d

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 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.

丿*梦醉红颜 2024-12-13 05:32:55

问题是因为在这一行中:

line_prefix = [''.join((str(value), " & ")) for value in penult_args]

您正在用原始(非美元符号)值覆盖 value

它仅适用于最后一个参数,因为上面的行不是为 args[-1] 调用的。

为循环使用不同的变量名称。

(Python 的作用域仅在函数和类内,for 循环和 if 语句没有独立的作用域。)

The problem is because in this line:

line_prefix = [''.join((str(value), " & ")) for value in penult_args]

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 and if statements are not independently scoped.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文