【问题标题】:Python: getting a certain no. of strings from a dictionaryPython:得到肯定的答案。字典中的字符串
【发布时间】:2012-01-06 08:01:51
【问题描述】:

我有一个以下格式的字典,我使用拆分函数拆分不同的元素(出现逗号(,)的地方),现在我正在尝试从列表中提取名称...我正在尝试使用常规表达式,但显然我很不幸地未能成为 python 新手...名称采用以下格式...

  1. 名字(空格)姓氏
  2. 姓名(空格​​)姓名(空格​​)姓名
  3. x.name
  4. x.y.name
  5. 姓名(空格​​)x.(空格)(姓名)

其中 x 和 y 表示名称首字母,例如 J. 代表 john 等。 另外,如果您可以指导我删除“\ t”,保持其他信息不变也很好。 任何形式的帮助都会受到欢迎......谢谢大家。

[[' I. Antonov', ' I. Antonova', ' E. R. Kandel', ' and R. D. Hawkins. Activity-dependent presynaptic facilitation and hebbian ltp are both required and interact during classical conditioning in aplysia. Neuron', ' 37(1):135--47', ' Jan 2003.'], ['\tSander M. Bohte ', ' Joost N. Kok', ' Applications of spiking neural networks', ' Information Processing Letters', ' v.95 n.6', ' p.519-520'], [' L. J. Eshelman. The CHC Adaptive Search Algorithm: How to Have Safe Search When Engaging in Nontraditional Genetic Recombination. Foundations Of Genetic Algorithms', ' pages 265-283', ' 1990.'], ['Wulfram Gerstner ', ' Werner Kistler', ' Spiking Neuron Models: An Introduction', ' Cambridge University Press', ''], [' D. O. Hebb. Organization of behavior. New York: Wiley', ' 1949.'], [' D. Z. Jin. Spiking neural network for recognizing spatiotemporal sequences of spikes. Physical Review E', '69', ' 2004.'], ['Wolfgang Maass ', ' Christopher M. Bishop', ' Pulsed Neural Networks', ' MIT Press', ' '], ['Wolfgang Maass ', ' Henry Markram', ' Synapses as dynamic memory buffers', ' Neural Networks', ' v.15 n.2', ' p.'], [' H. Markram', ' Y. Wang', ' and M. Tsodyks. Differential signaling via the same axon of neocortical pyramidal neurons. Neurobiology', ' 95:5323--5328', ' April 1998.'], ['\t\tD. E. Rumelhart ', ' G. E. Hinton ', ' R. J. Williams', ' Learning internal representations by error propagation', ' Parallel distributed processing: explorations in the microstructure of cognition', ' vol. 1: foundations', ' MIT Press', ' Cambridge', ' MA', ' 1986 </a> \t\t\t\t\t\t\t\t\t'], ['\t J. D. Schaffer', ' L. D. Whitley', ' and L. J. Eshelman. Combinations of genetic algorithms and neural networks: A survey of the state of the art. In Combinations of Genetic Algorithms and NeuralNetworks', ' 1992.', ' COGANN-92. International Workshop on', ' pages 1--37', ' Philips Labs.', ' Briarcliff Manor', ' NY', ' 6 Jun 1992.'], ['\t S. Song', ' K. D. Miller', ' and L. F. Abbott. Competitive hebbian learning through spike-timing-dependent synaptic plasticity. Nature Neuroscience', ' 3(9):919--926', ' 2000.'], ['\t L. Watts. Event-driven simulation of networks of spiking neurons. Advances in Neural Information Processing Systems', ' 6:927--934', ' 1994.']]

【问题讨论】:

    标签: python dictionary design-patterns


    【解决方案1】:

    看起来您将不得不根据您的输入进行大量调整。因为您正在解析的文本中有很多不同的单词和结构,所以您可能无法获得 100% 的准确度来创建规则。不过,这里有一个示例,假设您的原始输入文本称为 input_text(而且我认为使用 split() 方法并没有那么有用,因为逗号不只是分隔​​名称):

    import re
    
    regexes = (r'[A-Z][a-z]+ [A-Z][a-z]+', # capitalized first and last name
               r'[A-Z]\. [A-Z][a-z]+')     # capitalized initial, then last name
    names = []
    
    for regex in regexes:
        names += re.findall(regex, input_text)
    

    您显然希望为您的各种名称类型编写额外的特定正则表达式。这在查找名称方面做得很好,但也会产生很多误报(Information Processing 看起来很像基于这些规则的名称)。不过,这应该可以为您提供一个起点。

    【讨论】:

    • 上述解决方案非常有帮助......我想问你一些类似的问题......但在不同的背景下......问题是我的名单中有名字那是格式... ['john a, smith,william tell,jacob oram'] 现在我想更正名字 john a smith 是一个完整的名字,但由于原始数据的格式,它像这...现在使用上述方法,我使用 ab=re.search((r'[AZ][az]+ [AZ]\,[AZ][az]+'),zz[uy] 找到了所有此类名称) 其中 zz[uy] 是字符串的位置...
    • 现在我想要的是,我只想用正确的 john a 格式替换名称。 smith,william tell,jacob oram 在列表中...请帮忙
    • 基本上我想做的就是用“。”替换 john a, smith 中“a”之后的“,”。并将其放回字符串中
    • 你为什么不在一个新问题中问这个?做的时候把它链接在这里。这样其他人也可以加入并提供帮助。
    【解决方案2】:

    要删除制表符(以及字符串开头或结尾的其他空格):

    stripped = [s.strip() for t in mylist]
    

    老实说,如果您尝试提取姓名,那么这样的拆分行将无济于事——请注意一些姓名仍然与标题组合在一起。最好构建一个匹配名称的良好正则表达式,并在单独的行上使用 re.findall。

    【讨论】:

      【解决方案3】:

      要删除制表符和多余的空格,请使用 strip():

      >>> "\t foobar \t\t\t".strip()
      'foobar'
      

      【讨论】:

        【解决方案4】:

        也可能更容易找到一些已经完成这项工作的在线信息来源。例如,在 thisthis 这样的地方。

        【讨论】:

          【解决方案5】:
          1. 剥离所有字符串
          2. 识别肯定不是名称的字符串(很长的字符串,包含数字的字符串,以及列表中的后一个)
          3. 识别肯定是名称的字符串(列表开头的短字符串,以模式 $[AZ][az]{0,3}.?\s 开头的字符串(Dr., Miss, Mr, Prof,等)
          4. 找出最后不能用这些规则匹配的字符串,并尝试通过创建一个 certidude 系数来制作模糊规则来选择:靠近列表开头的字符串越短,得分越高最后有一个大尺寸的东西。添加类似的标准并设置最低分数。

          如果您需要高准确度,请使用名称数据库和贝叶斯过滤器的 loof。

          它不会完美:很难知道'name name name'和'word word word'之间的区别

          【讨论】:

            猜你喜欢
            • 2011-06-22
            • 2022-11-25
            • 2012-10-11
            • 2019-09-26
            • 2013-09-13
            • 1970-01-01
            • 2013-11-05
            • 2012-01-21
            相关资源
            最近更新 更多