如何在 Python 中从单词列表转为不同字母列表

发布于 2024-08-20 22:28:19 字数 706 浏览 10 评论 0原文

使用Python,我试图将一个单词句子转换为该句子中所有不同字母的平面列表。

这是我当前的代码:

words = 'She sells seashells by the seashore'

ltr = []

# Convert the string that is "words" to a list of its component words
word_list = [x.strip().lower() for x in words.split(' ')]

# Now convert the list of component words to a distinct list of
# all letters encountered.
for word in word_list:
    for c in word:
        if c not in ltr:
            ltr.append(c)

print ltr

此代码返回 ['s', 'h', 'e', 'l', 'a', 'b', 'y', 't', 'o', 'r '],这是正确的,但是这个答案是否有更Pythonic的方式,可能使用列表理解/set

当我尝试结合列表理解嵌套和过滤时,我得到的是列表列表而不是平面列表。

最终列表 (ltr) 中不同字母的顺序并不重要;重要的是它们是独一无二的。

Using Python, I'm trying to convert a sentence of words into a flat list of all distinct letters in that sentence.

Here's my current code:

words = 'She sells seashells by the seashore'

ltr = []

# Convert the string that is "words" to a list of its component words
word_list = [x.strip().lower() for x in words.split(' ')]

# Now convert the list of component words to a distinct list of
# all letters encountered.
for word in word_list:
    for c in word:
        if c not in ltr:
            ltr.append(c)

print ltr

This code returns ['s', 'h', 'e', 'l', 'a', 'b', 'y', 't', 'o', 'r'], which is correct, but is there a more Pythonic way to this answer, probably using list comprehensions/set?

When I try to combine list-comprehension nesting and filtering, I get lists of lists instead of a flat list.

The order of the distinct letters in the final list (ltr) is not important; what's crucial is that they be unique.

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

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

发布评论

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

评论(7

别低头,皇冠会掉 2024-08-27 22:28:19

集合提供了简单、高效的解决方案。

words = 'She sells seashells by the seashore'

unique_letters = set(words.lower())
unique_letters.discard(' ') # If there was a space, remove it.

Sets provide a simple, efficient solution.

words = 'She sells seashells by the seashore'

unique_letters = set(words.lower())
unique_letters.discard(' ') # If there was a space, remove it.
桃酥萝莉 2024-08-27 22:28:19

使 ltr 成为一个集合并稍微更改循环体:

ltr = set()

for word in word_list:
    for c in word:
       ltr.add(c)

或者使用列表理解:

ltr = set([c for word in word_list for c in word])

Make ltr a set and change your loop body a little:

ltr = set()

for word in word_list:
    for c in word:
       ltr.add(c)

Or using a list comprehension:

ltr = set([c for word in word_list for c in word])
无声无音无过去 2024-08-27 22:28:19
set([letter.lower() for letter in words if letter != ' '])

编辑:我刚刚尝试过,发现这也可以工作(也许这就是SilentGhost所指的):

set(letter.lower() for letter in words if letter != ' ')

如果你需要一个列表而不是一组,你可以

list(set(letter.lower() for letter in words if letter != ' '))
set([letter.lower() for letter in words if letter != ' '])

Edit: I just tried it and found this will also work (maybe this is what SilentGhost was referring to):

set(letter.lower() for letter in words if letter != ' ')

And if you need to have a list rather than a set, you can

list(set(letter.lower() for letter in words if letter != ' '))
只是在用心讲痛 2024-08-27 22:28:19
>>> set('She sells seashells by the seashore'.replace(' ', '').lower())
set(['a', 'b', 'e', 'h', 'l', 'o', 's', 'r', 't', 'y'])
>>> set(c.lower() for c in 'She sells seashells by the seashore' if not c.isspace())
set(['a', 'b', 'e', 'h', 'l', 'o', 's', 'r', 't', 'y'])
>>> from itertools import chain
>>> set(chain(*'She sells seashells by the seashore'.lower().split()))
set(['a', 'b', 'e', 'h', 'l', 'o', 's', 'r', 't', 'y'])
>>> set('She sells seashells by the seashore'.replace(' ', '').lower())
set(['a', 'b', 'e', 'h', 'l', 'o', 's', 'r', 't', 'y'])
>>> set(c.lower() for c in 'She sells seashells by the seashore' if not c.isspace())
set(['a', 'b', 'e', 'h', 'l', 'o', 's', 'r', 't', 'y'])
>>> from itertools import chain
>>> set(chain(*'She sells seashells by the seashore'.lower().split()))
set(['a', 'b', 'e', 'h', 'l', 'o', 's', 'r', 't', 'y'])
愛放△進行李 2024-08-27 22:28:19

以下是使用 py3k 进行的一些计时:

>>> import timeit
>>> def t():                    # mine (see history)
    a = {i.lower() for i in words}
    a.discard(' ')
    return a

>>> timeit.timeit(t)
7.993071812372081
>>> def b():                    # danben
    return set(letter.lower() for letter in words if letter != ' ')

>>> timeit.timeit(b)
9.982847967921138
>>> def c():                    # ephemient in comment
    return {i.lower() for i in words if i != ' '}

>>> timeit.timeit(c)
8.241267610375516
>>> def d():                    #Mike Graham
    a = set(words.lower())
    a.discard(' ')
    return a

>>> timeit.timeit(d)
2.7693045186082372

here are some timings made with py3k:

>>> import timeit
>>> def t():                    # mine (see history)
    a = {i.lower() for i in words}
    a.discard(' ')
    return a

>>> timeit.timeit(t)
7.993071812372081
>>> def b():                    # danben
    return set(letter.lower() for letter in words if letter != ' ')

>>> timeit.timeit(b)
9.982847967921138
>>> def c():                    # ephemient in comment
    return {i.lower() for i in words if i != ' '}

>>> timeit.timeit(c)
8.241267610375516
>>> def d():                    #Mike Graham
    a = set(words.lower())
    a.discard(' ')
    return a

>>> timeit.timeit(d)
2.7693045186082372
栀子花开つ 2024-08-27 22:28:19
set(l for w in word_list for l in w)
set(l for w in word_list for l in w)
南汐寒笙箫 2024-08-27 22:28:19
words = 'She sells seashells by the seashore'

ltr = list(set(list(words.lower())))
ltr.remove(' ')
print ltr
words = 'She sells seashells by the seashore'

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