未绑定的本地错误:(“赋值前引用的局部变量”)

时间:2017-09-13 19:09:04

标签: pandas numpy python-2.x

 <form name="simple" action="POST">

            <label for="name">
                Name:
            </label>
            <input type="text" id="demo" class="form-control" onsubmit="validate();"><br>

            <label for="email">
                E-mail:
            </label>
            <input type="email" id="email" class="form-control"><br>

            <label for="pwd">
                Password:
            </label>
            <input type="password" id="pwd" class="form-control"><br>

            <label for="phone">
                Phone:
            </label>
            <input type="text" id="phone" class="form-control"><br>

            <input type="button" type="submit" value ="Submit" class="form-control" onclick="validate();" >

            <input type="button" type="reset" value ="Reset" class="form-control">

        </form>
        <script>


        function validate()
    {
        var txt = document.getElementById("demo");
        alert(txt);
        if(txt == " " || txt == null)
        {
            alert("Name can't be left blank");
        }

    }
        </script>

我错过了什么吗?我写了类似于这么多次的东西,我从来没有得到过这个错误,但也许这真的很蠢?

2 个答案:

答案 0 :(得分:0)

OHH我刚刚运行了我的逻辑。我认为这确实是一件愚蠢的事。

cabinsubstr = ['C23', 'C25', 'C27']
for i in cabinsubstr:
    i.find('F ') != -1

甚至不读真,因为如果因为它没有找到它,我应该有它= = - 1。我是个白痴;)

答案 1 :(得分:0)

使用vectorized string methods编写此内容的另一种方法是:

import pandas as pd
import numpy as np
nan = np.nan
df = pd.DataFrame([{'Cabin': 'F G13'},
                   {'Cabin': 'A32 A45'},
                   {'Cabin': 'F23 F36'},
                   {'Cabin': 'B24'},
                   {'Cabin': nan}])

cabin_parts = df['Cabin'].str.split(' ', expand=True)
conditions = [pd.isnull(df['Cabin']), 
              df['Cabin'].str.startswith('F').astype(bool), 
              ~df['Cabin'].str.contains('F').astype(bool)]
choices = [None, 
           cabin_parts[1].str[0], 
           cabin_parts[0].str[0]]

df['Deck_ID'] = np.select(conditions, choices)

产生

     Cabin Deck_ID
0    F G13       G
1  A32 A45       A
2  F23 F36       F
3      B24       B
4      NaN    None

或者,如果我理解Cabin - &gt; Deck_ID正确命名模式,也许是

df['Deck_ID'] = df['Cabin'].str.extract(r'(\D\d*)?\s*(\D\d+)', expand=True)[1].str[0]

就足够了,因为

In [86]: df['Cabin'].str.extract(r'(\D\d*)?\s*(\D\d+)', expand=True)
Out[86]: 
     0    1
0    F  G13
1  A32  A45
2  F23  F36
3  NaN  B24
4  NaN  NaN

正则表达式模式(\D\d*)?\s*(\D\d+)具有以下含义:

(\D\d*)?     first capturing group: 0-or-1 (nondigit followed by 0-or-more digits)
\s*          0-or-more whitespace
(\D\d+)      second capturing group: (nondigit followed by 1-or-more digits)