Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
The community is reviewing whether to reopen this question as of 8 months ago.
Improve this question
// Here Is my Code
List data = [[0,1],[0,1],[0,1]];
List requiredList = [1,1,1];
To get the second value of the elements in the first list as second list you can do this:
List requiredList = data.expand((e)=>[e[1]]).toList();
Simply loop through the data and add the item in index 1 into output.
void main() async {
List data = [[0,1],[0,1],[0,1]];
List output = [];
for(final d in data){
output.add(d[1]);
}
print(output);
}
Output:
[1, 1, 1]
Your data contains lists, i.e to access data of list you have to pass index of list, i.e 0 1 2 etc.
List data = [[0,1],[0,1],[0,1]];
^ ^ ^
| | |
0 1 2
Above mentioned are index of each sub-list
So if you want to access data from sub-list, call the main list followed by index of sub-list and index number of elements of sub-list.
data[0][1] //this will give an output of 1
^ ^ ^
|. |. |
*. **. ***
*. => list containing lists here
** => Index of sub-list
*** => index of element in list
Check this video here to get more clarity of concept. I've attached a video related to python but same things apply in dart/flutter as well.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How can I create key/value-array pair in Scala. By this I mean in place of value I need an array.
val newRdd1 = rdd1.flatMap(x=>x.split(" "))
.map({case (key, Array(String)) => Array(String) })
You can achieve it using map(), it is similar in either plain scala program or Scala-in-SparkContext.
Example, you have a list of strings:
var sRec = List("key1,a1,a2,a3", "key2,b1,b2,b3", "key3,c1,c2,c3")
You can split it & convert to key/value(array of strings) assuming key is in 0th position, using:
sRec.map(x => (x.split(",")(0), Array(x.split(",")(1), x.split(",")(2), x.split(",")(3)))).
foreach(println)
(key1,[Ljava.lang.String;#7a81197d)
(key2,[Ljava.lang.String;#5ca881b5)
(key3,[Ljava.lang.String;#24d46ca6)
If you want to read a particular array element by key:
sRec.map(x => (x.split(",")(0),Array(x.split(",")(1), x.split(",")(2), x.split(",")(3)))).
map(x => (x._1, x._2(0))).foreach(println)
Output:
(key1,a1)
(key2,b1)
(key3,c1)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have the vector below:
v={'T','AT','AS','C'};
I would like to see all the possible permutations for this vector. To do so I can use the command below:
p=perms(v)
But I want to go one step further as each of the elements has sub index of 1 to 4, for example, T1,T2,T3,T4 .....C1,C2,C3,C4. I would like to have all the possible permutations with its sub index as see such results
T1,AT1,AS1,C1
C3,AT3,AS3,t3
AS2,AT2,C2,T2
.
.
.
Could you please help me how to do that?
Thanks
You can do this by first using ndgrid to generate a set of indices for all your possible combinations:
v = {'T1', 'AT1', 'AS1', 'C1'; ...
'T2', 'AT2', 'AS2', 'C2'; ...
'T3', 'AT3', 'AS3', 'C3'; ...
'T4', 'AT4', 'AS4', 'C4'};
[ind1, ind2, ind3, ind4] = ndgrid(1:4);
c = [v(ind1(:), 1) v(ind2(:), 2) v(ind3(:), 3) v(ind4(:), 4)];
And c will be a 256-by-4 cell array, as expected (44 combinations). Now you can expand each row by it's total number of permutations using perms like so:
p = perms(1:4);
p = reshape(c(:, p.').', 4, []).';
And p will be a 6144-by-4 cell array, also as expected (24 permutations times 256 combinations).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Assuming I have the following Scala classes:
Human(id: String, task: Task)
Task(id: String, time: Duration)
And having a List[(Human, Task)] with the following elements:
("H2", Task("T3", 5 minute))
("H3", Task("T1", 10 minute))
("H1", Task("T1", 10 minute))
("H1", Task("T2", 5 minute))
Now I want to functionally check if close elements have the same duration, and if so, order them by the human id.
In this case, the final list would have the elements sorted like so:
("H2", Task("T3", 5 minute))
("H1", Task("T1", 10 minute))
("H3", Task("T1", 10 minute))
("H1", Task("T2", 5 minute))
I tried to use sortBy to do so, but the way I'm doing, the final list will be fully ordered by the Human ID, not comparing the times.
Does anyone have any idea how can I do this?
Your question is a bit confused. You say you have a List of (Human,Task) tuples, but then you describe a collection of (String,Task) tuples.
Here's a way to sort a List[Human] according to the rules you've described.
def sortHumans(hs: List[Human]): List[Human] =
if (hs.isEmpty) Nil
else {
val target = hs.head.task.time
hs.takeWhile(_.task.time == target).sortBy(_.id) ++
sortHumans(hs.dropWhile(_.task.time == target))
}
This question already has answers here:
Removing Duplicates From Array of Custom Objects Swift
(4 answers)
Remove objects with duplicate properties from Swift array
(16 answers)
Closed 5 years ago.
I have an array
Contract object contains:
String:id
String:value
Array is:
contract1 = Contract.new()
contract1.id = 2
contract1.value = "Apple"
contract2 = Contract.new()
contract2.id = 2
contract2.value = "Pen"
contract3 = Contract.new()
contract3.id = 1
contract3.value = "Pineapple"
array = [Contract1, Contract2, Contract3]
I would to find out the list of contracts whose IDs are distinct.
I want to have a solution that doesn't make me change the implementation of my object (overriding the isEqual method etc) since I will be using it for more than one object through out my code.
Desired result:
[contract1, contract3] or [contract2, contract3]
Ideally, an extension with additionally a method to only return the values that are being made distinct:
Desired result: [2, 1]
I tried a couple of approaches from similar questions but either the answers are outdated or doesn't fit my need.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How many elements have a name longer than 10 characters in xlsx matlab?
I tried this.
[number cities] = xlsread('weather.xlsx', 'city')
if cities{1}<=char(10);
x+1=x;
Your code above only checks to see if the first city has less than 10 characters. Also, char(10) does not make any sense. You are checking to see if the string contained in cities{1} is less than or equal to the character array containing 10.
Because cities is in a cell array, I would use cellfun to first return the length of each city. Then you can use a sum as well as a Boolean condition to help you figure out how many there are that have more than 10 characters.
As such, here is the code:
A = cellfun(#length, cities);
numCitiesMoreThan10 = sum(A > 10);
Here is an example (simulated):
cities = {'New York', 'Los Angeles', 'Toronto', 'Ottawa', 'Sydney', 'Melbourne', ...
'Timbuktu', 'Singapore', 'Mississippi'};
A = cellfun(#length, cities);
numCitiesMoreThan10 = sum(A > 10);
>> numCitiesMoreThan10 =
2
This makes sense, as the only two cities with more than 10 characters (including spaces) are Los Angeles and Mississippi.
Aside
Just learned that cellfun has something built-in that can do this. You can also do:
A = cellfun('size', cities, 2);
This accesses each element in the cell array (cities), and returns the size of whatever dimension you specify in the last parameter of cellfun when you call it with the size parameter. You can also chain more than one cell array together. The reason why you are choosing the third parameter as 2 is because each string in the cell array is a 1 x N array. As such, we need to read how many columns there are so that this equates to the length of each string.