[Solved] I want to know the count of each item in a list

You can use GroupBy: var voteGroups = Votes.GroupBy(s => s); Now, if you want to know the count of each string use Enumerable.Count: foreach(var voteGroup in voteGroups) Console.WriteLine(“Vote:{0} Count:{1}”, voteGroup.Key, voteGroup.Count()); Another way is using ToLookup: var voteLookup = Votes.ToLookup(s => s); foreach (var voteGroup in voteLookup) Console.WriteLine(“Vote:{0} Count:{1}”, voteGroup.Key, voteGroup.Count()); The lookup has it’s … Read more

[Solved] Extracting objects from a list by quantity [closed]

If you are using java-8 or later then List<Drink> list = new ArrayList<>(); list.add(new Drink(“coke”, 30)); list.add(new Drink(“fanta”, 10)); list.add(new Drink(“coke”, 5)); list.add(new Drink(“sprite”, 1)); list.add(new Drink(“coke”, 10)); Map<String, Integer> map = list.stream() .collect(Collectors.groupingBy(Drink::getName, Collectors.summingInt(Drink::getAmount))); System.out.println(map); output {sprite=1, coke=45, fanta=10} Collection<Drink> c = list.stream().collect(Collectors.groupingBy(Drink::getName, Collectors.collectingAndThen( Collectors.reducing((Drink a, Drink b) -> { a.setAmount(a.getAmount() + b.getAmount()); return … Read more

[Solved] How can i copy elements in list to an array c/c++

is that if there is anyway to copy those elements from list to an array Yes, there is a way to copy objects from a container to another. You can use the following algorithm: get output iterator to the output container get input iterator to first element of the input container while input iterator does … Read more

[Solved] How to get a list in below format? [closed]

Use strip and split as below to get the value separated as you want. file = open(‘file.txt’) my_arr = [] for line in file: fields = line.strip().split() my_arr.append([fields[0], “https://stackoverflow.com/”, fields[1][1:]]) print(my_arr) Output: [[‘22882367’, “https://stackoverflow.com/”, ‘pgc-orc-hive-output’], [‘13454914’, “https://stackoverflow.com/”, ‘pqs’], [‘2254110952’, “https://stackoverflow.com/”, ‘processed-nrt-export’]] 3 solved How to get a list in below format? [closed]

[Solved] How to get the name of List [closed]

If what you mean is the name of the variable that was used when the list was passed to the method like this: static List<string> GatherDataPerProduct(List<Pandora.Data.DomainObject> lstdata) { if(lstData.value == “subjects”) { //do whatever } List<DomainObject> subjects; GatherDataPerProduct(subjects); then what you’re trying to do is not possible. The reason is that upon compilation, the compiler … Read more

[Solved] Python .join() format error

It was already said that the delimiter is inserted between each element not added to each element. However you can easily create a new list with the correctly added bear, for example: >>> lst = [‘brown’, ‘polar’, ‘grizzly’, ‘sun’, ‘kodiak’] >>> newlst = [item + ‘ bear’ for item in lst] >>> print(‘\n’.join(newlst)) brown bear … Read more

[Solved] Reshaping a weird list in python [duplicate]

IIUC, given array = [[[0,0,1,0],[0,0,0,1],[1,0,0,0],[0,1,0,0]], [[0,1,0],[1,0,0],[0,0,1],[0,0,1]], [[0],[1],[0],[1]], [[1],[1],[0],[1]]] Do import numpy as np >>> np.array(array).T array([[list([0, 0, 1, 0]), list([0, 1, 0]), list([0]), list([1])], [list([0, 0, 0, 1]), list([1, 0, 0]), list([1]), list([1])], [list([1, 0, 0, 0]), list([0, 0, 1]), list([0]), list([0])], [list([0, 1, 0, 0]), list([0, 0, 1]), list([1]), list([1])]], 2 solved Reshaping a … Read more

[Solved] Sorting strings into dictionary, where initial char is the key and the value is a list of all lines starting with that char

def main(): # I’m assuming you can get this far… lines = [ ‘1,some stuff 1’, ‘2,some stuff 2,more stuff’, ‘2,some stuff 4,candy,bacon’, ‘3,some stuff 3,this,is,horrible…’ ] # Something to hold your parsed data data = {} # Iterate over each line of your file for line in lines: # Split the data apart on … Read more

[Solved] python: index of list1 equal to index of list2 [duplicate]

well what the comments said and change it like the following too : id = [1, 2, 3, 4, 5, 6] name = [‘sarah’, ‘john’, ‘mark’, ‘james’, ‘jack’] userid = int(input(‘enter user ID: ‘)) if userid in id: ind = id.index(userid) #notice this statement is inside the if and not outside print(name[ind]) else: print(“wrong id”) … Read more