Python按多个元素和条件拆分列表

时间:2017-11-08 19:31:40

标签: python list split conditional-statements

我是python的新手,甚至在stackoverflow中引用了许多示例而陷入列表拆分。如果我想按以下条件拆分列表,我该怎么办?

任务1.一旦在“datalist”中找到“wordlist”中的项目,就像这样拆分列表。

Wordlist = ["Time", "date", "place",....]

output = ["A","B"]["Time","C","D","E"]["Date",.....]

任务2.找到特定项目后,列表将按照特定单词和包含的n个项目进行拆分,然后继续遍历dlist。例如

字,n =不。项目遵循

时间,1

日期,2

地点,1

...

输入:

datalist = ["A","B", "N", "K" , "R", "Time", "2230" , "C" , "Date" , '12/05', "E" , "F", "R", "F", "K" ,"Place", "XXXXXX", "H", "I" , "J" ]

wordlist = ["Time", "Date", "Place"]

n = [1,2,1]

输出:

newlist = [["A","B", "N", "K" , "R"] ,["Time", "2230"],[ "C"], [ "Date" , '12/05',"E"][ "F", "R", "F", "K" ], ["Place", "XXXXXX"], ["H", "I","J"] ]

这是我提到的示例解决任务-1部分而不是任务-2: Python spliting a list based on a delimiter word

3 个答案:

答案 0 :(得分:0)

任务1的单行解决方案:

[datalist[:datalist.index(wordlist[0])]] + [datalist[datalist.index(wordlist[i]):datalist.index(wordlist[i+1])] for i in range(len(wordlist)-1)] + [datalist[datalist.index(wordlist[-1]):]]

outputs

[['A', 'B', 'N', 'K', 'R'], ['Time', '2230'], ['C'], ['Date', '12/05', 'E'], ['F', 'R', 'F', 'K'], ['Place', 'XXXXXX'], ['H', 'I', 'J']]

任务2:

sol = []
i = 0
s = 0
while i < len(datalist):
    if datalist[i] in wordlist:
        cs = n[wordlist.index(datalist[i])]
        print(cs)
        sol += [datalist[s:i], datalist[i:i+cs+1]]
        i += cs
        s = i + 1
    i += 1

sol.append(datalist[s:])

outputs

[['A', 'B', 'N', 'K', 'R'], ['Time', '2230'], ['C'], ['Date', '12/05', 'E'], ['F', 'R', 'F', 'K'], ['Place', 'XXXXXX'], ['H', 'I', 'J']]

答案 1 :(得分:0)

 datalist = ['A', 'B', 'N', 'K', 'R', 'Time', '2230', 'C', 'Date', '12/05', 
'E', 'F', 'R', 'F', 'K', 'Place', 'XXXXXX', 'H', 'I', 'J']

得到的话:
    >>> [dl for dl in datalist if dl.isalpha() and len(dl) > 1]
    ['Time', 'Date', 'Place', 'XXXXXX']

获取字符与获取字词的唯一区别是长度应该等于1。

获取数字:
    >>> [dl for dl in datalist if dl.isnumeric()]
    ['2230']

获取日期:
    >>> [dl for dl in datalist if '/' in dl]
    ['12/05']
这个解决方案有点愚蠢。对于更精细的解决方案,我建议使用re模块。

然后,您可以将结果打包到列表中以获得所需的结果。

答案 2 :(得分:0)

与您链接的其他答案中的方法类似,我会指示您使用生成器来获得更通用的解决方案。

def split_list(wordlist, splitwords = {}):
    out = []
    worditer = iter(wordlist)
    for word in worditer:
        if word in splitwords: #potentially yield previous non-keyword list and build keyword list
            if out: #yield non-keyword list
                yield out
            out = [word] #start new list with keyword
            try:
                for _ in range(splitwords[word]): #add *n* more words after keyword
                    out.append(next(worditer))
            except StopIteration: #not enough items after keyword
                pass
            yield out #yield keyword list
            out = [] #reset accumulator
        else:
            out.append(word) #grow non-keyword list
    if out: #yield trailing non-keyword list
        yield out

datalist = ["A","B", "N", "K" , "R", "Time", "2230" , "C" , "Date" , '12/05', "E" , "F", "R", "F", "K" ,"Place", "XXXXXX", "H", "I" , "J" ]
splitwords = {"Time": 1, "Date": 2, "Place": 1}

newlist = list(split_list(datalist, splitwords))
print(newlist)