Python: Compare elements of list with previous and next list of elements?
Tag : list , By : Mossy Breen
Date : March 29 2020, 07:55 AM
Hope that helps I have a list of list, Let's say , When you reference items, you need to use list1 if i !=0 and j == list1[i-1][0]:
print(j,list1[i-1][0],'equal')
if i -1 < len(list1) and j==list1[i+1][0]:
print(j,list1[i+1][0],'equal')
for i, row in enumerate (list1): # Use the list1[1] list as the key data
for item in row[1]: # Compare all items in this list with previous row first element
# and next row first element
if i > 0: # Previous row
if item == list1[i-1][0]:
print ('{} : {} equal'.format(item, list1[i-1][0]))
if i < len(list1) - 1: # Next row
if item == list1[i+1][0]:
print ('{} : {} equal'.format(item, list1[i+1][0]))
|
How to use certain elements in a list to compare elements in other lists and output a certain element if duplicates are
Date : March 29 2020, 07:55 AM
like below fixes the issue I am new here (and new to programming). I have tried to locate the relevant questions with no luck. , @wagaman: Hi Pal, Here a little snippet to do this job: universe = []
universe.append([0, 'a', 1, 3, 3.3, 220, 22.27])
universe.append([0, 'b', 1, 13, 3.3, 220, 23.19])
universe.append([0, 'c', 1, 23, 3.3, 220, 24.11])
universe.append([1, 'a', 1, 3, 3.5, 200, 20.02])
universe.append([1, 'b', 1, 43, 3.3, 220, 25.94])
universe.append([2, 'a', 1, 3, 3.3, 250, 26.86])
def extract_from_lists(universe, keys=[1,2,3], matches=['a',1,3]):
if len(keys) == len(matches):
for l in universe:
valid = True
for i in range(len(keys)):
if l[keys[i]] != matches[i]:
valid = False
break
if valid:
print("{},{},{},{}".format(l[0],l[4],l[5],l[-1]))
else:
print('Keys List and Matches List length must be equal.')
extract_from_lists(universe)
|
compare two lists by elements and return the third list elements difference
Date : March 29 2020, 07:55 AM
should help you out If all elements in l1 are unique, you can create a dictionary from l1 and l1_values and then look up the values while looping through l1_new: my_dict = dict(zip(l1, l1_values))
[my_dict[k] for k in l1_new]
# [1, 3]
|
How to compare elements in list, but exclude tail of elements, if duplicates exist: make new list that includes the tail
Date : March 29 2020, 07:55 AM
this one helps. An approach would be using an OrderedDict (to keep the same order of currentList) with split() and some list comprehensions like this: from collections import OrderedDict
current_list = ["Blur1['size']: $gui index:1", "Blur1['size']: $gui index:3",
"Blur2['mix']: $gui?4:8"]
temp = [item.split('index:') for item in current_list]
d = OrderedDict()
for i in temp:
if len(i) == 2:
d[i[0]] = i[1] if i[0] not in d else '{}{}'.format(d[i[0]], i[1])
else:
d[i[0]] = ""
new_list = ['{}{}'.format(*item) for item in d.items()]
>>> new_list
["Blur1['size']: $gui 13", "Blur2['mix']: $gui?4:8"]
|
Return elements from list that satisfy condition of another list to then compare with a fourth list python
Date : March 29 2020, 07:55 AM
it should still fix some issue This is a perfect use-case for itertools.compress: >>> from itertools import compress, starmap
>>> from operator import gt
>>> bools = list(starmap(gt, zip(listA, listB)))
>>> bools
[False, True, False, False, True, False, True, False, False, True]
>>> list(compress(listA, bools))
[232, 237, 245, 230]
>>> list(compress(listD, bools))
[2, 5, 7, 10]
|