【问题标题】:Creating acronyms in Python在 Python 中创建首字母缩略词
【发布时间】:2010-12-04 18:49:56
【问题描述】:

在 Python 中,如何为给定字符串创建首字母缩写词?

喜欢,输入字符串:

'First Second Third'

输出:

'FST'

我正在尝试类似的东西:

>>> for e in x:
        print e[0]

但它不起作用......关于如何做到这一点的任何建议?我确信有一种正确的方法可以做到这一点,但我似乎无法弄清楚。我必须使用re 吗?

【问题讨论】:

    标签: python


    【解决方案1】:

    如果您想以语法正确的方式做事(无论语言环境如何),请使用title(),然后使用filter()

    acronym = filter(str.isupper, my_string.title())
    

    title() 非常棒;它使字符串带有标题并且根据语言环境是正确的。

    【讨论】:

      【解决方案2】:

      试试

      print "".join(e[0] for e in x.split())
      

      您的循环实际上循环了字符串x 中的所有字符。如果你想循环单词,你可以使用x.split()

      【讨论】:

      • 这里的“拆分”是原始文件中缺少的相关位。
      • 相差两秒,为你+1!
      • 谢谢,我确实想过使用它,但不知何故我坚持自己的解决方案。
      【解决方案3】:

      如果您只想使用大写字母

      >>>line = ' What AboutMe '
      >>>filter(str.isupper, line)
      'WAM'
      

      那些可能不是大写字母的词怎么办?

      >>>line = ' What is Up '
      >>>''.join(w[0].upper() for w in line.split())
      'WIU'
      

      只有大写字母怎么办?

      >>>line = ' GNU is Not Unix '
      >>>''.join(w[0] for w in line.split() if w[0].isupper())
      'GNU'
      

      【讨论】:

      • ''.join(w[0].upper() for w in line.split()) 应该是 ''.join(w[0] for w in line.split()) .upper()
      【解决方案4】:

      没有re

      >>> names = 'Vincent Vega Jules Winnfield'
      >>> ''.join(x[0] for x in names.split())
      'VVJW'
      

      【讨论】:

        【解决方案5】:

        现在来点不一样的……

        words = "There ain't no such thing as a free lunch."
        acronym = ''.join(word[0] for word in words.upper().split())
        print acronym
        # TANSTAAFL
        

        TANSTAAFL 是一个相当有名的人,顺便说一句)。

        【讨论】:

          【解决方案6】:
          s = 'First Second Third'
          x = s.split(' ')
          for e in x:
              print e[0]
          

          应该可以解决问题。

          【讨论】:

            【解决方案7】:

            你也可以使用

            re.split('\W')

            在非单词字符上拆分行/文本。这可能会更健壮一些。

            【讨论】:

              【解决方案8】:

              下面是如何用正则表达式做首字母缩略词,保持数字不变:

              import re
              words = "internet explorer 10"
              print re.sub(r"([a-zA-Z])[a-z,A-Z]+\s*",r"\1",words).upper()
              

              IE10

              【讨论】:

                【解决方案9】:

                这个任务有一个基于 python 的工具,它建议多个首字母缩略词,可以通过安装

                pip install acronym
                

                源代码仓库在这里:https://github.com/bacook17/acronym

                【讨论】:

                  【解决方案10】:

                  这是我的建议,因此我们可以删除诸如 to、and 、 of 之类的词以及诸如','之类的符号:

                  stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
                  Phrase=input("please enter the phrase you need its acronym: ")
                  acro=""
                  for i in range(len(stopwords)):
                      Phrase=Phrase.replace(stopwords[i]+' ',"")
                      Phrase=Phrase.replace(',',' ')
                  Phrase=Phrase.upper()
                  Words=Phrase.split( )
                  for word in Words:
                      acro = acro + word[0]
                  print(acro)
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2022-11-30
                    • 1970-01-01
                    • 2010-11-13
                    • 1970-01-01
                    • 2018-07-29
                    相关资源
                    最近更新 更多