如何检查数组中每个项目的第一个字母?

时间:2017-06-22 12:49:39

标签: python arrays string python-3.x

我正在建立一个猪拉丁语翻译,我无法弄清楚如何识别输入单词的第一个字母。我已将输入转换为数组,每个项目都是一个新单词,但如何选择每个项目的每个首字母以确定它是否为辅音/元音/等?

3 个答案:

答案 0 :(得分:4)

a = ['This', 'is', 'a', 'sentence']
for word in a:
    print(word[0])

输出:
Ť 一世 一个 小号

答案 1 :(得分:1)

words = ['apple', 'bike', 'cow']

使用列表理解,即从另一个的内容构建列表:

firsts = [w[0] for w in words]
firsts

输出

['a','b','c']

答案 2 :(得分:0)

使用list cmprh检查单词是否为空

a = ['This', 'is', '', 'sentence']
[w[0] for w in a if w]

输出:

['T', 'i', 's']