Difference between multiple elements in list with same string . Python 2.7 [closed]

2024/11/15 0:47:48

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':[]}
Answer

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.

https://en.xdnf.cn/q/119454.html

Related Q&A

EDX Course API: Getting EDX course list

I am making a project in python/flask. I want to get a list of all the courses of edx. But the API provides the list page by page. I cant figure out how to get the entire list. Any help is appreciated.…

How to extract particlar message from a vast displayed output using python regular expression?

Firstly in the code, i would like to know How can i add a for loop for CH (1-11) instead of writing for every number Also how to extract SUCCESS and FAILED message from the output (reference) For examp…

Enemy health bar aint draining pygame [duplicate]

This question already has answers here:How to put a health bar over the sprite in pygame(2 answers)Closed 3 years ago.Okay so I was trying to make a health bar for my enemy class and only a part of it …

Python - Create and instantiate class

I am building a class of playlists, which will hold many playlists of the same genre.class playlist(object):def __init__(self,name):self.name = nameI would like to instantiate them passing the user:def…

Missing samples of a dataframe in pandas

My df:In [163]: df.head() Out[163]: x-axis y-axis z-axis time 2017-07-27 06:23:08 -0.107666 -0.068848 0.963623 2017-07-27 06:23:08 -0.105225 -0.070068 0.963867 .....I set the index as dateti…

How to hide a button after clicked in Python

I was wondering how to hide my start button after being clicked so that If the user accidentally was clicker happy they wouldnt hit the button causing more bubbles to appear on screen. Below is a snipp…

Unable to click on QRadioButton after linking it with QtCore.QEventLoop()

Few days back i had situation where i had to check/uncheck QRadioButton in for loop. Here is the link Waiting in for loop until QRadioButton get checked everytime? After implementing QEventLoop on thi…

Distance Matrix Haversine

I am working on a data frame that looks like this :lat lon id_zone 0 40.0795 4.338600 1 45.9990 4.829600 2 45.2729 2.882000 3 45.7336 4.850478 4 45.6981 5.…

python google geolocation api using wifi mac

Im trying to use Googles API for geolocation giving wifi data to determine location. This is their intro. And this is my code@author: Keith """import requestspayload = {"c…

Python requests and variable payload

Reticulated members,I am attempting to use a GET method that is supported against the endpoint. However, I am using python and wanting to pass the user raw_input that is assigned to a variable:uid = ra…