Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要在列表中拆分字符串_Python - Fatal编程技术网

Python 需要在列表中拆分字符串

Python 需要在列表中拆分字符串,python,Python,问我:通过过滤下半部分列表,创建并打印一个单词列表,其中单词的前半部分与单词的后半部分匹配。例如“邦邦”、“弗劳夫鲁”、“嘎嘎”和“杂音” 到目前为止,我所拥有的: [word for word in lowers if list.(word)(1:len(word)/2:)==list.(word)(len(word)/2::)] 我不知道如何使单词列表,所以我只能使用这个过滤器的某些字符。我知道这行不通,但到目前为止,这是我的逻辑 试试这个: [word[len(word)//2://=w

问我:通过过滤下半部分列表,创建并打印一个单词列表,其中单词的前半部分与单词的后半部分匹配。例如“邦邦”、“弗劳夫鲁”、“嘎嘎”和“杂音”

到目前为止,我所拥有的:

[word for word in lowers if list.(word)(1:len(word)/2:)==list.(word)(len(word)/2::)]
我不知道如何使单词列表,所以我只能使用这个过滤器的某些字符。我知道这行不通,但到目前为止,这是我的逻辑

试试这个:


[word[len(word)//2://=word[:len(word)//2]]中的逐字切分
逻辑错误:从
列表中的索引1而不是0进行切分。(word)(1:len(word)/2:)

语法错误:
list.(word)
语法不正确,列表切片使用[]not()。只需使用
word[start:stop]
即可将其分解

使用:

[word[:len(word)//2]==word[len(word)//2:][code>


编辑:感谢Ignacio Vazquez Abrams的评论-使用整数除法(//运算符)实现Python 3兼容性

你确定你的意思不是列表理解吗?抱歉,这就是我的意思。要扩展这一点,请将
/
替换为
/
,这在2和3中都适用。