简体   繁体   English

当一个词已经在元组中时,Python 打印一条消息

[英]Python printing a message when a word is already in a tuple

Ok so the problem is I can't figure out how to get the "word already exists" to appear when trying to add a already existing word.好的,问题是我不知道如何在尝试添加一个已经存在的词时让“这个词已经存在”出现。 It just skips the whole thing and just keeps putting the already existing word into the tuple list.它只是跳过整个事情,只是继续将已经存在的单词放入元组列表中。

But it should just print that "the word already exists" and just go back without adding any new words.但它应该只打印“这个词已经存在”,然后返回而不添加任何新词。

def instoppning2tup(tuplelista):
   word = raw_input("Type the word: ")
   #desc = raw_input("Type the description: ")
   if word in tuplelista:
      print "word already exists"

   else:
        desc = raw_input("Give descrption to the word: ")
        tuplelista.append( (word,desc) )

You're checking if the word is in tuplelista but you add a tuple that contains (word, desc) into the tuplelista .您正在检查word是否在tuplelista但是您将包含(word, desc)的元组添加到tuplelista So tuplelista looks like:所以tuplelista看起来像:

[(word1, desc1), (word2, desc2), (word3, desc3), ...]

You should modify the condition: if word in tuplelista: into a function call, something like:您应该修改条件: if word in tuplelista:进入函数调用,类似于:

if not exists(word, tuplelista) and implement a function that checks if there is a tuple in the tuplelista that has word as its first element. if not exists(word, tuplelista)和执行一个函数,检查是否存在在一个元组tuplelista具有word作为它的第一个元素。

One way to do it:一种方法:

def exists(word, tuplelist):
    return any([x for x in tuplelist if x[0] == word]) 

If you convert your tupelisa to dictionary it will be possible:如果您将 tupelisa 转换为字典,则有可能:

if word in dict(tupelisa):
   ...

it will be probably a good idea to work with dictionary instead of list of tuples and at the end, if you need list of tuples just convert dictionary to list of tuples使用字典而不是元组列表可能是一个好主意,最后,如果您需要元组列表,只需将字典转换为元组列表

list(yourdic.items())

You can use something like this:你可以使用这样的东西:

>>> from functools import reduce
>>> from operator import add
>>> tuplelista = [('word1', 'description1'), ('word2', 'description2')]
>>> flat_tuplelista = reduce(add, tuplelista)
>>> flat_tuplelista
...['word1', 'description1', 'word2', 'description2']

Another way其它的办法

>>> tuplelista = [('word1', 'description1'), ('word2', 'description2')]
>>> flat_tuplelista = sum(tuplelista, ())
>>> flat_tuplelista
...['word1', 'description1', 'word2', 'description2']

And you can simply check:你可以简单地检查:

>>> if word in tuplelista:
...    print "word already exists"
... else:
...     desc = raw_input("Give descrption to the word: ")
...     tuplelista.append( (word,desc) )

Btw, it seems to me, it will be better to store data in dictionary, where word is a key, and description is a value.顺便说一句,在我看来,将数据存储在字典中会更好,其中单词是键,描述是值。 And then, you will be able to simply check if the word is in dictionary:然后,您将能够简单地检查单词是否在字典中:

>>> words = {'word1': 'desription1', 'word2': 'description2'}
>>> if word in words:
...    print "word already exists"
... else:
...     desc = raw_input("Give descrption to the word: ")
...     words[word] = desc

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

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