Flutter List Behaviour is not as expected - list

I am making a filtering system in flutter by using two lists called mailList and filteredMailList. The first time mailList is populated, this code runs:
setState(() {
filteredMailList.clear();
filteredMailList = mailList;
});
When this has been done, it seems as though any changes that I make on mailList get replicated to filteredMailList even though I haven't executed those two lines again. For example:
//Assume that mailList = ["1", "2", "3"] and filteredMailList = []
filteredMailList = mailList
//I expect then mailList = ["1", "2", "3"] and filteredMailList = ["1", "2", "3"]
mailList.add("4")
//I expect then mailList = ["1", "2", "3", "4"] and filteredMailList = ["1", "2", "3"]
//However when this is run mailList = ["1", "2", "3", "4"] and filteredMailList = ["1", "2", "3", "4"]
Does this have something to do with the fact that I might not be giving both lists the same contents, but instead saying that they are referencing the same list?
Is there a way to make filteredMailList only contain the elements of mailList and not actually reference the exact same list, so each one can still be edited independently?

in your code by calling filteredMailList = mailList; you are actually removing your original filteredMailList List (you don't have any refrence to that list anymore) and changing it to the mailList. so both filteredMailList and mailList points to a single List in memory
you can create a clone list from mailList like :
filteredMailList = [...mailList]; // or filteredMailList = mailList.toList();
or keep your original refrence and copy all items from mailList
filteredMailList.clear();
filteredMailList.addAll(mailList);

Related

Sort a nested structure in Coldfusion

I am trying to sort a nested struct using StructSort. Below is a simple example of what I am trying to do. The code below does not work and returns the following error "The specified element a does not contain a simple value." Is it possible to sort a nested structure in Coldfusion, and if so how?
<cfscript>
data = {"a":{"name":100},"b":{"name":50},"c":{"name":25},"d":{"name":75}};
dataSorted= StructSort(data, function(a,b) {
return compare(a.name, b.name);
});
writeDump(dataSorted);
</cfscript>
expected output
c
b
d
a
Also made a cffiddle here: https://cffiddle.org/app/e20a782a-be90-4e65-83de-e31eb83fdf4f
Docs: https://docs.lucee.org/reference/objects/struct/sort.html
<cfscript>
data = {
"a": {"name": 100},
"b": {"name": 50},
"c": {"name": 25},
"d": {"name": 75}
};
dataSorted = data.sort("numeric", "asc", "name")
writeDump(dataSorted);
</cfscript>
Result: array ["c", "b", "d", "a"]
This worked for me on lucee 5. something. The last argument in the sort function is the pathToSubElement within a struct. Fo your example it is simply one level deep using the name property.

Dropping rows in a dataframe

Suppose we have this dataframe:
a = [["1", "2"], ["1", "3"], ["2", "1"]]
data = pd.DataFrame(a, columns=["one", "two"])
I need a result like:
a = [["1", "2"], ["1", "3"]]
My question is how to drop this row (the third row)? Because here these two rows (first and third) are not the same but they have the same values in different columns.

How do I swap some characters of a String with values of a HashMap<String,String> in Kotlin?

Assuming I have a
val s: String = "14ABC5"
and have a HashMap
val b: HashMap<String,String> = hashMapOf("A" to "10", "B" to "11", "C" to "12", "D" to "13", "E" to "14", "F" to "15" )
How would I change all occurrences of A,B,C with 10, 11, 12 while keeping their order ("1", "4", "10", "11", "12", "5")?
So far I have this
val result: List<String> = s.toUpperCase().toCharArray().map{ it.toString() }.map{ it -> b.getValue(it)}
which works if ALL characters of the String exist in the HashMap but my String may contain inexistent keys as well.
You could either use getOrDefault(...), or the Kotlinesque b[it] ?: it.
By the way, if you're using the implicit lambda argument name (it), you can get rid of the it ->.
You can use the String as an iterable by default and simplify your code as follows:
s.map { it.toString() }
.map { b[it] ?: it }

Python list and for loop

I'm expecting this code to print spade:A spade:2 and so on until heart:K.
But it only does heart:A to heart:K.
How should I do it?
symbols = ["spade", "clover", "diamond", "heart"]
numbers = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
cards = {}
for num in numbers:
for symbol in symbols:
cards[num] = symbol
print cards
Use your itertools toolbox
import itertools
symbols = ["spade", "clover", "diamond", "heart"]
numbers = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
combinations = itertools.product(symbols, numbers)
cards = ["{}:{}".format(suit, rank) for suit,rank in combinations]
This will give you the list:
['spade:A',
'spade:2',
'spade:3',
'spade:4',
'spade:5',
'spade:6',
'spade:7',
'spade:8',
'spade:9',
'spade:10',
'spade:J',
'spade:Q',
'spade:K',
'clover:A',
'clover:2',
'clover:3',
'clover:4',
'clover:5',
'clover:6',
'clover:7',
'clover:8',
'clover:9',
'clover:10',
'clover:J',
'clover:Q',
'clover:K',
'diamond:A',
'diamond:2',
'diamond:3',
'diamond:4',
'diamond:5',
'diamond:6',
'diamond:7',
'diamond:8',
'diamond:9',
'diamond:10',
'diamond:J',
'diamond:Q',
'diamond:K',
'heart:A',
'heart:2',
'heart:3',
'heart:4',
'heart:5',
'heart:6',
'heart:7',
'heart:8',
'heart:9',
'heart:10',
'heart:J',
'heart:Q',
'heart:K']
The problem is that you are not iterating the right way and thus you are not appending in the list. The right way to do it is
symbols = ["spade", "clover", "diamond", "heart"]
numbers = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
cards = []
for j in range(len(symbols)):
for i in range(len(numbers)):
cards.append(str(symbols[j]+':'+str(numbers[i])))
print cards
with output:
['spade:A', 'spade:2', 'spade:3', 'spade:4', 'spade:5', 'spade:6', 'spade:7', 'spade:8',
'spade:9', 'spade:10', 'spade:J', 'spade:Q', 'spade:K', 'clover:A', 'clover:2',
'clover:3', 'clover:4', 'clover:5', 'clover:6', 'clover:7', 'clover:8', 'clover:9',
'clover:10', 'clover:J', 'clover:Q', 'clover:K', 'diamond:A', 'diamond:2', 'diamond:3',
'diamond:4', 'diamond:5', 'diamond:6', 'diamond:7', 'diamond:8', 'diamond:9', 'diamond:10',
'diamond:J', 'diamond:Q', 'diamond:K', 'heart:A', 'heart:2', 'heart:3', 'heart:4',
'heart:5', 'heart:6', 'heart:7', 'heart:8', 'heart:9', 'heart:10', 'heart:J', 'heart:Q', 'heart:K']
Made with Ipython Notebook in python 2.7
Hope it helps.
You are iterating the symbols just fine but when you are going over the numbers in the second loop, you are actually replacing the values set by the previous loop hence you only have values from the last loop left and everything is replaced. This means cards["A"] value is set 4 times in the loop and the last for the "heart" is retained. The same thing is happening for all the other indexes.

how to show data in row on Stacked bar chat by using MS Charts Control

I need to show the data from a SQL query as shown below on stacked bar chart by using MS Chart control on ASPX page
Where I have to show the chart like this
</asp:Chart>
You really should show some code if you want help, but here's a simple vb.net solution.
I added a chart to the page and then ran the following code to produce the below chart
Create your datatable - you can drop this and connect with SQL
Dim t As New DataTable
t.Columns.Add("Risk Categories")
t.Columns.Add("High Impact Risks")
t.Columns.Add("Medium Impact Risks")
t.Columns.Add("Low Impact Risks")
t.Columns.Add("No Impact Risks")
t.Rows.Add("Compliance,Law,Legislation", "4", "1", "0", "5")
t.Rows.Add("Construction", "5", "1", "1", "0")
t.Rows.Add("Design", "3", "1", "0", "0")
t.Rows.Add("Financial", "6", "0", "0", "2")
t.Rows.Add("Human Resources", "2", "0", "0", "10")
t.Rows.Add("Information & Communication", "1", "0", "0", "1")
t.Rows.Add("Interface", "1", "0", "0", "0")
t.Rows.Add("Logistic", "0", "1", "0", "6")
t.Rows.Add("Management", "0", "1", "0", "0")
t.Rows.Add("Planning", "3", "0", "0", "1")
Add the points to the new chart
Chart1.Series(0).ChartType = SeriesChartType.StackedBar
With Chart1.Series(0)
.Name = "High Impact Risks"
.Points.DataBind(t.DefaultView, "Risk Categories", "High Impact Risks", Nothing)
End With
Chart1.Series.Add("Medium Impact Risks")
Chart1.Series(1).ChartType = SeriesChartType.StackedBar
With Chart1.Series(1)
.Name = "Medium Impact Risks"
.Points.DataBind(t.DefaultView, "Risk Categories", "Medium Impact Risks", Nothing)
End With
Chart1.Series.Add("Low Impact Risks")
Chart1.Series(2).ChartType = SeriesChartType.StackedBar
With Chart1.Series(2)
.Points.DataBind(t.DefaultView, "Risk Categories", "Low Impact Risks", Nothing)
End With
Chart1.Series.Add("No Impact Risks")
Chart1.Series(3).ChartType = SeriesChartType.StackedBar
With Chart1.Series(3)
.Points.DataBind(t.DefaultView, "Risk Categories", "No Impact Risks", Nothing)
End With
''Show All Categories on RHS axis
Chart1.ChartAreas(0).AxisX.Interval = 1
Chart1.ChartAreas(0).AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont
''Move legend to bottom and center
Chart1.Legends(0).Docking = Docking.Bottom
Chart1.Legends(0).Alignment = StringAlignment.Center