Mapping between matrices - python-3.x

I have 2 matrices:
list_alpha = [['a'],
['b'],
['c'],
['d'],
['e']]
list_beta = [['1', 'a', 'e', 'b'],
['2', 'd', 'X', 'X'],
['3', 'a', 'X', 'X'],
['4', 'd', 'a', 'c'],
And my goal is if a letter from list_alpha is in a sublist of list_beta, then the first element of that line in list_beta (the #) is added to the correct line in list_alpha.
So my output would be:
final_list = [['a', '1', '3', '4'],
['b', '1'],
['c', '4'],
['d', '2', '4'],
['e', '1']]
But I'm pretty new to python and coding in general and I'm not sure how to do this. Is there a way to code this? Or do I have to change the way the data is stored in either list?
Edit:
Changing list_alpha to a dictionary helped!
Final code:
dict_alpha = {'a': [], 'b': [], 'c': [], 'd': [], 'e':[]}
list_beta = [['1', 'a', 'e', 'b'],
['2', 'd', 'X', 'X'],
['3', 'a', 'X', 'X'],
['4', 'd', 'a', 'c'],
['5', 'X', 'X', 'e'],
['6', 'c', 'X', 'X']]
for letter in dict_alpha:
for item in list_beta:
if letter in item:
dict_alpha.get(letter).append(item[0])
print(dict_alpha)

You can use dict_alpha as same as list_alpha , then fix your for loop.
For example:
dict_alpha = [['a'],
['b'],
['c'],
['d'],
['e']]
list_beta = [['1', 'a', 'e', 'b'],
['2', 'd', 'X', 'X'],
['3', 'a', 'X', 'X'],
['4', 'd', 'a', 'c'],
['5', 'X', 'X', 'e'],
['6', 'c', 'X', 'X']]
for al in dict_alpha:
for bt in list_beta:
for i in range(1, len(bt)):
if (bt[i] == al[0]):
al.append(bt[0])
print(dict_alpha)
Output:
[['a', '1', '3', '4'],
['b', '1'],
['c', '4', '6'],
['d', '2', '4'],
['e', '1', '5']]
Hope to helpful!

Related

How do I replace all occurrences of '+' with '.5' in a dataframe?

I have a dataframe below:
data = {'Name': ['A', 'B', 'C', 'D'],
'Lower': ['+', '2', '2+', '3'],
'Upper': ['2','3+','4+','5']}
df= pd.DataFrame(data)
The expected output should be:
data = {'Name': ['A', 'B', 'C', 'D'],
'Lower': ['.5', '2', '2.5', '3'],
'Upper': ['2','3.5','4.5','5']}
I have tried using the code below but it only replaces + and not 2+, 3+, 4+
df.replace('+','.5', regex=False)
I also tried using str.replace but the rest of the values become NaN:
df['Lower'].str.replace('+', '.5')
you can override the value by looping, but it's not the fastest solution
import pandas as pd
data = {'Name': ['A', 'B', 'C', 'D'],
'Lower': ['+', '2', '2+', '3'],
'Upper': ['2','3+','4+','5']}
lower = []
upper = []
newdata = {'Name': ['A', 'B', 'C', 'D'],
'Lower': lower,
'Upper': upper}
for i in data['Lower']:
if "+" in i:
lower.append(i.replace("+", ".5"))
else:
lower.append(i)
for j in data['Upper']:
if "+" in j:
upper.append(j.replace("+", ".5"))
else:
upper.append(j)
df= pd.DataFrame(newdata)
print(df)

Is there a way to transform a nominal DataFrame into a Bubble Plot in Altair?

I have the following code:
test_dict = {'A': ['a', 'b', 'c'], 'B': ['d', 'e', 'f'], 'C': ['g', 'h', 'i'],
'D': ['j', 'k', 'l'], 'E': ['m', 'n', 'o'], 'F': ['p', 'q', 'r'],
'G': ['s', 't', 'u'], 'H': ['v', 'w', 'x']}
test_df = pd.DataFrame(test_dict)
which produces the following DataFrame:
Is there a way to transform this into a BubblePlot in Altair? I want the columns (A - H) to make up the y-axis, and I want there to be a bubble for each entry in the column (so in this case, three entries per column). Can this be done?
You have wide-form data that you need to transform to long-form data (See See Wide-form vs. Long-form data in Altair's docs). You can generally address this with the Fold Transform:
import pandas as pd
import altair as alt
test_dict = {'A': ['a', 'b', 'c'], 'B': ['d', 'e', 'f'], 'C': ['g', 'h', 'i'],
'D': ['j', 'k', 'l'], 'E': ['m', 'n', 'o'], 'F': ['p', 'q', 'r'],
'G': ['s', 't', 'u'], 'H': ['v', 'w', 'x']}
test_df = pd.DataFrame(test_dict)
alt.Chart(test_df).transform_fold(
list(test_df.columns), as_=['key', 'value']
).mark_point().encode(
x='value:N',
y='key:N',
)

How do you rearrange a list of lists where the first element holds multiple values and creates a new list of lists while repeating the first element?

Given a list of lists:
list_format = [['a', 'c', 'f', 'b'], ['j', 'l', 'o', 'c'], ['q', 's', 'v', 'e']]
'c', 'f', 'b' must be mapped to 'a'
'l', 'o', 'c' must be mapped to 'j'
's', 'v', 'e' must be mapped to 'q'
The output should look like this:
[['a','c'],['a','f'],['a','b'],['j','l'],['j','o'],['j','c'],['q','s'],['q','v'],['q','e']]
I've tried so far:
list_dict = {element[0]:element[1:] for element in list_format}
newer_lst = []
for key, value in list_dict.items():
newer_lst.append((key, value))
newer_lst
Gives me the output of tuples:
[('a', ['c', 'f', 'b']), ('j', ['l', 'o', 'c']), ('q', ['s', 'v', 'e'])]
I'm newer at this and trying to rearrange, any advice would be awesome, been stuck for days with trial and error(searched google countless times and constantly googling. I feel I'm getting close but can't seem to put it together.
Here is a one-liner, using slicing:
[[i[0],j] for i in list_format for j in i[1:]]
gives:
[['a', 'c'], ['a', 'f'], ['a', 'b'], ['j', 'l'], ['j', 'o'], ['j', 'c'], ['q', 's'], ['q', 'v'], ['q', 'e']]
Also, if you iterate through your value variable, you get your result:
list_dict = {element[0]:element[1:] for element in list_format}
newer_lst = []
for key, value in list_dict.items():
for i in value:
newer_lst.append((key, i))
print(newer_lst)
You don't need to create a loop, just loop on sub array and append new sub array to main output array on the fly, like new_list.append([lst[0], item])
new_list = []
for lst in list_format:
for item in lst[1:]:
new_list.append([lst[0], item])
print(new_list)
#output
#[['a', 'c'], ['a', 'f'], ['a', 'b'], ['j', 'l'], ['j', 'o'], ['j', 'c'], ['q', 's'], ['q', 'v'], ['q', 'e']]

Is there a function to create a list of unique elements in unique index using python?

Is there a way to create a new set of lists of elements, with each element of the original list in a unique index in the following lists?
orginal_list=['r', 'g', 'b', 'y']
output: ['y', 'g', 'r', 'b'],['g', 'y', 'b', 'r'],['r', 'b', 'y', 'g'],['b', 'r', 'g', 'y']
or
output: ['y', 'r', 'b', 'g'],['g', 'y', 'r', 'b'],['r', 'b', 'g', 'y'],['b', 'g', 'y', 'r']
or
...
I have tried to use iterators.permutations, but this does not fit with the unique index requirement.
Easiest would be to rotate the list. Here is a simple generator function producing all such rotations:
def rots(lst):
for i in range(len(lst)):
yield lst[i:] + lst[:i]
>>> list(rots(['r', 'g', 'b', 'y']))
[['r', 'g', 'b', 'y'],
['g', 'b', 'y', 'r'],
['b', 'y', 'r', 'g'],
['y', 'r', 'g', 'b']]
The rotating guarantees that each element actually occurs in each index exactly once.

Extract longest common path between two lists in python

Lets say there are two lists
L1=[['A', ['C', ['B', ['D', 0]]]],
[['A', ['D', ['K', ['C', ['E', 0]]]]],
[['A', ['C', ['B', ['M', 0]]]]]
and
L2=[['A', ['C', ['B', ['K', 0]]]],
[['A', ['C', ['B', ['B', ['E', 0]]]]],
[['A', ['D', ['K', ['F', 0]]]]]
Then the output should return all the sub-paths with longest common path. For example:
Since 'A', 'C', 'B' is common L1 and L2; output should be:
[['A', ['C', ['B', ['D', 0]]]],
[['A', ['C', ['B', ['M', 0]]]],
[['A', ['C', ['B', ['K', 0]]]],
[['A', ['C', ['B', ['B', ['E', 0]]]]]
. Also, 'A', 'D', 'K' is also common for one time in L1 and L2; the output whould be:
[['A', ['D', ['K', ['C', ['E', 0]]]]],
[['A', ['D', ['K', ['F', 0]]]]]
I tried :
[i for i in L1 if i in L2]
but it will give the output of all the common paths till the leaf (end).
take something from the great and marvelous c!
you can simply use a while in another and check letter by letter, if one is different exit the first while and put the character to 0
like
while (var[i][n] && var2[i][n])
while (var[i][n] == var2[i][n])
n = n + 1
var[i][n] = 0
or something like that.
but it is not really optimised.

Resources