繁体   English   中英

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

[英]applying multiple if and elif statements to substrings in a list of strings in a for loop

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

## make an object attr for the column    
attr = ['C1:C3159']
## make all lower case
[x.lower() for x in attr]
## initialize an empty list
categories = []
## loop through attr object and append categories to the "categories" list
for i in attr:
    if 'pest' or 'weed' or 'disease' or 'cide' or 'incid' or 'trap'/
    or 'virus' or 'IPM' or 'blight' or 'incid' or 'rot' or 'suck' in i:
        categories.append("pest management")

    elif 'fert' or 'dap' or 'urea' or 'manga' or 'npk' pr 'inm' in i:
        categories.append("fertilizer")

    elif 'wind' or 'rain' or 'irr' or 'alt' or 'moist' or 'soil' or 'ph'\
    or 'drip'or 'environ' or 'ec' in i:
        categories.append("environment")

    elif 'spac' or 'name' or 'stor' or 'yield' or 'rogu' or 'maint'\
    or 'cond' or 'prod' or 'fenc' or 'child' or 'row' or 'prun' or 'hoe'\
    or 'weight' or 'prep' or 'plot' or 'pull' or 'topp' in i:
        categories.append("operations")

    elif 'plant' or 'germin' or 'age' or 'bulk' or 'buds'  or 'matur'\
    or 'harvest' or 'surviv' or 'health' or 'height' or 'grow' in i:
        categories.append("life cycle")

    elif 'price' or 'sold' or 'inr' or 'cost' in i:
        categories.append("market")

    elif 'shed' or 'post' or 'fenc' or 'pond' or 'stor' in i:
        categories.append("PPE")

    else:
        categories.append("uncategorized")

我遇到的问题是,在第一个if语句之后,不会在循环中评估elif语句,并且返回的列表仅包含归类为“害虫管理”的几样东西。 有谁知道如何做我在这里试图做的事情,以便评估整个循环? 下面是列表中字符串的一小部分。

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

修改

您必须使用检查in所有字符串中,如果情况

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:         

if语句,不评估elif语句

if-elif语句是互斥的。 如果您希望其他if条件在第一个if之后求值, if每个语句放入if而不是elif

我会为此使用正则表达式。

许多人认为,如果用正则表达式解决问题,最终会遇到两个问题,但是我相信,如果您做得很整洁,就可以避免这种困境。

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每个类别仅扫描一次,而不是每个单词一次,因此执行起来也应该更快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM