使用列表理解进行元组拆包失败,但适用于for循环

问题描述 投票:0回答:1

摘要

我使用了半复杂的正则表达式从网站检索数据。我的问题是必须对匹配的数据集进行一些后处理。

我已经使数据处理达到我想要的位置的95%以上,但是,我收到了我无法推理的简单错误消息;真奇怪。

我可以绕过它,但是那不是重点。 我正在尝试弄清这是一个错误还是我在打开元组时从根本上忽略了某些东西

背景信息

我必须克服的一件事是,每个“真正的比赛”都会得到4个比赛。这意味着我针对一项的数据分散在4个匹配项中。

以简单的图形形式(略微简化)

index |  a    b    c    d    e    f    g    h    i    j 
--------------------------------------------------------
   1: | ( ), ( ), ( ), ( ), ( ), (█), ( ), ( ), ( ), ( )
   2: | (█), (█), (█), (█), ( ), ( ), ( ), ( ), ( ), ( )
   3: | ( ), ( ), ( ), ( ), (█), ( ), ( ), ( ), ( ), ( )
   4: | ( ), ( ), ( ), ( ), ( ), ( ), (█), (█), (█), (█)

   5: | ( ), ( ), ( ), ( ), ( ), (▒), ( ), ( ), ( ), ( )
   6: | (▒), (▒), (▒), (▒), ( ), ( ), ( ), ( ), ( ), ( )
   7: | ( ), ( ), ( ), ( ), (▒), ( ), ( ), ( ), ( ), ( )
   8: | ( ), ( ), ( ), ( ), ( ), ( ), (▒), (▒), (▒), (▒)

   9: | ...
        ...
 615: | ...

我可以获取所有数据,但是我想像这样对它进行压缩...

index |  a    b    c    d    e    f    g    h    i    j 
--------------------------------------------------------
   1: | (█), (█), (█), (█), (█), (█), (█), (█), (█), (█)
   2: | (▒), (▒), (▒), (▒), (▒), (▒), (▒), (▒), (▒), (▒)

   3: | ...
        ...
 154: | ...

数据

matches = [('', '', '', '', '', '', '', '', '', ''), ('Android Studio 3.6 Beta 1', '3.6', 'Beta', '1', '', '', '', '', '', ''), ('', '', '', '', 'October 10, 2019', '', '', '', '', ''), ('', '', '', '', '', '', 'https://dl.google.com/dl/android/studio/ide-zips/3.6.0.13/android-studio-ide-192.5916306-linux.tar.gz', '3.6.0', '13', '192'), ('', '', '', '', '', 'stable', '', '', '', ''), ('Android Studio 3.5.1', '3.5.1', '', '', '', '', '', '', '', ''), ('', '', '', '', 'October 2, 2019', '', '', '', '', ''), ('', '', '', '', '', '', 'https://dl.google.com/dl/android/studio/ide-zips/3.5.1.0/android-studio-ide-191.5900203-linux.tar.gz', '3.5.1', '0', '191'), ('', '', '', '', '', '', '', '', '', ''), ('Android Studio 3.6 Canary 12', '3.6', 'Canary', '12', '', '', '', '', '', ''), ('', '', '', '', 'September 18, 2019', '', '', '', '', ''), ('', '', '', '', '', '', 'https://dl.google.com/dl/android/studio/ide-zips/3.6.0.12/android-studio-ide-192.5871855-linux.tar.gz', '3.6.0', '12', '192')]

代码

Works

请注意变量abcdefghij以及我必须如何在底部的for-loop拆包它们

f = [f for index, (_, _, _, _, _, f, *_) in enumerate(matches) if index % 4 == 0]
abcd = [(a, b, c, d) for index, (a, b, c, d, *_) in enumerate(matches) if index % 4 == 1]
e = [e for index, (_, _, _, _, e, *_) in enumerate(matches) if index % 4 == 2]
ghij = [(g, h, i, j) for index, (*_, g, h, i, j) in enumerate(matches) if index % 4 == 3]

abcdefghij = zip(abcd, e, f, ghij)

for (a, b, c, d), e, f, (g, h, i, j) in abcdefghij:
    print("a", a, "\nb", b, "\nc", c, "\nd", d, "\ne", e, "\nf", f, "\ng", g, "\nh", h, "\ni", i, "\nj", j, "\n", "-" * 100)

失败

[请注意,我正在尝试使用变量abcdefgh立即打开相同的元组的包装, ij

f = [f if f == "stable" else "preview" for index, (_, _, _, _, _, f, *_) in enumerate(matches) if index % 4 == 0]
a, b, c, d = [(a, b, c, d) for index, (a, b, c, d, *_) in enumerate(matches) if index % 4 == 1]
e = [e for index, (_, _, _, _, e, *_) in enumerate(matches) if index % 4 == 2]
g, h, i, j = [(g, h, i, j) for index, (*_, g, h, i, j) in enumerate(matches) if index % 4 == 3]

abcdefghij = zip(a, b, c, d, e, f, g, h, i, j)

for a, b, c, d, e, f, g, h, i, j in abcdefghij:
    print("a", a, "\nb", b, "\nc", c, "\nd", d, "\ne", e, "\nf", f, "\ng", g, "\nh", h, "\ni", i, "\nj", j, "\n", "-" * 100)

使用此代码,我收到以下错误消息...

... a, b, c, d = [(a, b, c, d) for index, (a, b, c, d, *_) in enumerate(matches) if index % 4 == 1]`
ValueError: too many values to unpack (expected 4)`

期望

我本来希望这两种方法执行完全相同的逻辑,并且最终结果应该完全相同。

它们不是!为什么?

python python-3.x tuples iterable-unpacking
1个回答
0
投票

您的列表[(a, b, c, d) for index, (a, b, c, d, *_) in enumerate(matches) if index % 4 == 1]具有4个以上的元素,这意味着仅使用四个变量尝试对其进行解压缩将失败。

代替

a, b, c, d = [(a, b, c, d) for index, (a, b, c, d, *_) in enumerate(matches) if index % 4 == 1]

尝试

a, b, c, d, *e = [(a, b, c, d) for index, (a, b, c, d, *_) in enumerate(matches) if index % 4 == 1]

© www.soinside.com 2019 - 2024. All rights reserved.