【问题标题】:python 3.5.0 ::: Compare values inside lists with each otherspython 3.5.0 ::: 将列表中的值相互比较
【发布时间】:2015-11-15 21:33:49
【问题描述】:

我想比较我放在一个变量中的 vogals,看看它们是否不同。我只想输出该变量中存在的 vogal,但我不希望它多次出现相同的 vogal。

我的代码:

lista = []
for vogal2 in palavra:
    if vogal2 in 'aeiou':
        lista.append(vogal2)

x ='0'
for val in lista:
    y = lista.index(val)
    if val == x:
        lista[y] = []
    x = val
print (lista)

输出:

>>> Enter a word: inconveniente
['i', 'o', [], 'i', 'e', 'e']

我想要这样的输出:

>>> Enter a word: inconveniente
['i', 'o', 'e']

你能帮帮我吗?我正在使用 python 3.5.0

【问题讨论】:

    标签: list python-3.x indexing


    【解决方案1】:

    这是做同样事情的不同代码:

    word = input("Enter a word: ")
    
    output = []
    
    for letter in word:
        if letter in "aeiou" and not letter in output:
            output.append(letter)
    
    print(output)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      相关资源
      最近更新 更多