有人可以解释这是怎样的措辞 [英] how this is wording can some one explain

查看:61
本文介绍了有人可以解释这是怎样的措辞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一些代码用于生成单词列表,但我不知道这段代码是如何工作的。有人可以解释这个代码splist



here is some code for it generating a list of words but i did not know how this code will work. can someone explain this code splist

splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
  deletes    = [a + b[1:] for a, b in splits if b]

推荐答案

除非 word 已经是声明的列表,否则它将无效。尝试:

As it stands it will do nothing unless word is already a declared list. Try:
# Create a simple list of words
#
word = [ "one","two","three"]

# Create a new list of pairs of elements of word.
# Each pair will be the first i entries and the remaining entries, as i
# goes from 0 to the number of words
#
splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
splits

[([], ['one', 'two', 'three']), (['one'], ['two', 'three']), (['one', 'two'], ['three']), (['one', 'two', 'three'], [])]

# Create a list from each entry combining the first item
# with the last 1 to n of the second item, in each pair,
# ignoring any where the second item is null.
#
deletes    = [a + b[1:] for a, b in splits if b]
deletes

[['two', 'three'], ['one', 'three'], ['one', 'two']]





看看https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions [ ^ ]有关列表推导的更多信息。



Take a look at https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions[^] for more information on list comprehensions.


这篇关于有人可以解释这是怎样的措辞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆