简体   繁体   English

Python 3.3:如何从每行的第一个单词中提取第一个字母?

[英]Python 3.3: How to extract the first Letters from the first Words on each line?

I need assistance for this problem I'm having. 我需要解决这个问题。 I'm trying to get my program to grab the first Letter from the first Word on every single line and print them in a single string. 我正在尝试让我的程序从每行的第一个单词中抓取第一个字母并将其打印在单个字符串中。

For example if I type the following words in a block of text: 例如,如果我在文本块中键入以下单词:

People like to eat pie for three reasons, it tastes delicious. The taste is unbelievable, next pie makes a
great dessert after dinner, finally pie is disgusting.

The result should be "Pg" this is a small example but you get the idea. 结果应该是“ Pg”,这是一个小例子,但您明白了。

I started on the code but I'm clueless on where to go. 我开始编写代码,但是对去哪里一无所知。

#Prompt the user to enter a block of text.
done = False
print("Enter as much text as you like. Type EOF on a separate line to finish.")
textInput = ""
while(done == False):
    nextInput= input()
    if nextInput== "EOF":
        break
    else:
        textInput += nextInput

#Prompt the user to select an option from the Text Analyzer Menu.
print("Welcome to the Text Analyzer Menu! Select an option by typing a number"
    "\n1. shortest word"
    "\n2. longest word"
    "\n3. most common word"
    "\n4. left-column secret message!"
    "\n5. fifth-words secret message!"
    "\n6. word count"
    "\n7. quit")

#Set option to 0.
option = 0

#Use the 'while' to keep looping until the user types in Option 7.
while option !=7:
    option = int(input())

#I have trouble here on this section of the code.
#If the user selects Option 4, extract the first letter of the first word
    #on each line and merge into s single string.
    elif option == 4:
        firstLetter = {}
        for i in textInput.split():
            if i < 1:
                print(firstLetter)

You can store the inputs as a list, then get the first character from each list: 您可以将输入存储为列表,然后从每个列表中获取第一个字符:

textInput = []
while(done == False):
    nextInput= input()
    if nextInput== "EOF":
       break
    else:
        textInput.append(nextInput)



...


print ''.join(l[0] for l in textInput)

I'd start by making a list of lines instead of a single string: 我将从列出行而不是单个字符串开始:

print("Enter as much text as you like. Type EOF on a separate line to finish.")

lines = []

while True:
    line = input()

    if line == "EOF":
        break
    else:
        lines.append(line)

Then, you can get the first letters with a loop: 然后,您可以循环获取第一个字母:

letters = []

for line in lines:
    first_letter = line[0]
    letters.append(first_letter)

print(''.join(letters))

Or more concisely: 或更简而言之:

print(''.join([line[0] for line in lines]))

This is very simple: 这很简单:

with open('path/to/file') as infile:
    firsts = []
    for line in infile:
        firsts.append(line.lstrip()[0])
print ''.join(firsts)

Of course, you could do the same thing with the following two-liner: 当然,您可以使用以下两种方法执行相同的操作:

with open('path/to/file') as infile:
    print ''.join(line.lstrip()[0] for line in infile)

Hope this helps 希望这可以帮助

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

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