使用列表推导提取元组列表

时间:2018-02-28 00:44:30

标签: python python-2.7 list-comprehension

我正在尝试从元组列表列表中提取数据,但我遇到了一个问题,因为它们与正常for循环和此列表理解循环有不同的输出:

这是我的示例数据,实际上来自我的数据库并将其作为unicode返回,我只是转换为列表:

[[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]]
[[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]

通过使用我的简单for循环,这是正确的输出:

for i in datas:
    print i
[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]
[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]
[(2108, u'0930')]
[(3012, u'1')]
[(2108, u'0930')]
[(3012, u'1')]
[(2108, u'0930')]
[(3012, u'1')]
[(2108, u'0930')]
[(3012, u'1')]

但如果我将使用列表理解风格,那么现在将是不同的:

print [i for i in datas]
[[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]]
[[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]

提前感谢任何意见或建议!

1 个答案:

答案 0 :(得分:0)

通过for循环,您打印列表中的项目,但在列表解析本身创建外部列表。

我认为你需要,

Imports System.Text.RegularExpressions 'At the top of the form class

 Public Class FormName

 Private Sub Tbx_EscuelaElegida_LostFocus(.....

 'DECLARE VARIABLE AND ASSIGN THE TEXTBOX CONTENT TO IT
    Dim TestsVariable As String = TextboxName.text


    'IF IT DOES NOT CONTAIN INTEGERS, SHOW A MESSAGE
    If Not Regex.IsMatch(TestsVariable, "^[0-9 ]+$") Then

        MsgBox("No Decimal Numbers")

    End If

 End sub
End Class