具有相同字符串的列表中的多个元素之间的差异。 Python 2.7 [英] Difference between multiple elements in list with same string . Python 2.7

查看:618
本文介绍了具有相同字符串的列表中的多个元素之间的差异。 Python 2.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点混乱,所以我会尽我所能解释我的目标。 简而言之,我试图查看列表中的子列表。在这些子列表中,一些具有相同的起始元素(子列表[0]),并且我想记录该子列表与以相同元素开始的其他子列表之间的差异

  data = [['o1415','1','0','1'],['o1415','0','0','0'] ,'01414','0','0','0'],['o1414','1','0','0'], '0'],['o1408','0','0','1'],['o1406','0','0','0']] 
D_changes = {}

这里是一个包含4个元素的列表。 。第一个有一个名字,第二/第三/第四个元素有数字。



我试图生成一个字典,{name: ,差别])}



例如,data [0]和data [1]都有o1415因为他们有相同的字符串为第一个元素我想比较其余的列表彼此。因此数据[0]与数据[1]在数据[0] [1]和数据[0] [2]中不同。 。 。所以我想添加'o1415':['first','third']到空字典D_changes。



另一个例子是o1414 [2],data [3],data [4],对于这些列表,一个元素在[1]位置是不同的,所以我想添加'o1414' / p>

到我想要获得这种类型内容的字典

  desired_changes = {'o1415':['first','third'],'o1414':['first'],'o1408':[],'o1406':[]} 


解决方案

我会给你一个方向,而不是一个完整的答案。



首先,加载一个dict到类似的项目进行进一步处理;我将使用 defaultdict

  d = defaultdict 

data = [['o1415','1','0','1'],['o1415','0','0','0'], ','0','0','0'],['o1414','1','0','0'],['o1414','0','0','0' ,['o1408','0','0','1'],['o1406','0','0','0']]


d [sub [0]]。append([sub:1]]中的x的int(x))

然后,对于给定的键,只需查看其值的 zip 即可。即'o1414':

  d ['o1414'] 
Out [58]:[[0,0 ,0],[1,0,0],[0,0,0]]

list(zip(* d ['o1414']))
Out [59] [(0,1,0),(0,0,0),(0,0,0)]


$ b b

我们知道如果它们都是相等的,如果它们都是1,或者所有0;否则它是不同的。所以只要做:

  [any(x)and not all(x)for x in zip(* d ['o1414' ])] 
Out [60]:[True,False,False]



像美学 - any(x)而不是所有(x)



无论如何, True 意味着你在这个槽中有一个不同的值。我会把它留给你做所有的钥匙,并把它变成你想要的格式。


This is a little confusing so I will try my best to explain my goal. In a nutshell i'm trying to look at a sublist within a list. In those sublists, some have the same starting element (sublist[0]) and i want to record the differences between that sublist with other sublists starting with the same element

data = [['o1415', '1', '0', '1'], ['o1415', '0', '0', '0'], ['o1414', '0', '0', '0'], ['o1414', '1', '0', '0'], ['o1414', '0', '0', '0'], ['o1408', '0', '0', '1'], ['o1406', '0', '0', '0']]
D_changes = {}

here is a list with 4 elements . . the first of which has a name, 2nd/3rd/4th elements have digits .

i'm trying to generate a dictionary that has the {name:[then,the,differences])}

for example data[0] and data[1] both have 'o1415' as their first element . since they have the same string for the first element i want to compare the rest of the lists with each other . so data[0] differs in data[0][1] and data[0][2] from data[1] . . . so i want to add 'o1415':['first','third'] to the empty dictionary D_changes.

another example would be 'o1414' which is in data[2],data[3],data[4] and for these lists, one element is different in the [1] position so i'd like to add 'o1414' : ['first'] to the empty dictionary above

in the end i want to obtain a dictionary with this type of content

desired_changes = {'o1415':['first','third'],'o1414':['first'],'o1408':[],'o1406':[]}

解决方案

I'll give you a direction more than a full answer.

First, load up a dict to group like items for further processing; I'll use a defaultdict:

d = defaultdict(list)

data = [['o1415', '1', '0', '1'], ['o1415', '0', '0', '0'], ['o1414', '0', '0', '0'], ['o1414', '1', '0', '0'], ['o1414', '0', '0', '0'], ['o1408', '0', '0', '1'], ['o1406', '0', '0', '0']]

for sub in data:
    d[sub[0]].append([int(x) for x in sub[1:]])

Then, for a given key, simply look at the zip of its values. i.e. for 'o1414':

d['o1414']
Out[58]: [[0, 0, 0], [1, 0, 0], [0, 0, 0]]

list(zip(*d['o1414']))
Out[59]: [(0, 1, 0), (0, 0, 0), (0, 0, 0)]

We know if they're all equal if it's all 1, or all 0; otherwise it's different. So just do:

[any(x) and not all(x) for x in zip(*d['o1414'])]
Out[60]: [True, False, False]

I particularly like the aesthetics of that - any(x) and not all(x). Python can be beautiful sometimes.

Anyway, True means that you have a differing value in that slot. I'll leave it up to you do do that for all your keys and to get it into the format that you want.

这篇关于具有相同字符串的列表中的多个元素之间的差异。 Python 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆