【问题标题】:How to split string in a list and compare each word with a set of key words如何在列表中拆分字符串并将每个单词与一组关键词进行比较
【发布时间】:2023-03-23 02:37:01
【问题描述】:

我有一个列表,内容如下:-

for 30 days
for 40 working days
for 20 weeks
for 2 months

我想拆分每个句子并与一组关键字进行比较:-

day
week
month
year

如果关键字'days' 出现在字符串中,那么我想将该字符串中的数字与'1' 相乘。如果存在关键字'month',则将该字符串中的数字乘以'30',依此类推……我是python新手,请!

我的代码

   with open("test_term.csv", "rb") as file1:
        reader = csv.reader(file1)
        extractedlist = list(reader)
        #print extractedlist
def split_line(text):
    # split the text
    words = text[0].split(' ')
    # for each word in the line:
    new_list = []
    for word in words:
        #print word
        #print w2n.word_to_num(word)
        conversion = w2n.word_to_num(word)
        if isinstance(conversion, (int,long)):
            #print conversion
            new_list.append(conversion)            

        else:
            new_list.append(word)


    return new_list

for extraRow in extractedlist:
    worn = split_line(extraRow)
    keywords = {"day":1,"days":1,"year":365,"years":365,"week":7,"weeks":7,"month":30,"months":30}
    #for s in worn:
     #   splitted_string = s.split(' ')
    interesting_words = worn[2:]
    mult = 1
    for k,v in keywords.iteritems():
        for word in interesting_words :
            mult = v
            break
        result = mult*worn[1]
        print result

现在我只有一个输入字符串for thirty working days这里'thirty'正在转换为'30'所以在穿我们有'for thirty working days' 输出是:-

210  
900  
10950
900  
210  
10950
30   
30   

但我期望的输出是 30*1 即'30'

【问题讨论】:

  • 内容真的是这样吗,还是左/右侧可以多加文字?
  • 检查this demo
  • 我刚刚发布了一个适合我的答案(我已经在我的电脑上试过了)
  • @javidgon...您能否检查一下我在问题中发布的代码并相应地更新您的答案?...提前谢谢!

标签: python string python-2.7


【解决方案1】:

你可以先创建一个字典: dictionnary = {"day":1, "month":30 ... }

使用拆分字符串,如:

splitted_string = ["for", 30, "working", "days"]
interesting_words = splitted_string[2:] # ["working", "days"]

从那里,您可以获取元素“days”并在您的字典中找到相应的元素。找到元素后,我们只需获取值并打破循环。

mult = 1
for k,v in dictionnary.iteritems():
    for word in interesting_words :
        if k in word :
            mult = v
            break

你终于可以执行你的操作了:

result = mult*splitted_string[1] #30

【讨论】:

  • splitted_string = wear.split(' ') ... 给出一个 AttributeError: 'list' object has no attribute 'split' ... 我该如何解决?
  • 这意味着wear已经是一个列表而不是一个字符串。 >>> s = "for 30 days" >>> s.split(' ') 给你:['for', '30', 'days']
  • 我如何拆分磨损...原因是,这只是我编写的代码的一部分,磨损实际上是经过另一次大量操作后得到的...无论如何我可以拆分字符串一个接一个地磨损...列表在磨损,我知道磨损本身就是一个列表..
  • worn应该已经是你的元素列表了,你可以打印出来确定一下。
  • ['for', 30, 'working', 'days'] 是打印磨损的输出
【解决方案2】:

如果您的数据在列表中,您可以对其进行迭代。然后拆分每个字符串并在列表末尾搜索一个关键字('day' in ' '.join(data_split[2:])):

data = ['for 30 days',
    'for 40 working days',
    'for 20 weeks',
    'for 2 months']

for d in data:
    data_split = d.split(' ')
    if 'day' in ' '.join(data_split[2:]):
        print(int(data_split[1]))
    elif 'month' in ' '.join(data_split[2:]):
        print(int(data_split[1]) * 30)

【讨论】:

    【解决方案3】:
    import csv     # imports the csv module
    
    f = open('file.csv', 'rb') # opens the csv file
    results = []
    try:
        reader = csv.reader(f)  # creates the reader object
        for row in reader:   # iterates the rows of the file in orders
            l = row[0].split(' ')
            if 'day' in l[2]:
                l[1] = int(l[1]) * 1
            elif 'working' in l[2]:
                if len(l) > 3  and 'day' in l[3]:
                    l[1] = int(l[1]) * 1
            elif 'week' in l[2]:
                l[1] = int(l[1]) * 7
            elif 'month' in l[2]:
                l[1] = int(l[1]) * 30
            elif 'year' in l[2]:
                l[1] = int(l[1]) * 365
            results.append(l)
    
    finally:
        print results
        f.close()      # closing
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 2012-11-07
      • 2020-11-07
      相关资源
      最近更新 更多