【问题标题】:Correct the spellings of words in string with the help of a dictionary?借助字典更正字符串中单词的拼写?
【发布时间】:2019-01-08 12:01:00
【问题描述】:

我有一个输入列表,其中 REJECTED 的拼写写为 RE1ECTED,而 DATE 的拼写写为 OATE。 我需要更正拼写并得到一个输出列表:

output_list = ["REJECTED 00000 00000","DATE TIME =06/27/18 21 37","NACH NE NO XY"]

我能够单独获得更正的列表,但不像上面显示的串联。我可以期待一些建议吗?

input_list = ["=RE1ECTED 00000 00000",'OATE TIME =06/27/18 21 37','NACH NE NO xy']
Reg_dictionary = {"REJECTED" : ["=RE"],"DATE" : ["OA","DA"] }
for key,value in Reg_dictionary.items():
    temp = [key, value]
    for each_value in value:
        for string_list in input_list:
            count  = -1
            # print (count_number_of_string)
            each_in_string  = string_list.split(" ")
            for each_word in each_in_string:
                count = count + 1
                if each_value in each_word:
                    # print (count)
                    # print (string_list)
                    # print(each_word)
                    # print (key)
                    # print(each_in_string)
                    (each_in_string[count]) =key
                    print(each_in_string)

print(each_in_string) 的输出是这样的:

['REJECTED', '00000', '00000']

['DATE', 'TIME', '=06/27/18', '21', '37']

预期输出是:

output_list = ["REJECTED 00000 00000","DATE TIME =06/27/18 21 37","NACH NE NO XY"]

【问题讨论】:

    标签: python regex string


    【解决方案1】:

    您必须将列表中的元素连接在一起,然后将列表连接在一起。您可以使用空格作为元素之间的分隔符连接列表中的元素,如下所示:' '.join(your_list),这将为您提供字符串。然后只需将 2 个字符串与 [string_1 + ' ' + string_2] 连接在一起。应该这样做:)

    【讨论】:

      【解决方案2】:

      你可以这样做:

      from functools import reduce 
      
      input_list = ["=RE1ECTED 00000 00000",'OATE TIME =06/27/18 21 37','NACH NE NO xy']
      Reg_dictionary = {"REJECTED" : ["=RE"],"DATE" : ["OA","DA"] }
      l = []
      for key,value in Reg_dictionary.items():
          temp = [key, value]
          for each_value in value:
              for string_list in input_list:
                  count  = -1
                  # print (count_number_of_string)
                  each_in_string  = string_list.split(" ")
                  for each_word in each_in_string:
                      count = count + 1
                      if each_value in each_word:
                          # print (count)
                          # print (string_list)
                          # print(each_word)
                          # print (key)
                          # print(each_in_string)
                          (each_in_string[count]) =key
                          #print(each_in_string)
                          l.append(reduce(lambda x,y: x+' '+y, each_in_string)) # added this line
      print(l)
      

      【讨论】:

      • 嗨@Rodwan Bakkar 感谢您的帮助,我需要一个包含所有三个字符串的输出。 output_list = ["拒绝 00000 00000","日期时间 =06/27/18 21 37","NACH NE NO XY"]
      • 要做到这一点,根据您的方法,您必须添加 Reg_dictionary = {"REJECTED" : ["=RE"],"DATE" : ["OA","DA"], "NACH" : ["NA"] } 之类的内容,但这不是正确的方法,您的代码有点混乱。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 2016-07-13
      • 2016-09-06
      • 2020-02-08
      • 2018-05-23
      • 1970-01-01
      相关资源
      最近更新 更多