How to add items from one list to another? - python-3.x

I have a list1 in python as listed below:
ls1
['A', 4, 'M', '1', 128.2, 169.818, '2019-02-27']
['B', 4, 'M', '1', 169.818, 172.3, '2019-02-25']
ls2
['2019-02-27','2019-02-25']
When I am trying to add another date item from another list, it is not adding as a part of each row in list rather it is adding as a seperate component like below:
ls3
['A', 4, 'M', '1', 128.2, 169.818, '2019-02-27'],
'2019-02-27',
['B', 4, 'M', '1', 169.818, 172.3, '2019-02-25'],
'2019-02-25'
I would rather needed ls3 as:
['A', 4, 'M', '1', 128.2, 169.818, '2019-02-27','2019-02-27']
['B', 4, 'M', '1', 169.818, 172.3, '2019-02-25','2019-02-25']

You can use a list comprehension:
ls1 = [['A', 4, 'M', '1', 128.2, 169.818, '2019-02-27'], ['B', 4, 'M', '1', 169.818, 172.3, '2019-02-25']]
ls2 = ['2019-02-27','2019-02-25']
new_ls1 = [l1 + [l2] for l1, l2 in zip(ls1, ls2)]
A more hack-ey way (not faster, use the first one!):
new_ls1 = list(map(list, zip(*zip(*ls1), ls2)))
Or, if you would like, you can operate in-place:
for i, item in enumerate(ls2):
ls1[i].append(item)

Related

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 I get FOR to work on a list but leaping through it according to another list and how to get it back to the start to keep counting like a loop?

So this is the chromatic_scale = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
I want to form major, minor and other scales based on this.
To form a major scale, for example, I need to leap through the chromatic scale like this: major = [2, 2, 1, 2, 2, 2, 1] (in music, we say "tone, tone, semitone, tone, tone, tone, semitone" where tone means leaping 2 list items and semitone means leaping 1, thus the list)
A practical example: Starting from 'C', I should get ['C', 'D', 'E', 'F', 'G', 'A', 'B', 'C'] (yes, it should loop back to 'C' at the end).
1 - I thought of doing it with FOR but how do I get FOR to work on a list (chromatic) but leaping through it according to another list (major)?
2 - And if I start from 'A', for instance, how to get it back to the beginning to keep counting?
At the end, I managed to do it but I used WHILE instead of FOR.
chromatic = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B',
'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
def major(tom):
major_scale = [2, 2, 1, 2, 2, 2, 1]
step = 0
t = chromatic.index(tom)
m = []
while len(m) < 8:
m.append(chromatic[t])
if len(m) == 8:
break
t += major_scale[step]
step += 1
return m
x = major('D')
print(x)
Thank you everyone!

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']]

Mapping between matrices

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!

Count the elements in a list of a list(python)

[['s', 'a', 'b', 't'], ['s', 'c', 'd', 't'], ['s', 'c', 'e', 't','g']]
I want to count how many elements are in each list of the list.
so,[4,4,5].How can i do that in python?
Just use len for each element on the given list:
l = [['s', 'a', 'b', 't'], ['s', 'c', 'd', 't'], ['s', 'c', 'e', 't','g']]
[len(x) for x in l] # [4, 4, 5]
you can utilize the len() function. It takes a list as a parameter and returns the elements count.
python len function

Resources