简体   繁体   English

在python中将字符串列表拆分为子列表

[英]split string list into sublist in python

Looking for a way to split the following string list into sublist and print using a for loop. 寻找一种将以下字符串列表拆分为子列表并使用for循环进行打印的方法。

[[[u'Book1', None, u'Thriller', u'John C', u'07/12/2012'],
[u'Book2', u'1', u'Action', u'Tom B', u'07/12/2012'],
[u'Book3', None, u'Romance', u'Angie P', u'07/12/2012'],
[u'Book4', None, u'Comedy', u'Tracy N', u'07/12/2012'],
[u'Book5', None, u'Drama', u'Kumar P', u'07/12/2012'],
[u'Book6', None, u'Action&Drama', u'Ben J', u'07/12/2012']]]

Any suggestion please. 请提出任何建议。

Are you looking for this? 您在找这个吗?

def testString():
    input = [[
    [u'Book1', None, u'Thriller', u'John C', u'07/12/2012'],
    [u'Book2', u'1', u'Action', u'Tom B', u'07/12/2012'],
    [u'Book3', None, u'Romance', u'Angie P', u'07/12/2012'],
    [u'Book4', None, u'Comedy', u'Tracy N', u'07/12/2012'],
    [u'Book5', None, u'Drama', u'Kumar P', u'07/12/2012'],
    [u'Book6', None, u'Action&Drama', u'Ben J', u'07/12/2012']
    ]]
    for subarray in input[0]:
        print (subarray)

This is the output 这是输出

['Book1', None, 'Thriller', 'John C', '07/12/2012']
['Book2', '1', 'Action', 'Tom B', '07/12/2012']
['Book3', None, 'Romance', 'Angie P', '07/12/2012']
['Book4', None, 'Comedy', 'Tracy N', '07/12/2012']
['Book5', None, 'Drama', 'Kumar P', '07/12/2012']
['Book6', None, 'Action&Drama', 'Ben J', '07/12/2012']

Your question is a bit vague! 您的问题有点含糊! If I understood your question correctly, you can do it like this: 如果我正确理解了您的问题,则可以这样进行:

a = [[[u'Book1', None, u'Thriller', u'John C', u'07/12/2012'], [u'Book2', u'1', u'Action', u'Tom B', u'07/12/2012'], [u'Book3', None, u'Romance', u'Angie P', u'07/12/2012'], [u'Book4', None, u'Comedy', u'Tracy N', u'07/12/2012'], [u'Book5', None, u'Drama', u'Kumar P', u'07/12/2012'], [u'Book6', None, u'Action&Drama', u'Ben J', u'07/12/2012']]]
for v in a[0]:
    print(v)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM