这是作业:编写代码,使用存储在org中的字符串并创建一个首字母缩写词,该首字母缩写词被分配给变量acro。仅应使用每个单词的第一个字母,首字母缩写词中的每个字母应为大写字母,并且不应使用任何分隔首字母缩写词的字母。首字母缩略词中不应包含的单词存储在列表停用词中。例如,如果为org分配了字符串“ hello to world”,则结果首字母缩写应为“ HW”。
因此,我设法从句子中每个单词中获取了首字母,但其中包括停用词列表,如何从最终结果中删除那些首字母?
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"
acro = ''
acro = [i[0] for i in org.upper().split(' ')]
答案 0 :(得分:3)
您可以在列表理解中具有逻辑,因此您可以执行类似的操作
acro = [i[0] for i in org.upper().split(' ') if i.lower() not in stopwords]
答案 1 :(得分:1)
在列表理解中使用split
,并在末尾使用join
以形成字符串:
org = 'hello to world'
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
acro = ''
print(acro.join([x[0].upper() for x in org.split() if x.lower() not in stopwords]))
# HW
或者使用map
+ lambda
(出于娱乐目的 ):
acro.join(map(lambda x: x[0].upper() if x.lower() not in stopwords else '', org.split()))
答案 2 :(得分:0)
似乎您的停用词并非全部用小写字母表示。这样的事情会做:
org = "The organization for health, safety, and education"
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
acro = [i[0].upper() for i in org.split(' ') if i not in stopwords]
print(''.join(acro))
返回“ OHSE”。
答案 3 :(得分:0)
如果这是一项任务,并且您想了解会发生什么,那么分步执行可能会很好:
acro_temp = [i for i in org.split(' ')] # convert to array
acro_temp = [i for i in acro_temp if i not in stopwords] # remove stopwords
acro_temp = [i.upper() for i in acro_temp] # make the words uppercase
acro = [i[0] for i in acro_temp] # use only first letter
然后进行压缩,可以使用@TheStrangeQuark的建议。
答案 4 :(得分:0)
您快到了。您的停用词列表应包含不区分大小写的条目(即全部大写或全部小写)。您只需要在列表理解中添加一个if条件即可。
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"
stopwords = set( w.upper() for w in stopwords )
acro = [i[0] for i in org.upper().split(' ') if i not in stopwords]
答案 5 :(得分:0)
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"
org_split = org.split(' ')
acro = ''
for letter in org_split:
if letter.lower() not in stopwords:
letter = letter[0].upper()
acro += acro.join(letter)
print(acro)
这将使您获得“ OHSE”,其他人则得到['O','H','S','E']。两者在技术上都是正确的,因此这取决于您的需求。
答案 6 :(得分:0)
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"
acro = ""
for i in org.split(" "):
if i not in stopwords:
acro = acro + i[0].upper()
print(acro)
答案 7 :(得分:0)
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', 'The']
sent = "The water earth and air are vital"
acro = ""
sent_words = sent.split()
for word in sent_words:
if word not in stopwords:
acro = acro + word[:2]
if word != sent_words[-1]:
acro += ". "
acro = acro.upper()
print(acro)