python中的更改列表

python中的更改列表,python,list,itertools,Python,List,Itertools,如何制作这样的多个列表: ['The Lord of the Rings: The Fellowship of the Ring (2001)'] ['The Lord of the Rings: The Two Towers (2002)'] ['"5 Second Movies" (2007) {The Lord of the Rings and the Two Towers (#1.63)}'] ['The Lord of the Rings: The Fellowship of

如何制作这样的多个列表:

['The Lord of the Rings: The Fellowship of the Ring (2001)']

['The Lord of the Rings: The Two Towers (2002)']

['"5 Second Movies" (2007) {The Lord of the Rings and the Two Towers (#1.63)}']
 ['The Lord of the Rings: The Fellowship of the Ring (2001)',
'The Lord of the Rings: The Two Towers (2002)',
'"5 Second Movies" (2007) {The Lord of the Rings and the Two Towers (#1.63)}']
进入如下列表:

['The Lord of the Rings: The Fellowship of the Ring (2001)']

['The Lord of the Rings: The Two Towers (2002)']

['"5 Second Movies" (2007) {The Lord of the Rings and the Two Towers (#1.63)}']
 ['The Lord of the Rings: The Fellowship of the Ring (2001)',
'The Lord of the Rings: The Two Towers (2002)',
'"5 Second Movies" (2007) {The Lord of the Rings and the Two Towers (#1.63)}']
我试过这个:

x = open("ratings.list.txt","r")
movread = x.readlines()
x.close()



#s = raw_input('Search: ').lower()
for ns in movread:


    if 'the lord of the' in ns.lower():
        d = re.split('\s+',ns,4)
        Title = d[4].rstrip()
        Rating= d[3]



        lists = [Title]

        combined = [item for sublist in lists for item in sublist]
        print combined
但它给了我这个输出:

['T', 'h', 'e', ' ', 'L', 'o', 'r', 'd', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 'R', 'i', 'n', 'g', 's', ':', ' ', 'T', 'h', 'e', ' ', 'R', 'e', 't', 'u', 'r', 'n', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 'K', 'i', 'n', 'g', ' ', '(', '2', '0', '0', '3', ')']
['T', 'h', 'e', ' ', 'L', 'o', 'r', 'd', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 'R', 'i', 'n', 'g', 's', ':', ' ', 'T', 'h', 'e', ' ', 'F', 'e', 'l', 'l', 'o', 'w', 's', 'h', 'i', 'p', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 'R', 'i', 'n', 'g', ' ', '(', '2', '0', '0', '1', ')']
谢谢你们的帮助,伙计们:D

更新

文件如下所示(它是IMDB上所有电影的列表,因此大小非常大):

你想

[movie[0] for movie in movies]
示例脚本如下所示

import pprint    

movies = [
['The Lord of the Rings: The Fellowship of the Ring (2001)'],
['The Lord of the Rings: The Two Towers (2002)'],
['"5 Second Movies" (2007) {The Lord of the Rings and the Two Towers (#1.63)}'],
]

pprint.pprint([movie[0] for movie in movies], indent=4)
这个输出

[   'The Lord of the Rings: The Fellowship of the Ring (2001)',
    'The Lord of the Rings: The Two Towers (2002)',
    '"5 Second Movies" (2007) {The Lord of the Rings and the Two Towers (#1.63)}']

当您读取文件时,
电影
列表将由您填充

从文本文件中将它们作为行读取,可以执行以下操作:

看起来您希望能够搜索标题,因此在这种情况下:

search = raw_input("Enter title: ").lower()

with open("ratings.list.txt") as f:
    titles = [line for line in f if search in line.lower()]
该代码将返回整行代码。你怎么处理这条线取决于你自己

例如,要从行中提取标题+年份,可以执行以下操作:

for title in titles:
    print title.strip().split(' ', 6)
例如:

如果行是:

     0000000125  1196672   9.2  The Shawshank Redemption (1994) 
产出将是:

['0000000125', '', '1196672', '', '', '9.2', ' The Shawshank Redemption (1994)']

看起来你的实际问题要比附加列表的问题复杂得多。您可以为结果考虑数据库。

但是关于你原来的问题:

如果您只是想将列表折叠成字符串列表,那么有一个很好的技巧,使用
sum

>>> listoflists = [['abc'],['def'],['g'],['abc'],['abcdefg']]
>>> sum(listoflists,[])
['abc', 'def', 'g', 'abc', 'abcdefg']
您还可以使用列表理解,这在我的测试中实际上更快:

combined = [ x[0] for x in listoflists ]
但是,如果项目是列表元素,最快的方法是在创建时将它们添加到组合的
中,而不需要在末尾单独循环:

#Outside the loop
combined = []

# inside the loop
title = d[4].rstrip()
lists = [title]
combined += lists
但是,似乎没有理由让中间
列出
变量,而只是使用:

title = d[4].rstrip()
combined.append(title)

您可能想将您的
打印组合
移动到循环之外,这样它就可以在最后打印它。

只需
[sub[0]对于原始文件中的sub]
?我建议您读入文件,并通过换行而不是逐个字符拆分。我该怎么做?我不擅长python,只是刚开始@我的问题是,这是一个巨大的文件,比我给你看的三个列表都多!那么,这个文件中是否有不止一个电影标题?如果这个文件只包含电影标题,每行一个,那么上面打开文本文件的代码应该会解决一个棘手的问题,那就是它是一个巨大的文件,比我向您展示的三个列表都多!列表并不是通过将文件按行分隔,而是将每行添加到电影列表中。然后它们将用逗号分隔