Python将重复列表转换为子列表

4xy9mtcn  于 2023-02-01  发布在  Python
关注(0)|答案(4)|浏览(132)

我有多个列表包含,例如.

a = [['server_1', 'abc'], ['server_2', 'abc'], ['server_1', 'def']]
b = [['server_3', 'abc'], ['server_3', 'def'], ['server_4', 'abc']]
c = [['server_5', 'abc'], ['server_6', 'abc'], ['server_5', 'def']]

我想把数据转换成子列表,例如像下面:

OUTPUT:
a = [['server_1', 'abc', 'def'], ['server_2', 'abc']]
b = [['server_3', 'abc', 'def'], ['server_4', 'abc']]
c = [['server_5', 'abc', 'def'], ['server_6', 'abc']]

我想要做的是挑选出服务器名称,并将该列表附带的任何子数据添加到单个元素中,如上所示。
任何帮助都很感激
我尝试使用多个循环和if语句来比较列表中的前一个值和下一个值,但没有效果。

   for part in repeating_list:      
      if part not in repeating_list:        
        new_list.append([part])      
      else:        
        pass (this should return it to sublist)
  return new_list
bzzcjhmw

bzzcjhmw1#

您可以使用collections.defaultdict在一次传递中合并子列表

from collections import defaultdict
a = [['server_1', 'abc'], ['server_2', 'abc'], ['server_1', 'def']]

def combine(my_list):
    result = defaultdict(list)
    for key, value in my_list:
        result[key].append(value)
    return [[key] + value for key, value in result.items()]

print(combine(a))

输出

[['server_1', 'abc', 'def'], ['server_2', 'abc']]
e1xvtsh3

e1xvtsh32#

一个简单的方法是使用dict,将第一个元素存储为键,然后将第二个元素作为列表附加到值上。

from collections import defaultdict

a = [['server_1', 'abc'], ['server_2', 'abc'], ['server_1', 'def']]

# Use a defaultdict to avoid testing for key in dict
data = defaultdict(list)
for item in a:
    data[item[0]].append(item[1])

# Convert to a list of lists, flattening the values
print([[k, *v] for k, v in data.items()])

[[“服务器_1”,“abc”,“定义”],[“服务器_2”,“abc”]]

t40tm48m

t40tm48m3#

解决这个问题的一个简单方法是使用两遍方法,这意味着我们首先获取服务器的名称,然后在第二遍中使用服务器提取数据。
下面是示例代码:

a = [['server_1', 'abc'], ['server_2', 'abc'], ['server_1', 'def']]
b = [['server_3', 'abc'], ['server_3', 'def'], ['server_4', 'abc']]
c = [['server_5', 'abc'], ['server_6', 'abc'], ['server_5', 'def']]

def solution(inp):
    servers: list[str] = []

    for x in inp:
        if x[0] not in servers: servers.append(x[0])

    output = []

    for server in servers:
        current_server_params = [server]
        for x in inp:
            if x[0] == server:
                current_server_params.extend(x[1:])
        output.append(current_server_params)

    print(output)

solution(a)
solution(b)
solution(c)

输出:

[['server_1', 'abc', 'def'], ['server_2', 'abc']]
[['server_3', 'abc', 'def'], ['server_4', 'abc']]
[['server_5', 'abc', 'def'], ['server_6', 'abc']]
wwtsj6pe

wwtsj6pe4#

我将添加一个键列表(new_list_keys),它可以跟踪索引,并且可以很容易地检查键(part[0])是否已经存在。如果没有,则将部分添加到new_list,并将相关键添加到键列表。如果是,则将列表的其余部分连接到键的idx处的列表

def solution(repeating_list):
  new_list = []
  new_list_keys = []
  for part in repeating_list:
    if part[0] not in new_list_keys:        
      new_list.append(part)
      new_list_keys.append(part[0])
    else:        
      idx = new_list_keys.index(part[0])
      new_list[idx]+=part[1:]
  return new_list

print(solution([['server_1', 'abc'], ['server_2', 'abc'], ['server_1', 'def']]))
print(solution([['server_3', 'abc'], ['server_3', 'def'], ['server_4', 'abc']]))
print(solution([['server_5', 'abc'], ['server_6', 'abc'], ['server_5', 'def']]))

相关问题