简体   繁体   English

如何将列表拆分为多个列表?

[英]How can I split a list into multiple lists?

I am trying to convert [CS-PP-GE-RI-ET] which is the first element in a list to:我正在尝试将列表中的第一个元素[CS-PP-GE-RI-ET]转换为:

[CS]
[CS-PP]
[CS-PP-GE]
[CS-PP-GE-RI]
[CS-PP-GE-RI-ET]

Here is what I have got so far:这是我到目前为止所得到的:

data = ['CS-PP-GE-RI-ET',
        'CS-PP-ET-GE',
        'CS-PP-ET-GE-BD',
        'CS-PP-ET-GE',
        'CS-PP-ET-CD-PI-GE']

word0 = []
word1 = []
for i in range(len(data)):
    # print(i)  # 0 1 2 3 4 ... however many elements are in data
    split = data[i].split('-')  # prints CS-PP-GE-RI-ET ...
    # print(split)  # this prints ['CS', 'PP', 'GE', 'RI', 'ET'] for each element
    for j in range(len(split)-1):  # you have to use range(len()-1 to iterate all the way to the end of the index count
        # print(split[j])  # this prints each element CS PP GE RI ET CS PP ...
        # word1.append(split[j])
        temp0 = split[j]
        word0.append(temp0)
        temp1 = split[j + 1]
        temp3 = temp0 + '-' + temp1
        word0.append((temp3))

    print(word0)

This is what I get though:这就是我得到的:

['CS', 'CS-PP', 'PP', 'PP-GE', 'GE', 'GE-RI', 'RI', 'RI-ET']
['CS', 'CS-PP', 'PP', 'PP-GE', 'GE', 'GE-RI', 'RI', 'RI-ET', 'CS', 'CS-PP', 'PP', 'PP-ET', 'ET', 'ET-GE']
...

I know I am missing a basic understanding of str and append() but can't seem to figure it out.我知道我缺少对strappend()的基本了解,但似乎无法弄清楚。

The final result should be:最终结果应该是:

[CS]
[CS-PP]
[CS-PP-GE]
[CS-PP-GE-RI]
[CS-PP-GE-RI-ET]
[CS]
[CS-PP]
[CS-PP-ET]
[CS-PP-ET-GE]
[CS]
...

Thanks for the help.谢谢您的帮助。

We can use itertools.accumulate here我们可以在这里使用itertools.accumulate

from itertools import accumulate

l = 'CS-PP-GE-RI-ET'.split('-')
print(*accumulate(l, lambda x, y: '-'.join([x, y])), sep='\n')

CS
CS-PP
CS-PP-GE
CS-PP-GE-RI
CS-PP-GE-RI-ET

For a general solution you can do:对于一般解决方案,您可以执行以下操作:

data = ['CS-PP-GE-RI-ET',
        'CS-PP-ET-GE',
        'CS-PP-ET-GE-BD',
        'CS-PP-ET-GE',
        'CS-PP-ET-CD-PI-GE']

def acc(s):
    l = s.split('-')
    func = lambda x, y: '-'.join([x, y])
    return '\n'.join(accumulate(l, func))

print(*map(acc, data), sep='\n\n')

CS
CS-PP
CS-PP-GE
CS-PP-GE-RI
CS-PP-GE-RI-ET

CS
CS-PP
CS-PP-ET
CS-PP-ET-GE

CS
CS-PP
CS-PP-ET
CS-PP-ET-GE
CS-PP-ET-GE-BD

CS
CS-PP
CS-PP-ET
CS-PP-ET-GE

CS
CS-PP
CS-PP-ET
CS-PP-ET-CD
CS-PP-ET-CD-PI
CS-PP-ET-CD-PI-GE

This should write all your items into a list containing list, since your lists will need a place holder.这应该将您的所有项目写入包含列表的列表中,因为您的列表将需要一个占位符。

wrapper_list = list()
my_list = ['CS-PP-GE-RI-ET']
for str_item in my_list:
    temp_string = ""
    combined_string_list = list(str_item.split('-'))
    for index in range(len(str_item)):
         wrapper_list.append(["-".join(combined_string_list[0:index+1])])
print(wrapper_list)  

You can also do it like this:你也可以这样做:

for word in data:
    for i in range(len(word)):
        if word[i] == '-':
            print([ word[:i] ])
    print([ word ])

Or with list comprehension:或者使用列表理解:

print( '\n'.join( str([ word[:i] ]) 
                  for word in data
                  for i in range(len(word))
                  if word[i] == '-' or i == len(word) - 1 ) )

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

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