簡體   English   中英

如何根據項目是否在另一個列表中將項目中的內容按項目進行分組?

[英]How to group contents in a list from item to item based on whether those items are in another list?

我有以下清單:

x = ['0001', 'Random message XYX', 'Random second message IAI', '0002', 'Random message IAM', 'Random second message OMA', 'Random third message OMA', '0003', 'Random message XAK', 'Random second message YAB', '0004', ' Random message INA']

我還有另一個清單

y = ['0001', '0002', '0003', '0004']

我想根據組y對列表x進行分組,以便輸出為:

x = [['0001', 'Random message XYX', 'Random second message IAI'], ['0002', 'Random message IAM', 'Random second message OMA', 'Random third message OMA'], ['0003', 'Random message XAK', 'Random second message YAB'], ['0004', ' Random message INA']]

我努力了:

x = ['0001', 'Random message XYX', 'Random second message IAI', '0002', 'Random message IAM', 'Random second message OMA', 'Random third message OMA', '0003', 'Random message XAK', 'Random second message YAB', '0004', ' Random message INA']

y = ['0001', '0002','0003', '0004']

grouped_list = []
for entry in x:
    if entry in y:
        new_list = []
        new_list.append(entry)
        for i in range(x.index(entry)+1, len(x)):
            if(x[i][0] not in y):
                new_list.append(x[i])
            else:
                break
        grouped_list.append(list(new_list))
print (grouped_list)

但是,這只是打印[]

有人可以告訴我我要打印輸出的內容該怎么做嗎?

編輯:

我已使用適用於此示例的y.luis答案進行了一些更改,但是在使用實際數據時發現了一個問題。 我在兩個列表中都有重復的條目,這導致它覆蓋x列表中的數據,而不僅僅是將其分組。 如果運行此代碼,則x列表的最后一部分將被覆蓋:

x = ['0001', 'Random message XYX', 'Random second message IAI', '0002', 'Random message IAM', 'Random second message OMA', 'Random third message OMA', '0003', 'Random message XAK', 'Random second message YAB', '0004', ' Random message INA', '0001', 'Random message ryryry', 'Random second message ryyryyryryry']

y = ['0001', '0002','0003', '0004', '0001', '0002']

grouped_list = []
for entry in x:
    if entry in y:
        new_list = []
        new_list.append(entry)
        for i in range(x.index(entry)+1, len(x)):
            if(x[i] not in y):
                new_list.append(x[i])
            else:
                break
        grouped_list.append(list(new_list))
print (grouped_list)

有人可以告訴我如何避免這種情況嗎?

if您的最內心有錯誤,請執行以下操作:

if(x[i][0] not in y):

在這里,您要檢查項目的第一個字符是否在列表中。 它應該是:

if(x[i] not in y):

如果要避免重復使用組密鑰,可以使用字典:

grouped_list = []
d = {}
i = 0
current_key = None

while i < len(x):

    if x[i] in y:
        current_key = x[i]
        if not d.has_key(current_key):
            d[current_key] = []
        i += 1
        continue

    while i < len(x) and x[i] not in y:
        d[current_key].append(x[i])
        i += 1

for k in d:
    grouped_list.append([k] + d[k])

print (grouped_list)

兩個班輪怎么樣? (對不起,不能在一排內完成)

# At the top of your .py file    
from __future__ import print_function

x = ['0001', 'Random message XYX', 'Random second message IAI', '0002', 'Random message IAM', 'Random second message OMA', 'Random third message OMA', '0003', 'Random message XAK', 'Random second message YAB', '0004', ' Random message INA']
y = ['0001', '0002', '0003', '0004']

indexes = [k for k in [x.index(toks) for toks in y]]
print([x[i:j] for i, j in zip(indexes, indexes[1:]+[len(x)])])

給我

[['0001', 'Random message XYX', 'Random second message IAI'],
 ['0002',
  'Random message IAM',
  'Random second message OMA',
  'Random third message OMA'],
 ['0003', 'Random message XAK', 'Random second message YAB'],
 ['0004', ' Random message INA']]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM