列表理解输出无

发布于 2025-02-11 08:14:12 字数 442 浏览 2 评论 0原文

我是Python的新手,我想尝试使用列表理解,但是我得到的结果却没有。

print
wordlist = ['cat', 'dog', 'rabbit']
letterlist = []
letterlist = [letterlist.append(letter) for word in wordlist for letter in word if letter not in letterlist]
print letterlist

# output i get: [None, None, None, None, None, None, None, None, None]
# expected output: ['c', 'a', 't', 'd', 'o', 'g', 'r', 'b', 'i']

这是为什么?看来它可以以某种方式起作用,因为我得到了预期的结果数(9),但所有人都不是。

I'm new to python and I wanted to try to use list comprehension but outcome I get is None.

print
wordlist = ['cat', 'dog', 'rabbit']
letterlist = []
letterlist = [letterlist.append(letter) for word in wordlist for letter in word if letter not in letterlist]
print letterlist

# output i get: [None, None, None, None, None, None, None, None, None]
# expected output: ['c', 'a', 't', 'd', 'o', 'g', 'r', 'b', 'i']

Why is that? It seems that it works somehow because I get expected number of outcomes (9) but all of them are None.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陈独秀 2025-02-18 08:14:12

list.Append(element)不返回任何内容 - 它将元素附加到就地列表。

您的代码可以被重写为:

wordlist = ['cat', 'dog', 'rabbit']
letterlist = [letter for word in wordlist for letter in word]
letterlist = list(set(letterlist))
print letterlist

…如果您真的想使用列表理解,或者:

wordlist = ['cat', 'dog', 'rabbit']
letterset = set()
for word in wordlist:
    letterset.update(word)
print letterset

…可以说是更清楚的。这两个都假定顺序无关紧要。如果是这样,您可以使用OrderDict:

from collections import OrderedDict
letterlist = list(OrderedDict.fromkeys("".join(wordlist)).keys())
print letterlist

list.append(element) doesn’t return anything – it appends an element to the list in-place.

Your code could be rewritten as:

wordlist = ['cat', 'dog', 'rabbit']
letterlist = [letter for word in wordlist for letter in word]
letterlist = list(set(letterlist))
print letterlist

… if you really want to use a list comprehension, or:

wordlist = ['cat', 'dog', 'rabbit']
letterset = set()
for word in wordlist:
    letterset.update(word)
print letterset

… which is arguably clearer. Both of these assume order doesn’t matter. If it does, you could use OrderedDict:

from collections import OrderedDict
letterlist = list(OrderedDict.fromkeys("".join(wordlist)).keys())
print letterlist
凉薄对峙 2025-02-18 08:14:12

list.Append返回。您需要调整列表理解中的表达式以返回字母。

wordlist = ['cat', 'dog', 'rabbit']
letterset = set()
letterlist = [(letterset.add(letter), letter)[1]
              for word in wordlist
              for letter in word
              if letter not in letterset]
print letterlist

list.append returns None. You need to adjust the expression in the list comprehension to return letters.

wordlist = ['cat', 'dog', 'rabbit']
letterset = set()
letterlist = [(letterset.add(letter), letter)[1]
              for word in wordlist
              for letter in word
              if letter not in letterset]
print letterlist
沐歌 2025-02-18 08:14:12

如果订单没关系,请执行此操作:

 resultlist = list({i for word in wordlist for i in word})

If order doesn't matter, do this:

 resultlist = list({i for word in wordlist for i in word})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文