我有一个列表:
loc_list=[['1,', "'Houston'"],
['4,', "'Stafford'"],
['5,', "'Bellaire'"],
['5,', "'Sugarland'"],
['5,', "'Houston'"],
['6,', "'Atlanta'"],
['6,', "'Sacramento'"],
['7,', "'Milwaukee'"],
['8,', "'Chicago'"],
['8,', "'Dallas'"],
['8,', "'Philadephia'"],
['8,', "'Seattle'"],
['8,', "'Miami'"],
['9,', "'Arlington'"],
['11,', "'Austin'"]]
现在我想在第一个位置合并具有相同元素的列表。
Desired_list=[['1,', "'Houston'"],
['4,', "'Stafford'"],
['5,', "'Bellaire','Sugarland','Houston'"],
['6,', "'Atlanta','Sacramento'"],
['7,', "'Milwaukee'"],
['8,', "'Chicago','Dallas','Philadephia','Seattle','Miami'"],
['9,', "'Arlington'"],
['11,', "'Austin'"]]
我很无能。
答案 0 :(得分:1)
你可以这样做:
desired_list = {}
for elem in loc_list:
if not elem[0] in desired_list:
desired_list[elem[0]] = []
desired_list[elem[0]].append(elem[1])
print desired_list
这不是列表而是字典 输出是:
{'9,': ["'Arlington'"], '8,': ["'Chicago'", "'Dallas'", "'Philadephia'", "'Seattle'", "'Miami'"], '11,': ["'Austin'"], '7,': ["'Milwaukee'"], '6,': ["'Atlanta'", "'Sacramento'"], '5,': ["'Bellaire'", "'Sugarland'", "'Houston'"], '4,': ["'Stafford'"], '1,': ["'Houston'"]}
答案 1 :(得分:0)
分开您的疑虑。 根据您的索引范围,只需使用数组或哈希表。
然后对于每个项目,你可以拥有一套,并根据你的愿望,你可以在你的集合中具有多样性。
对于初始构造,您需要遍历列表。
答案 2 :(得分:0)
使用类似dict
的结构会更好,defaultdict
set
似乎是最佳选择。
以下是一个例子:
>>> from collections import defaultdict
>>> d = defaultdict(set)
>>> for index,city in loc_list:
... d[index].add(city)
...
>>> for k,v in d.items():
... print(k,v)
...
4, {"'Stafford'"}
6, {"'Atlanta'", "'Sacramento'"}
1, {"'Houston'"}
5, {"'Houston'", "'Bellaire'", "'Sugarland'"}
9, {"'Arlington'"}
8, {"'Philadephia'", "'Dallas'", "'Seattle'", "'Chicago'", "'Miami'"}
7, {"'Milwaukee'"}
11, {"'Austin'"}
BTW:您的输入列表看起来有点奇怪,键是带有尾随昏迷的字符串(而不是数字):'4,'
; 值有双引号:"'Chicago'"
。
答案 3 :(得分:0)
我已经写过这个解决方案,我觉得它非常清晰有效。 使用defaultdict(集合包中的数据类型),您可以轻松获得相同的结果:
import collections as co
loc_list=[['1,', "'Houston'"],
['4,', "'Stafford'"],
['5,', "'Bellaire'"],
['5,', "'Sugarland'"],
['5,', "'Houston'"],
['6,', "'Atlanta'"],
['6,', "'Sacramento'"],
['7,', "'Milwaukee'"],
['8,', "'Chicago'"],
['8,', "'Dallas'"],
['8,', "'Philadephia'"],
['8,', "'Seattle'"],
['8,', "'Miami'"],
['9,', "'Arlington'"],
['11,', "'Austin'"]]
def transform_list():
count = co.defaultdict(list)
for sublist in loc_list:
count[sublist[0]].append(sublist[1])
return count
if __name__ == '__main__':
count = transform_list()
print(count)
将列表传递给defaultdict构造函数,您可以在指定新密钥时自动创建空列表。 我建议你考虑这个例子:defaultdict examples