将多个if和elif语句应用于for循环中字符串列表中的子字符串

时间:2015-06-29 06:59:06

标签: python string if-statement for-loop substring

我的电子表格中填充了列(C1:C3159)中无组织的打开文本字段,我希望通过文本中的各种关键字进行排序。我正在尝试编写一些循环遍历列的python代码,查找关键字,并将该单元格中字符串的类别附加到空列表中,具体取决于在文本中找到的单词。到目前为止,我的代码看起来像这样。

attr = ['Age of plantation',
'Altitude of Plantation',
'Annual production Last year (In Kg)',
'Average Price paid per kg in NPR (Last Year)',
'Majority Bush type',
'Pruning Cycle',
'Tea sold to ( Last Year)',
'Boll weight in grams',
'CLCuV incidence %',
'Dibbles per row',
'Gap Filling',
'Germination %',
'Hoeing',
'Land preparation',
'Land preparation date',
'Pest & disease incidence',
'Plot size in metre Square',
'Rows per entry',
'Spacing between plants in cms']

我遇到的问题是,在第一个if语句之后,elif语句没有在循环中被评估,而我返回的列表只包含被归类为"害虫管理的几个东西。"有没有人知道如何做我在这里尝试做的事情,以便评估完整的循环?列表中的一小部分字符串发布在下面。

#include <stdio.h>

struct p
{  
    int x;
    int y;
};

int main()
{   
    int p2 = 55;
    int p3 = 99;
    //const struct p *ptr1 = {&p2,&p3};  --- giving the expected result
    const struct p *ptr1;
    ptr1->x = &p2;  //error
    ptr1->y = &p3;  //error
    printf("%d %d \n", ptr1->x, ptr1->y);
}

3 个答案:

答案 0 :(得分:1)

<强>修改

你必须使用in检查if case

中的所有字符串
if 'pest' in i or 'weed' in i or 'disease' in i or 'cide' in i or 'incid' in i or 'trap' in i  or 'virus' in i or 'IPM' in i or 'blight' in i or 'incid' in i or 'rot' in i or 'suck' in i:

每次在您的计划中,由于if

,第一个if 'pest' or声明为真

在python中

如果使用仅""的语句来检查它是否为空字符串。如果它是空字符串False则返回True。由于此属性, if案例匹配

if "sad":
    print "Why!"
output: Why!

if "":
    print "Why!"
output:         

答案 1 :(得分:1)

if语句未对elif语句进行评估

if-elif语句是互斥的。如果您希望在第一个if将{1}}中的每个语句放入if而非if

之后评估其他elif条件

答案 2 :(得分:0)

我会使用正则表达式。

很多人争辩说,如果你用正则表达式解决问题,你最终会遇到两个问题,但我相信如果你干净利落地做,你可以避免这种困境。

import re

pestmanagementattributes = [
    'pest', 'weed', 'disease', 'cide', 'incid', 'trap',
    'virus', 'IPM', 'blight', 'incid', 'rot', 'suck'
]
r_pestmanagement = re.compile(".*" + (".*|.*".join(pestmanagementattributes)) + ".*")

fertilizerattributes = ['fert', 'dap', 'urea', 'manga', 'npk', 'inm']
r_fertilizer = re.compile(".*" + (".*|.*".join(fertilizerattributes)) + ".*")

for i in attr:
    if r_pestmanagement.match(i):
        categories.append("pest management")
    elif r_fertilizer.match(i):
        categories.append("fertilizer")
...
    else:
        categories.append("uncategorized")

这也应该要快得多,因为你的字符串i每个类别只扫描一次,而不是每个字一次。