简体   繁体   English

打印出列表中每个项目的第一个字母

[英]print out first letter of each item in a list

I am trying to create a program that asks for a word input until '' is entered.我正在尝试创建一个程序,在输入 '' 之前要求输入一个单词。 Then the program will print out all of the words joined in a sentence.然后程序将打印出一个句子中连接的所有单词。 Then take the first letter of each word to make an acrostic.然后取每个单词的第一个字母来制作一个离合词。 I am using python.我正在使用蟒蛇。 Example shown below.示例如下所示。 Thank you in advance.先感谢您。 This is due really soon.这很快就要到期了。 :) :)

  • What I have coded:我编码的内容:
sentence = []

acrostic = []

word = -1

while word:

     sentence.append(word)

      acrostic.append(sentence[0].upper())
print(sentence)
print("-- {}".format(acrostic))
  • What I want the code to do:我希望代码做什么:
Word: A

Word: cross

Word: tick

Word: is

Word: very

Word: evil

Word: 

A cross tick is very evil

-- ACTIVE

For input:对于输入:

  • in a loop, ask the user a word, if it nothing just stop在一个循环中,问用户一个词,如果没有就停止
  • if it's a word, save it in sentence and it's first letter in acrostic ( word[0] not sentence[0] )如果是单词,则将其保存在sentence ,并且是acrostic的第一个字母( word[0]不是sentence[0]

For output:对于输出:

  • for the sentence join the words with a space : " ".join(sentence)对于句子用空格连接单词: " ".join(sentence)
  • for the acrostic, join the letters with nothing : "".join(acrostic)对于离合词,将字母加入空字符: "".join(acrostic) .join "".join(acrostic)
sentence = []
acrostic = []
while True:
    word = input('Please enter a word, or enter to stop : ')
    if not word:
        break
    sentence.append(word)
    acrostic.append(word[0].upper())

print(" ".join(sentence))
print("-- {}".format("".join(acrostic)))

Gives

Please enter a word, or " to stop : A
Please enter a word, or " to stop : cross
Please enter a word, or " to stop : tick
Please enter a word, or " to stop : is
Please enter a word, or " to stop : very
Please enter a word, or " to stop : evil
Please enter a word, or " to stop : 
A cross tick is very evil
-- ACTIVE

python 3.8 or later python 3.8 或更高版本

sentence = []
acrostic = []
while user_input := input('word: '):
    sentence.append(user_input)
    acrostic.append(user_input[0].upper())

print(' '.join(sentence))
print(f"-- {''.join(acrostic)}")

output:输出:

word: A
word: cross
word: tick
word: is
word: very
word: evil
word: 
A cross tick is very evil
-- ACTIVE

python 3.6 and 3.7蟒蛇 3.6 和 3.7

sentence = []
acrostic = []
while True:
    user_input = input('word: ')
    if not user_input:
        break
    sentence.append(user_input)
    acrostic.append(user_input[0].upper())

print(' '.join(sentence))
print(f"-- {''.join(acrostic)}")

python 3.5 or earlier python 3.5 或更早版本

sentence = []
acrostic = []
while True:
    user_input = input('word: ')
    if not user_input:
        break
    sentence.append(user_input)
    acrostic.append(user_input[0].upper())

print(' '.join(sentence))
print('-- {}'.format(''.join(acrostic)))

Maybe you're looking for something like this:也许你正在寻找这样的东西:

sentence = []
acrostic = []
word = -1

while word != "":
    word = input("Word: ")

    if word:
        sentence.append(word)
        acrostic.append(word[0].upper())

print(" ".join(sentence))
print("-- {}".format("".join(acrostic)))

Goal: To extract first letter of each word untill '' is entered.目标:提取每个单词的第一个字母,直到输入 ''。

acrostic = ""
sentence = ""
while(True):
    word = input("enter a word= ")
    if(word!=''):
        acrostic+=word[0].upper()
        sentence+=word+" " 
    else:
        break
print(sentence)
print(acrostic)

With your sample input;使用您的样本输入;

enter a word= A
enter a word= cross
enter a word= tick
enter a word= is
enter a word= very
enter a word= evil
enter a word= 

output输出

A cross tick is very evil
ACTIVE

While everybody has you covered for basic loops, this is a nice example to use the iter(callable, sentinel) pattern:虽然每个人都了解基本循环,但这是使用iter(callable, sentinel)模式的一个很好的例子:

def initial():
    return input('Word: ')[:1]

print('-- ' + ''.join(iter(initial, '')))

Will produce:将产生:

Word: A
Word: cross
Word: tick
Word: is
Word: very
Word: evil
Word: 
-- Active

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

相关问题 如何打印列表中每个元素的第一个字母 - how to print first letter of each element in list 打印出以字母表中每个字母开头的文件中第一次出现的单词 - Print out the the first occurrence of a word in a file beginning with each letter in the Alphabet Scrapy 只返回按字母顺序排列的每个字母的第一项 - Scrapy returns only first item of each letter in alphabetical list 如何打印出每个列表的第一个字符,然后打印下一个字符 - How to print out the first character of each list, then print the next character Python 使用单个打印命令打印每个嵌套列表的第一项 - Python printing the first item of each nested list with a single print command 为什么我只打印列表中每个项目的第一个字母? - Why do I only print the first letters of each item in the list? 我需要用大写打印列表中每个项目的前三个字母 - I need to print the first three letters of each item in the list in capitals 如何在一行中的列表中打印每个字符串的首字母? - How do I print the first letter of each string in a list in one line? 打印列表中的第一项 - print the first item in list of lists 循环打印两个列表,以获得两列在每个列表的每个元素的第一个字母之间具有固定(自定义集)空间 - Loop print through two lists to get two columns with fixed(custom set) space between the first letter of each element of each list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM