通过列表对键内的值进行分组

2024-06-28 16:32:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将键中的值分组到一个值列表中,方法是在出现的每个引号后将它们拆分。所以每个地标都有一个位置列表, 例如:

{'Landmarks': ['Great norwich', 'Larger building', 'Leaning building'],
 'location':  ['North 28th Street',
               'Stadium High School',
               '',
               'Charles Bridge',
               "St Vitus' Cathedral",
               '',
               'All Saints Church',
               'Royal Courts of Justice',
               'Lonsdale Road']
}

预期产出:

作为字典:

{'Landmarks': ['Great norwich', 'Larger building', 'Leaning building'],
 'location':  [['North 28th Street', 'Stadium High School'],
               ['Charles Bridge', "St Vitus' Cathedral"],
               ['All Saints Church', 'Royal Courts of Justice', 'Lonsdale Road']]
}

或作为数据帧:

Landmarks               Locations
Great norwich           ['North 28th Street','Stadium High School']
Larger building         ['Charles Bridge','St Vitus' Cathedral',]
Leaning building        ['All Saints Church','Royal Courts of Justice','Lonsdale Road']

我试过:

pd.DataFrame(data)

我得到错误->

arrays must all be same length

我可以使用以下命令删除引号:

for v in data.values():
    if ('') in v:
        v.remove((''))

但是,如何在删除前将引号之前的所有值作为列表分配,以获得上面的预期值


Tags: street列表引号bridgehighbuildingschoolcharles
1条回答
网友
1楼 · 发布于 2024-06-28 16:32:03

下面是一种在''上拆分列表的方法。它使用了一个简短的列表理解itertools.groupby和一个assignment expression(python≥ 3.8

from itertools import groupby
d2 = d.copy()
d2['location'] = [G for _,g in groupby(d['location'], ''.__eq__)
                  if (G:=list(g)) != ['']]

python版本<;3.8

from itertools import groupby
d2 = d.copy()
d2['location'] = [G for _,g in groupby(d['location'], ''.__eq__)
                  for G in [list(g)] if G != ['']]

输出:

{'Landmarks': ['Great norwich', 'Larger building', 'Leaning building'],
 'location': [['North 28th Street', 'Stadium High School'],
  ['Charles Bridge', "St Vitus' Cathedral"],
  ['All Saints Church', 'Royal Courts of Justice', 'Lonsdale Road']]}

作为数据帧:

>>> pd.DataFrame(d2)
          Landmarks                                                     location
0     Great norwich                     [North 28th Street, Stadium High School]
1   Larger building                        [Charles Bridge, St Vitus' Cathedral]
2  Leaning building  [All Saints Church, Royal Courts of Justice, Lonsdale Road]

使用的输入:

d = {'Landmarks': ['Great norwich', 'Larger building', 'Leaning building'],
     'location': ['North 28th Street',
                  'Stadium High School',
                  '',
                  'Charles Bridge',
                  "St Vitus' Cathedral",
                  '',
                  'All Saints Church',
                  'Royal Courts of Justice',
                  'Lonsdale Road']}

相关问题 更多 >