python - 如何合并列表的多个元素?
问题描述
在这种情况下,我有两个词(代码和问题),在它们中的每个元音之后,我想放一个特定的符号(在我的例子中,我决定使用“#”)。我设法制作了一个列表,其中在某个单词中的某个元音之后有一个符号(例如 co#de) 现在剩下的就是,我想将这些单词合并在一起。我什至决定在这里采取正确的方法吗?
我有一个包含 6 个元素的列表:
# there is "#" after every vowel in a word
lst = ["code#", "que#stion", "questi#on", "co#de", "questio#n", "qu#estion"]
我想将这些元素合并在一起,这样我就可以得到一个只有两个元素的新列表。
# the two words stay the same, but there are now multiple "#" in every word
new_lst = ["co#de#", "qu#e#sti#o#n"]
这是在 python 中甚至可以做的事情吗?
解决方案
可以从一个新的联合国开始吗mark
:)list
>>> poundit = lambda x: ''.join('{}#'.format(y) if y.lower() in ['a', 'e', 'i', 'o', 'u'] else y for y in x)
>>> lst
['code#', 'que#stion', 'questi#on', 'co#de', 'questio#n', 'qu#estion']
>>> set(poundit(x) for x in (y.replace('#', '') for y in lst))
set(['qu#e#sti#o#n', 'co#de#'])
推荐阅读
- haskell - 哈斯克尔。变量不在范围内:howManyTimes :: Integer -> [Integer] -> t
- python - Django 日期时间格式不同于 DRF 序列化程序日期时间格式
- c++ - 如何在 C++ 中获取有关服务的启动类型的信息?
- r - R Plotly 将颜色更改为 add_bars 部分中的条形
- python - 如何在 Django 中使用 UserCreationForm 将 DateField 实现到自定义用户模型的注册表单中?
- c# - 结合代表顺序而不是同时运行?
- minizinc - Minizinc:车间问题,尽量减少花费太长时间
- wordpress - 如何在页眉中发布 WordLift 的 JSON-LD
- c# - 当 WCF 服务托管在 Windows 服务中时读取 TNSNAMES.ORA
- sql-server - SQL Server 2008R2:SET ANSI_NULLS OFF 不影响与空值的合并匹配