How to generate the random values of map from a given set of values and then store the key and values into separate variables in scala - list

I am trying to generate the 1000 random key-val map pairs from the given(statically defined) 2 map key-val pairs in scala and also later i would want to break the key and value pairs and store them into separate variables
Whatever i have tried:
object methodTest extends App{
val testMap = Map("3875835"->"ABCDE","316067107"->"EFGHI")
def getRandomElement(seq: Map[String,String]): Map[String,String] = {
seq(Random.nextInt(seq.length))
}
var outList = List.empty[Map[String,String]]
for(i<-0 to 1000){
outList+=getRandomElement(testMap)
}
print(outList)
}
The Output should generate 1000 Key-val pairs of map like i am showing below
[3875835,ABCDE]
[316067107,EFGHI]
[3875835,ABCDE]
[316067107,EFGHI]
[316067107,EFGHI]
............
............
............. upto 1000 random key-val pairs
Please help me figure out where i am going wrong and let me know How to resolve it, if any issue regarding the requirement, please feel free to comment for it

You can transform your seed map testMap into sequence of key/value tuples using .toSeq and then generate key/value pairs by iterating over the list of numbers from 0 until 1000, associating each number to a random choice between first or second element of the seed:
import scala.util.Random
val testMap = Map("3875835" -> "ABCDE", "316067107" -> "EFGHI")
val seed = testMap.toSeq
val keyValuesList = (0 until 1000).map(index => seed(Random.nextInt(seed.size)))
Note: 0 until 1000 will return all the numbers from 0 to 1000 excluded, so 1000 numbers. If you use 0 to 1000 you will get all the numbers from 0 to 1000 included, so 1001 numbers.
If you want to print the resulting list, you can use .foreach method with println function as argument:
keyValuesList.foreach(println)
And you will get:
(3875835,ABCDE)
(316067107,EFGHI)
(3875835,ABCDE)
(3875835,ABCDE)
(316067107,EFGHI)
(3875835,ABCDE)
...
if you want to keep only the keys, you can iterate on the list using .map method, taking only the first element of the tuple by using ._1 method, that retrieve the first element of a tuple:
val keys = keyValuesList.map(keyValuePair => keyValuePair._1)
And if you want only a list containing all second elements of each pair:
val values = keyValuesList.map(keyValuePair => keyValuePair._2)

Related

How to iterate through single <key, value> pairs of List in dart

I have 2 Lists (uid and url) that are growable, and I need to set the first List as the key and the second as value. At some point, i'll have a 3rd List (randomUids) which will be keys and will print out the corresponding values. Here is the example code:
List<String> uid = ["uid1", "uid2","uid3","uid4"]; //Lists will grow larger after a while
List<String> url = ["url1","url2","url3","url4"];
List<String> randomUids = ["uid4", "uid2"];
When I try:
Map<List, List> mapKeyValue = Map();
mapKeyValue[uid] = url;
print( uid.contains(randomUids));
I get a false. Also, the print returns uid and url Lists as 2 long indices instead of separate Strings. How can I iterate the List so that url.contains(randomUids) is true. Also how can I print out the values of randomUids.
When I try:
print( uid.contains(randomUids));
I get a false.
Your code asks if uid (a List of Strings) contains randomUids (another List of Strings). It returns false because uid's elements are not Lists; they're Strings.
Presuming that you want the nth element of uid to correspond to the nth element of url, and you can guarantee that uid.length == url.length, you can construct a Map of UIDs to URLs:
assert(uid.length == url.length);
var uidMap = <String, String>{
for (var i = 0; i < uid.length; i += 1)
uid[i]: url[i],
};
And then you can iterate over randomUids and do lookups:
for (var uid in randomUids) {
if (uidMap.containsKey(uid)) {
print(uidMap[uid]);
}
}

Filter more times in a Java 8 Stream

Is it possible to filter more times in a Stream? For example if I have a list with IDs and I want to Stream a HashMap and map the key of the HashMap to key in a list and where they are match, I want to get the object from the HashMap and filter it again for example an int field in that object which is greater than 3 for example and sum them in the end. For example if its found 10 case where the list's key and HashMap's key are equal and it's filter those 10 cases and founds 3 case where for example an int field greater then 3 it gives back a sum of these in the end.
Here is my code so far:
When i try to print the list of the result of this i get this:
java.util.stream.ReferencePipeline$2#70177ecd
somemap.entrySet().stream().filter(e -> aListContainingIds.contains(e.getKey()))
.map(Map.Entry::getValue)
.map(n -> n.getTheOtherListFromMatchedValue().stream().filter(n -> n.getAnIntFieldFromObject() > 3))
.collect(Collectors.toList());
I think you should use Stream.flatMaptoInt (or Stream.flatMaptoLong) instead of just map:
int total = somemap.entrySet().stream()
.filter(e -> aListContainingIds.contains(e.getKey()))
.map(Map.Entry::getValue)
.flatMapToInt(value -> value.getTheOtherListFromMatchedValue().stream())
.filter(n -> n.getAnIntFieldFromObject() > 3)
.sum();
In general, flatMapToInt (or flatMap if the stream elements are instances of some class instead of primitives) is to be used when the function you are applying returns another stream, and you want the elements of that stream to be part to the original stream.
At the end, you can get the total with the IntStream.sum method.

Take first N keys by max value from dict {key:list}

is there have easy way to take first N keys which have max value from they list in dict {key:list}
is there have easy way to take first N keys which have max value from they list in dict {key:list}
def main():
for x in range(len(sale10k)):
timelist.append(sale10k[x][3])
pricesList.append(sale10k[x][4])
if sale10k[x][0] in salesByCategory.keys():
salesByCategory[sale10k[x][0]].append(float(sale10k[x][4]))
else:
salesByCategory[sale10k[x][0]]=[]
salesByCategory[sale10k[x][0]].append(float(sale10k[x][4]))
salesByCategory1={}
for key,value in salesByCategory.items():
salesByCategory1[key]=sum(salesByCategory.get(key))
#fiveLarges=heapq.nlargest(5,salesByCategory1,key=salesByCategory1.get)
salesBycatalog={}
for y in range(len(catalog)):
salesBycatalog[catalog[y][0]]=catalog[y][5]
totalByGroup={}
for key, value in salesBycatalog.items():
if value in totalByGroup.keys():
totalByGroup[value].append(salesByCategory1.get(key))
else:
totalByGroup[value]=[]
totalByGroup[value].append(salesByCategory1.get(key))
print(totalByGroup)
if __name__ == "__main__":
main()
i have 2 files excel.cvs
my output from now is this :
{'POLO SHIRTS': [2609.76, 13339.109999999991, 15622.410000000007], 'APPAREL ACCESSORIES': [22596.24999999999, 20901.099999999995, 31007.8], 'PANTS': [8031.729999999998, 11179.949999999999, 5405.839999999997, 9023.949999999999, 21523.819999999996, 26030.800000000017], 'FOOTWEAR ACCESSORIES': [8686.369999999999], 'GLIDING SP.EQUIPMENT': [22136.399999999987, 27678.920000000006, 14222.21999999999, 30013.37000000001], 'SHOES': [1903.66, 25443.21999999999, 22152.530000000006, 11585.410000000002, 38504.679999999986, 7787.670000000004, 10256.860000000002, 1377.1199999999997, 15459.799999999992, 20919.56000000001, 6299.769999999996, 1555.4499999999998, 17470.460000000006, 29361.220000000034, 4070.9000000000033, 27045.450000000004, 20721.829999999994, 780.55, 24671.590000000015, 13189.570000000002, 6442.700000000001, 6105.390000000005, 12701.659999999998, 29418.89000000001, 7295.620000000001, 26344.420000000002, 3262.12, 11710.460000000006, 3272.2999999999993, 17055.989999999994, 9019.77, 12722.570000000003, 20020.150000000005, 30164.860000000026, 17513.14, 3168.6200000000003, 27008.24, 14585.679999999988, 15273.48, 24172.329999999998, 33968.96000000003, 35480.790000000015, 25150.459999999992, 24207.679999999997, 26909.090000000007, 17692.079999999998, 27844.97999999999, 33847.389999999985, 13266.239999999994, 11757.349999999997, 24469.410000000018, 8214.879999999997, 3966.6899999999964, 5336.910000000003, 27766.659999999978, 24636.97000000002, 21330.829999999994, 10331.680000000004, 19769.529999999995, 20764.439999999984, 2873.509999999999, 23263.23, 15127.240000000003, 13282.320000000003, 32917.03000000001, 17657.12, 9959.55, 21052.779999999995, 16015.79, 2667.2699999999995, 16041.830000000004, 2309.9000000000005, 8095.450000000001, 23628.889999999985, 3846.259999999999, 6795.61, 14608.109999999995, 6422.360000000001, 3241.279999999999, 19220.27999999999, 20836.899999999994, 28446.07000000001, 13984.979999999992, 10006.460000000003, 14417.309999999998, 9069.470000000001, 8081.38, 1766.8899999999999, 19041.750000000004, 3310.279999999999, 3649.49, 11089.069999999994, 10946.420000000002, 16297.91, 3788.1000000000004, 27356.640000000007, 14024.480000000001, 29409.03], 'SUITS': [28587.990000000016, 14337.800000000001], 'BALLS': [25855.07, 15207.729999999992, 25567.809999999987, 8428.509999999998, 15119.609999999995, 26069.969999999983, 29843.490000000023], 'TOPS': [1673.2000000000005, 8673.400000000001, 23610.79999999999, 2090.380000000001], 'HEADWEAR': [2075.3000000000015, 18891.799999999996, 39717.93, 33657.65, 9965.720000000005, 12030.020000000006, 670.9999999999999, 12694.720000000007, 24846.22000000001, 1606.1799999999994, 9993.330000000002, 10154.900000000005], 'HARDWARE ACCESSORIES': [14619.109999999997], 'OTHER SHIRTS': [18013.450000000004], 'PROTECTION GEAR': [26454.929999999997], 'JERSEYS': [23741.06, 38425.269999999975], 'SANDALS/SLIPPERS': [9103.83, 21025.040000000005, 12702.349999999999, 26766.439999999984, 29818.339999999993], 'SHORTS': [14817.77, 29540.92999999998, 9415.059999999996, 14582.480000000001], 'JACKETS': [30096.11000000001, 13372.469999999998, 31145.73000000001, 6011.17, 12225.300000000003, 23485.399999999998, 13889.96], 'SWIMWEAR': [14035.140000000001, 20232.629999999997, 5142.340000000001, 2945.349999999998, 23495.320000000003, 8207.920000000004, 11972.729999999994], 'T-SHIRTS': [11130.700000000004, 8315.83, 8346.719999999998, 27847.550000000007, 22704.759999999995, 7828.200000000002, 17823.379999999997, 2248.46, 9012.14, 7774.72, 12030.049999999996, 4207.649999999999, 21293.16, 3159.4700000000007, 13385.12, 30507.87], 'UNDERWEAR': [10419.31, 31017.909999999993, 2794.590000000002, 18625.990000000005, 21829.879999999994], 'SWEATSHIRTS': [4317.6799999999985, 23453.049999999985, 28176.49000000001], 'TIGHTS': [23823.43999999999, 11180.129999999996], 'BAGS': [13980.240000000007, 18509.50999999999, 20064.309999999998, 22317.360000000004, 17641.04]}
i need this :
SHOES: 1519077.15 €
T-SHIRTS: 207615.78 €
HEADWEAR: 176304.77 €
BALLS: 146092.19 €
JACKETS: 130226.14 €
I have data stored in dict orderBygroup {key-list(of float values)} and need to take first 5 keys with max value.
My second question is - dict salesByCategory1 is make with loping to salesByCategory and sum of all values to receive the total for article number.
Can i get that totals with some smartes way ?
is there have easy way to make that output ?
totalByGroup1={}
for key,value in totalByGroup.items():
totalByGroup1[key]=sum(totalByGroup.get(key))
Create a new dictionary with summed elements. More resources.
sorted5=sorted(totalByGroup1, key=totalByGroup1.get, reverse=True)[:5]
print(sorted5)
sorting and taking the first 5 elements
output is : ['SHOES', 'T-SHIRTS', 'HEADWEAR', 'BALLS', 'JACKETS']
more time, more resurses
for key in sorted5:
print(key,': ','{0:.2f}'.format(totalByGroup1.get(key)))
and now result :
SHOES : 1519077.15
T-SHIRTS : 207615.78
HEADWEAR : 176304.77
BALLS : 146092.19
JACKETS : 130226.14
now lets ask again if we have 4 record in dict wit that data:
a:[1,2,3],b:[6,7,8],c:[4,5,6],d:[9,10,11]
how to get first 2 key,value sorted by max value -->>
d:30,b:21
if we have 1000 record in dict - ?? how to get first N key sorted by max value of list
example go:1563,do:1560,bo:1490,ro:1480 .. etc

Insertion order of a list based on order of another list

I have a sorting problem in Scala that I could certainly solve with brute-force, but I'm hopeful there is a more clever/elegant solution available. Suppose I have a list of strings in no particular order:
val keys = List("john", "jill", "ganesh", "wei", "bruce", "123", "Pantera")
Then at random, I receive the values for these keys at random (full-disclosure, I'm experiencing this problem in an akka actor, so events are not in order):
def receive:Receive = {
case Value(key, otherStuff) => // key is an element in keys ...
And I want to store these results in a List where the Value objects appear in the same order as their key fields in the keys list. For instance, I may have this list after receiving the first two Value messages:
List(Value("ganesh", stuff1), Value("bruce", stuff2))
ganesh appears before bruce merely because he appears earlier in the keys list. Once the third message is received, I should insert it into this list in the correct location per the ordering established by keys. For instance, on receiving wei I should insert him into the middle:
List(Value("ganesh", stuff1), Value("wei", stuff3), Value("bruce", stuff2))
At any point during this process, my list may be incomplete but in the expected order. Since the keys are redundant with my Value data, I throw them away once the list of values is complete.
Show me what you've got!
I assume you want no worse than O(n log n) performance. So:
val order = keys.zipWithIndex.toMap
var part = collection.immutable.TreeSet.empty[Value](
math.Ordering.by(v => order(v.key))
)
Then you just add your items.
scala> part = part + Value("ganesh", 0.1)
part: scala.collection.immutable.TreeSet[Value] =
TreeSet(Value(ganesh,0.1))
scala> part = part + Value("bruce", 0.2)
part: scala.collection.immutable.TreeSet[Value] =
TreeSet(Value(ganesh,0.1), Value(bruce,0.2))
scala> part = part + Value("wei", 0.3)
part: scala.collection.immutable.TreeSet[Value] =
TreeSet(Value(ganesh,0.1), Value(wei,0.3), Value(bruce,0.2))
When you're done, you can .toList it. While you're building it, you probably don't want to, since updating a list in random order so that it is in a desired sorted order is an obligatory O(n^2) cost.
Edit: with your example of seven items, my solution takes about 1/3 the time of Jean-Philippe's. For 25 items, it's 1/10th the time. 1/30th for 200 (which is the difference between 6 ms and 0.2 ms on my machine).
If you can use a ListMap instead of a list of tuples to store values while they're gathered, this could work. ListMap preserves insertion order.
class MyActor(keys: List[String]) extends Actor {
def initial(values: ListMap[String, Option[Value]]): Receive = {
case v # Value(key, otherStuff) =>
if(values.forall(_._2.isDefined))
context.become(valuesReceived(values.updated(key, Some(v)).collect { case (_, Some(v)) => v))
else
context.become(initial(keys, values.updated(key, Some(v))))
}
def valuesReceived(values: Seq[Value]): Receive = { } // whatever you need
def receive = initial(keys.map { k => (k -> None) })
}
(warning: not compiled)

How to save multiple arguments to a Map?

I am using arguments from command line which is in the form of an Array and I would like to convert that into a Map.
So for example when I run my code with scala abc.scala A 10 B 20 C 30 I want to have a Map(A->10, B->20, C->30). Also I can use only val so I cannot reassign it because it is immutable. I am using the following piece of code unsuccessfully:
val names = args.filter(x => for(i <- 0 to args.length-1) i%2==0)
val numbers = args.partition(args(i) => i%2==1)
names.zip(numbers).toMap
You want grouped:
args.grouped(2).map { case Array(n,v) => (n,v) }.toMap
You're using for in completely the wrong way. That runs a new iteration every element of your args. If you want an index that you can work off of, try args.zipWithIndex (which pairs an index with each element). Alternatively, look at args.grouped(2).