change values in last two lists of lists - python

I am looking for a solution to change the last two lists in a list of lists. The number of lists inside the list is variable. The change of the values depends on every time the last two lists.
list_of_colors=[['red','red','red','red'],['red','red','red','red'],
['red','red','red','red'], ['red','red','red','red']]
I expect the second to the last list changes completely from 'red' to 'lightgrey' and in the last list, only the last two from 'red' into 'lightgrey' - like this:
list_of_colors=[['red','red','red','red'],['red','red','red','red'],
['lightgrey','lightgrey','lightgrey','lightgrey'], ['red','red','lightgrey','lightgrey']]
This list of lists is to color a plotly table - Thanks for the help

For a more dynamic solution, i.e. in case the lists don't have a fixed length, you can try this:
list_of_colors[-2] = ['lightgrey'] * len(list_of_colors[-2])
last_n = 2
list_of_colors[-1][-last_n:] = ['lightgrey'] * last_n
In the last_n variable, I have specified the number of elements you wish to change, of the last list.

If I understood your question correctly, the parent list which contains the lists can have variable number of lists inside it, and you specifically know what values you require inside the last two lists, Then, this should work:
list_of_colors[-1] = ['lightgrey','lightgrey','lightgrey','lightgrey']
list_of_colors[-2] = ['red','red','lightgrey','lightgrey']

Related

Putting Result of Multiplication in a List

I am trying to put a series of multiplications inside a list, I am using the code below:
listx = []
for i in range (2):
list = [(3*i)]
listx.append(list)
The problem is that this will put the two results inside two separate lists inside a lists, I just wants the floats to be inside the first list.
listx = []
for i in range (2):
listx.append(3*i)
Just use this one. There is no need to create another list for storing the result. You created another list to store the value and appended that list into your listx
You can also use list comprehensions. Basically it's the same with for cycles but it's much shorter and for simpler operations like yours it's easier to read.
listx = [i*3 for i in range(2)]
This should produce a single list with values multiplied by 3 as integers

Combining three different list in python

Thank you for looking at my issue.
I'm trying to compare cells from three csv files to make sure they are exactly the same info. the cells in the csv can contain names, dates or ID numbers. All have to match.
compile = []
for a in Treader,Vreader,Dreader:
for b in a:
compile.append(b[0])
However, the number of variables will fluctuate and I don't want to keep adding index splicing every time. see "complie.append(b[0])" . The question now what way can I construct this to give me a random amount of variables and random number of indexes based on the length "len" of the original list. can i use the range function for that? not sure how i can create something like this.
The current question I have is
List = [[sally,john,jim], [sally,john,jim], [sally,john,jim]]
If I have the list above how could I get it to show
List =[sally,sally,sally]
List1 = [john,john,john]
List2 = [jim,jim,jim]
Also I want to be able to come up with unlimited number of list based on the length of this list that is inside the list. In this case its 3 for three names.
Some of my list has 30 some has 5 so its important I can assign it without having to type list1 to list 30 and manually assign each one.
you may use:
compile = list(zip(Treader,Vreader,Dreader))
this will create a list of tuples, a tuple will have like (sally,john,jim)
after your edit
you may use:
list(zip(*List))
output:
[('sally', 'sally', 'sally'), ('john', 'john', 'john'), ('jim', 'jim', 'jim')]

How to order a list based on what a function returns when called with its items

Basically, I need to order a 2D array. Genes is an array of 8 lists, all containing 8 items, all of which are floats. This is for an evolution simulator of sorts, hence 'genes'. My current solution is this:
scores = []
[scores.append(score(x)) for x in genes]
unsorted = genes
genes = [unsorted[0]]
for y in range(7):
for x in range(len(genes)):
if score(unsorted[y+1]) >= score(genes[x]):
genes.insert(x, unsorted[y+1])
break
I have a list of all the scores, I save a copy of 'genes' called 'unsorted', and set genes as the first item it once contained. The nested loop underneath should run through unsorted, taking each item through the 'x' loop, and inserting it into 'genes' once it finds the first item of score equal or smaller than its own. I thought this would work, but for some reason, it returns lists of random sizes, like 3, 2 and 5 or even 16. If you have a more efficient or pythonic way to do this, or just one that works, please help!
That is what sorted is for.
genes = sorted(genes, key=score)

Please tell this newbie what the difference is between these two list outputs?

I am struggling with something and have tracked it down to the difference between two lists inside my code: from the Python Debugger:
(Pdb) Values
['Thing1', 'Thing2', 'Thing3']
(Pdb) values2
[['Thing1', 'Thing2', 'Thing3']]
I do NOT want the double brackets, what does this mean and how do I get rid of them?
'Values' creation was by:
values = ['Thing1','Thing2','Thing3']
'Values2' creation was by:
for report in Report.objects.filter(id=id):
values2.append([str(report.name), str(report.subject), str(report.description)])
Why am I getting this difference and how can I get Values2 to look like Values ?
Don't think of it as "double brackets". Think of it as two sets of single brackets, one inside the other. A set of brackets means you have a list. Two sets of single brackets means you have two lists. One set of single brackets inside another means you have a list inside another list.
This is because the value you appended was a list, because you did values2.append([...]). The [...] is a list, so you appended a list; that is, you put a nested list inside values2.
If you don't want that, you could do:
values2.extend([str(report.name), str(report.subject), str(report.description)])
extend will add each element of the list as a separate element, instead of adding the whole list as one element. (Whether this will actually work in the larger context of your program depends on what you actually do with values2.)
values2 is a list with a single element that is a list.
You are appending a list to a list. What you want to do is extend the values2 list:
for report in Report.objects.filter(id=id):
values2.extend([str(report.name), str(report.subject), str(report.description)])
If you look at your loop
for report in Report.objects.filter(id=id):
values2.append( [ str(report.name), str(report.subject), str(report.description) ] )
you can see that what you are appending to values2 is the the list
[ str(report.name), str(report.subject), str(report.description) ]
so values2 - assuming it was empty in the first place - now has one element, being that list, and it looks like
[
['Thing1', 'Thing2', 'Thing3'],
]
as you described.
If you want to append the three strings instead then you have to do it one at a time, like this
for report in Report.objects.filter(id=id):
values2.append(str(report.name))
values2.append(str(report.subject))
values2.append(str(report.description))
or you could use extend instead, but I believe that would be less clear.

finding first item in a list whose first item in a tuple is matched

I have a list of several thousand unordered tuples that are of the format
(mainValue, (value, value, value, value))
Given a main value (which may or may not be present), is there a 'nice' way, other than iterating through every item looking and incrementing a value, where I can produce a list of indexes of tuples that match like this:
index = 0;
for destEntry in destList:
if destEntry[0] == sourceMatch:
destMatches.append(index)
index = index + 1
So I can compare the sub values against another set, and remove the best match from the list if necessary.
This works fine, but just seems like python would have a better way!
Edit:
As per the question, when writing the original question, I realised that I could use a dictionary instead of the first value (in fact this list is within another dictionary), but after removing the question, I still wanted to know how to do it as a tuple.
With list comprehension your for loop can be reduced to this expression:
destMatches = [i for i,destEntry in enumerate(destList) if destEntry[0] == sourceMatch]
You can also use filter()1 built in function to filter your data:
destMatches = filter(lambda destEntry:destEntry[0] == sourceMatch, destList)
1: In Python 3 filter is a class and returns a filter object.

Categories

Resources