【发布时间】:2020-01-13 22:56:20
【问题描述】:
我想做什么?
我想创建一个 python 程序,它接受一个文本文件,将文本变成这样的字符串列表
@987654321 @ (1)
,将每个单词的字母数放入不同的列表中,如下所示[3, 7, 7, 7, 3, 8, 2, 2](2)
并检查如果每个字符串元素的数量大于 3 (>3),则删除其第一个字母并将其添加到带有 'xy' 字符串的单词的末尾。最终列表的结果应该是:['Man', 'equestrxy', 'daptedaxy', 'piritssxy', 'set', 'ressed.pxy', 'Up', 'to'](3)
我已经做了什么?我做了 (1) 和 (2) 部分代码,我目前正在尝试 (3) 。
我的 cmets 代码:
text = open("RandomTextFile.txt").read().split() #this is the the part (1)
#function that creates the second part (2)
def map_(A):
return list(map(len, A))
words = map_(text) #list that contains the example list of (2)
#This is the part (3) and I try to achieve it by creating a loop
for i in range(y):
if words[i]>3:
text[i] = [x + string for x in text]
谁能建议我可以做些什么来实现第 (3) 部分?提前致谢!
【问题讨论】:
标签: python arrays string list split