繁体   English   中英

如何去除大写字母,然后再将它们放回原处?

[英]How to strip capital letters and then put them back in again in the same place?

我需要输入包含大写字母和小写字母的句子,并输出每个单词的位置。 但是,如果一个单词用大写字母输入,而同一单词仅以小写字母输入,则将其计为一个单独的单词。 例如。 如果输入了书本和书本,这些将被算作2个单独的单词。 我尝试使用.upper / .lower函数,但是这样做后没有办法将大写字母放回去吗? 我需要删除大写字母,然后再放回去。

    def analyse_sentence():
       sentence=input("Please enter your sentence")
       sentence=sentence.split
       compression(sentence)

    def compression(sentence):
        positions=[]
        Unique_words=[]
    for i in sentence:
      if i not in unique:
         unique.append(i)
         positions.append(unique_words.index(i)+1)

在堆栈溢出中发布时,应提供代码示例。 您拥有什么数据,代码是什么,代码做什么,打算获得什么。 为您提供帮助要容易得多。 无论如何,这是我为您提供的尝试:

text = 'blabla word1 word2 WORD word book BOOK bLaBlA'
unique_words =  list(set(text.lower().split(' ')))
print('text : %s' % text)
print('unique words : %s' % ','.join(unique_words))

编辑:

def analyse_sentence():
   sentence = input("Please enter your sentence")
   unique_words =  list(set(sentence.lower().split(' ')))
   print('sentence : %s' % sentence)
   print('unique words : %s' % ','.join(unique_words))
   return unique_words

暂无
暂无

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

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