简体   繁体   中英

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.

[[[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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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