How to use the pass statement?

前端 未结 15 2505
旧时难觅i
旧时难觅i 2020-11-22 15:03

I am in the process of learning Python and I have reached the section about the pass statement. The guide I\'m using defines it as being a Null sta

15条回答
  •  深忆病人
    2020-11-22 15:19

    Here's an example where I was extracting particular data from a list where I had multiple data types (that's what I'd call it in R-- sorry if it's the wrong nomenclature) and I wanted to extract only integers/numeric and NOT character data.

    The data looked like:

    >>> a = ['1', 'env', '2', 'gag', '1.234', 'nef']
    >>> data = []
    >>> type(a)
    
    >>> type(a[1])
    
    >>> type(a[0])
    
    

    I wanted to remove all alphabetical characters, so I had the machine do it by subsetting the data, and "passing" over the alphabetical data:

    a = ['1', 'env', '2', 'gag', '1.234', 'nef']
    data = []
    for i in range(0, len(a)):
        if a[i].isalpha():
            pass
        else:
            data.append(a[i])
    print(data)
    ['1', '2', '1.234']
    

提交回复
热议问题