用于解包带括号抑制的列表的 f-string 语法 [英] f-string syntax for unpacking a list with brace suppression

查看:72
本文介绍了用于解包带括号抑制的列表的 f-string 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用新的 f-string 格式检查我的一些字符串格式选项.我经常需要解压缩列表和其他未知长度的迭代.目前我使用以下...

<预><代码>>>>a = [1, 'a', 3, 'b']>>>("解压一个列表:" + " {} "*len(a)).format(*a)'解压列表:1 a 3 b '

这虽然有点麻烦,但使用 3.6 之前的 .format 符号来完成这项工作.考虑到运行时字符串连接,新的 f-string 格式选项很有趣.这是我遇到问题的 {} 数量的复制.在我之前的示例中,我只是创建了必要的结构并在 .format() 部分中解压.

尝试这样做产生了一种有效的变体:

1) 两个大括号放在一起不会解压...

<预><代码>>>>'解压一个列表' f' {{*a}}''解压一个列表{*a}'

2) 在内部 {} 对周围添加空格:

这有效,但留下左大括号 {, } 存在:

<预><代码>>>>'解压列表' f' { {*a} }'"解包一个列表 {1, 3, 'a', 'b'}"

2b) 将变体连接成一个 f 字符串

这使得外观和语法更好,因为显然是从左到右进行评估.然而,这仍然留下了封闭的大括号:

<预><代码>>>>f'解压一个列表 { {*a} }'"解包一个列表 {1, 3, 'a', 'b'}"

3) 尝试仅使用 {a}

自动解包

也许,我对整个过程想得太多,并希望有某种形式的自动解包.这只是产生了用 [] 替换大括号的列表表示:

<预><代码>>>>f'解压一个列表{a}'解压一个列表 [1, 'a', 3, 'b']"

需要什么来抑制上述变体 (2) 中的大括号,或者我必须继续使用现有的 .format() 方法?我想保持简单并使用 f-string 提供的新功能,而不是恢复到比我目前满意的 Python 版本更早的版本.我开始怀疑 f'strings' 没有提供其 .format() 兄弟提供的内容的完整覆盖.我暂时将其搁置一旁,因为我什至还没有涉足转义编码和无法在 f 字符串中使用 \.我已经阅读了 PEP 并进行了广泛的搜索,但是,我觉得我遗漏了显而易见的东西,或者我希望的东西目前是不可能的.

几个小时后

4) 使用下标手动切掉括号:str(a)[1:-2]

我确实找到了这个变体,它可以满足我需要的某些情况

f'解压一个列表:{str(a)[1:-2]}'"解压一个列表:1, 'a', 3, 'b"

但是切片只是为了方便,并且仍然在结果周围留下字符串引号.

5) 和@SenhorLucas 的最终解决方案

 a = np.arange(10)打印(f"{*a,}")(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

用尾随逗号解包.

解决方案

只需在解压后的列表后添加一个逗号即可.

a = [1, 2, 3]打印(f解包清单:{*a,}")

对此语法有更长的解释in这个话题.

I have been examining some of my string format options using the new f-string format. I routinely need to unpack lists and other iterables of unknown length. Currently I use the following...

>>> a = [1, 'a', 3, 'b']
>>> ("unpack a list: " + " {} "*len(a)).format(*a)
'unpack a list:  1  a  3  b '

This, albeit a bit cumbersome, does the job using pre-3.6 .format notation. The new f-string format option is interesting given runtime string concatenation. It is the replication of the number of {} that I am having problems with. In my previous example, I simply created the necessary structure and unpacked within the .format() section.

Attempts to do this yielded one variant that worked, however:

1) Both curly brackets together doesn't unpack...

>>> 'unpack a list'  f' {{*a}}'
'unpack a list {*a}'

2) Adding spaces around the interior {} pair:

This works but leaves opening and closing braces {, } present:

>>> 'unpack a list'  f' { {*a} }'
"unpack a list {1, 3, 'a', 'b'}"

2b) Concatenating the variants into one f-string

This made the look and syntax better, since the evaluation, apparently, is from left to right. This, however, still left the enclosing curly brackets present:

>>> f'unpack a list { {*a} }'
"unpack a list {1, 3, 'a', 'b'}"

3) Tried automatic unpacking with just {a}

Perhaps, I was overthinking the whole procedure and hoping for some form of automatic unpacking. This simply yielded the list representation with the curly brackets being replaced with [] :

>>> f'unpack a list {a}'
"unpack a list [1, 'a', 3, 'b']"

What is required to suppress the curly brackets in variant (2) above, or must I keep using the existing .format() method? I want to keep it simple and use the new capabilities offered by the f-string and not revert back beyond the python versions which pre-date what I am currently comfortable with. I am beginning to suspect that f'strings' do not offer a complete coverage of what is offered by its .format() sibling. I will leave it at that for now, since I haven't even ventured into the escape encoding and the inability to use \ in an f-string. I have read the PEP and search widely, however, I feel I am missing the obvious or what I wish for is currently not possible.

EDIT several hours later:

4) Use subscripting to manually slice off the brackets: str(a)[1:-2]

I did find this variant which will serve for some cases that I need

f'unpack a list: {str(a)[1:-2]}'
"unpack a list: 1, 'a', 3, 'b"

But the slicing is little more than a convenience and still leaves the string quotes around the resultant.

5) and the final solution from @SenhorLucas

 a = np.arange(10)

print(f"{*a,}")
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Unpacking with trailing comma.

解决方案

Just add a coma after the unpacked list.

a = [1, 2, 3]
print(f"Unpacked list: {*a,}")

There is a longer explanation to this syntax in this thread.

这篇关于用于解包带括号抑制的列表的 f-string 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆