摘要

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

我已经将数据处理达到了我想要的位置的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: | ...




作品

请注意变量abcdefghij,以及如何在底部的for-loop中解压缩它们

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')]

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,和i

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')]

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)`



期望

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

他们不是!为什么?

最佳答案

@PaulPanzer似乎起作用。我将必须验证一切是否正确排列。但是我为什么需要那?


q是可迭代的,您的理解会为此(?)生成一个包含26个元组的列表,每个元组有4个项目。

z = [(a,b,c,d) for i, (a,b,c,d,*e) in enumerate(q)]


In [6]: len(z)
Out[6]: 26

In [7]: len(z[0])
Out[7]: 4

In [17]: z[:3]
Out[17]: [('a', 'a', 'a', 'a'), ('b', 'b', 'b', 'b'), ('c', 'c', 'c', 'c')]


当您尝试打开包装时,尝试将26个项目填充到四个名称/变量中

In [8]: a,b,c,d = z
Traceback (most recent call last):

  File "<ipython-input-8-64277b78f273>", line 1, in <module>
    a,b,c,d = z

ValueError: too many values to unpack (expected 4)


zip(*list_of_4_item_tuples)list_of_4_item_tuples转换为4个元组,每个元组有26个项目

In [9]:

In [9]: a,b,c,d = zip(*z)    # z is the result of the list comprehension shown above

In [11]: len(a),len(b),len(c),len(d)
Out[11]: (26, 26, 26, 26)




测试的东西

import string
a = string.ascii_lowercase
b = string.ascii_lowercase
c = string.ascii_lowercase
d = string.ascii_lowercase
e = string.ascii_lowercase
f = string.ascii_lowercase
q = zip (a,b,c,d,e,f)

关于python - 使用列表理解进行元组拆包失败,但可用于for循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58401261/

10-12 00:15
查看更多