Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在列表中收集部分重复的元素,并将它们合并到一个元素中_Python_Python 3.x - Fatal编程技术网

Python 在列表中收集部分重复的元素,并将它们合并到一个元素中

Python 在列表中收集部分重复的元素,并将它们合并到一个元素中,python,python-3.x,Python,Python 3.x,我试图解析一个包含属性及其值的巨大Excel文件。 问题如下:某些属性可以包含多个值 例如: list = ['a=1', 'b=2', 'c=3', 'd=4', 'd=5', 'd=6', 'e=7'] 应该是: list2 = ['a=1', 'b=2', 'c=3', 'd=4,5,6', 'e=7'] 元素是长度可变的字符串,它们之间用“=”分隔 以下是我从Excel文件生成列表的方式: #for each row in the excel file. for rows in ra

我试图解析一个包含属性及其值的巨大Excel文件。 问题如下:某些属性可以包含多个值

例如:

list = ['a=1', 'b=2', 'c=3', 'd=4', 'd=5', 'd=6', 'e=7']
应该是:

list2 = ['a=1', 'b=2', 'c=3', 'd=4,5,6', 'e=7']
元素是长度可变的字符串,它们之间用“=”分隔

以下是我从Excel文件生成列表的方式:

#for each row in the excel file.
for rows in range(DATA_ROW, sheet.nrows):
#generate a list with all properties.
for cols in range(sheet.ncols):
    #if the propertie is not emty 
    if str(sheet.cell(PROPERTIE_ROW,cols).value) is not '':
        proplist.append(sheet.cell(PROPERTIE_ROW,cols).value + '=' + str(sheet.cell(rows,cols).value) + '\n')
我试了一下,但效果不太好

last_item = ''
new_list = []
#find and collect multiple values.
for i, item in enumerate(proplist):
#if the propertie is already in the list
if str(item).find(last_item) is not -1:
    #just copy the value and append it to the propertie
    new_list.insert(i, propertie);
else:
    #slize the string in propertie and value
    pos = item.find('=')
    propertie = item[0:pos+1]
    value = item[pos+1:len(item)]
    #save the propertie
    last_item = propertie
    #append item
    new_list.append(item)

任何帮助都将不胜感激

如果顺序不重要,您可能会使用
defaultdict
来处理这类事情:

from collections import defaultdict
orig = ['a=1', 'b=2', 'c=3', 'd=4', 'd=5', 'd=6', 'e=7']
d = defaultdict(list)
for item in orig:
    k,v = item.split('=',1)
    d[k].append(v)

new = ['{0}={1}'.format(k,','.join(v)) for k,v in d.items()]
print(new)  #['a=1', 'c=3', 'b=2', 'e=7', 'd=4,5,6']

我想如果顺序确实重要,您可以使用
orderedict
+
setdefault
,但它确实没有那么漂亮:

from collections import OrderedDict
orig = ['a=1', 'b=2', 'c=3', 'd=4', 'd=5', 'd=6', 'e=7']
d = OrderedDict()
for item in orig:
    k,v = item.split('=',1)
    d.setdefault(k,[]).append(v)

new = ['{0}={1}'.format(k,','.join(v)) for k,v in d.items()]
print new # ['a=1', 'b=2', 'c=3', 'd=4,5,6', 'e=7']

当存在重复的键和值时会发生什么情况?说
['a=7','a=7']
?是否应该合并到
['a=7,7']
?应该维持秩序吗?从技术上讲,这不会发生。但如果是这样,则可以忽略重复项。顺序很重要,应该保留。我不知道这是否是个好主意,但我创建了一个OrderedDefaultDict,如下所示:
类OrderedDefaultDict(OrderedDict,defaultdict):
定义为
def\uu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu(self,default)的
;OrderedDict.\uuuuu init\uuuuuuuuuuuu(自)
。这似乎有效,但我没有多重继承的经验。我只是祈祷
orderedict
defaultdict
能够覆盖
dict
的不同方法。“但我想我至少应该提一下这个想法。”史蒂文伦巴尔斯基——实际上我以前也提过。但实际上,将
OrderedDict
子类化并将“默认”行为放入
\uuuuu missing\uuuu
可能更容易。