简体   繁体   English

使用列表理解和集合

[英]Using list comprehension and sets

Create and print a list of words for which both the following criteria are all met: 创建并打印满足以下条件的单词列表:

  • the word is at least 8 characters long; 这个词至少有8个字符;
  • the word formed from the odd-numbered letter is in the set of lower-case words; 由奇数字组成的单词在小写单词集中; and
  • the word formed from the even-numbered letters is in the set of lower-case words. 由偶数字母组成的单词在小写单词集合中。

For example, the word "ballooned" should be included in your list because the word formed from the odd-numbered letters, "blond", and the word formed from the even-numbered letters, "aloe", are both in the set of lower-case words. 例如,单词“ballooned”应该包含在你的列表中,因为由奇数字母“blond”形成的单词和由偶数字母“aloe”形成的单词都在小写单词。 Similarly, "triennially" splits into "tinily" and "renal", both of which are in the word list. 类似地,“三年”分裂为“轻微”和“肾脏”,两者都在单词列表中。

My teacher told us we should use a set: s=set(lowers) because this would be faster. 我的老师告诉我们,我们应该使用一套: s=set(lowers)因为这会更快。

what i have so far: 到目前为止我有什么:

s=set(lowers)
[word for word in lowers if len(word)>=8
                        and list(word)(::2) in s
                        and list(word)(::-2) in s]

I do not think I am using the set right. 我认为我没有使用正确的设置。 can someone help me get this to work 有人可以帮助我让这个工作

The problem is that you cast word to a list (unnecessary), your slices are not in brackets (you used parenthesis), and your second slice uses the wrong indices (should be 1::2 , not ::-2 ). 问题是你将word转换为列表(不必要),你的切片不在括号中(你使用括号),你的第二个切片使用错误的索引(应该是1::2 ,而不是::-2 )。

Here are the slices done correctly: 以下是正确完成的切片:

>>> word = "ballooned"
>>> word[::2]
'blond'
>>> word[1::2]
'aloe'

Note that s is an odd name for a collection of lowercase words. 请注意, s是小写单词集合的奇数名称。 A better name would be words . 一个更好的名字是words

Your use of set is correct. 你对set使用是正确的。 The reason your teacher wants you to use a set is it is much faster to test membership of a set than it is for a list. 你的老师希望你使用一组的原因是它的速度快得多一组比它是一个列表的成员测试。

Putting it together: 把它放在一起:

words = set(lowers)
[word for word in words if len(word) >= 8
                        and word[::2] in words
                        and word[1::2] in words]

Here is a quick example of how to structure your condition check inside of the list comprehension: 以下是如何在列表理解中构建条件检查的快速示例:

>>> word = 'ballooned'
>>> lowers = ['blond', 'aloe']
>>> s = set(lowers)
>>> len(word) >= 8 and word[::2] in s and word[1::2] in s
True

edit: Just realized that lowers contains both the valid words and the "search" words like 'ballooned' and 'triennially', in any case you should be able to use the above condition inside of your list comprehension to get the correct result. 编辑:刚才意识到lowers包含有效单词和“搜索”单词,如'ballooned'和'triennially',无论如何你应该能够在列表理解中使用上述条件来获得正确的结果。

list(word)(::2)

First, the syntax to access index ranges is using squared parentheses, also, you don't need to cast word to a list first, you can directly do that on the string: 首先,访问索引范围的语法是使用平方括号,同样,您不需要先将word转换为列表,您可以直接在字符串上执行此操作:

>>> 'ballooned'[::2]
'blond'

Also, [::-2] won't give you the uneven word, but a reversed version of the other one. 另外, [::-2]不会给你不均匀的单词,而是另一个单词的反转版本。 You need to use [1::2] (ie skip the first, and then every second character): 你需要使用[1::2] (即跳过第一个,然后是第二个字符):

>>> 'ballooned'[::-2]
'dnolb'
>>> 'ballooned'[1::2]
'aloe'

In general it is always a good idea to test certain parts separately to see if they really do what you think they do. 一般来说,分别测试某些部件以确定它们是否真的按照您的想法行事总是一个好主意。

this should do it: 这应该这样做:

s=set(lowers)
[word for word in lowers if len(word)>=8 and word[::2] in s and word[1::2] in s]

or using all() : 或使用all()

In [166]: [word for word in lowers if all((len(word)>=8,
                                               word[::2] in s,
                                               word[1::2] in s))]

use [::] not (::) and there's no need of list() here, plus to get the word formed by letters placed at odd position use [1::2] . 使用[::] not (::)并且这里不需要list() ,加上用于放置在奇数位置的字母形成的单词使用[1::2]

In [151]: "ballooned"[::2]
Out[151]: 'blond'

In [152]: "ballooned"[1::2]
Out[152]: 'aloe'

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

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