list

tuple list from dict in python

How can I obtain a list of key-value tuples from a dict in python? Thanks ...

Efficiency of using a Python list as a queue

A coworker recently wrote a program in which he used a Python list as a queue. In other words, he used .append(x) when needing to insert items and .pop(0) when needing to remove items. I know that Python has collections.deque and I'm trying to figure out whether to spend my (limited) time to rewrite this code to use it. Assuming that ...

Merge and Update Two Lists in C#

I have two List<T> objects: For example: List 1: ID, Value where Id is populated and value is blank and it contains say IDs from 1 to 10. 1,"" 2,"" ... 10,"" List 2: ID, Value and other attributes all filled with values but this list is a subset of List 1 in terms of IDs. (e.g only 3 items) 2,67 4,90 5,98 What I want is a merged li...

How to handle add to list event?

I have a list like this: List<Controls> list = new List<Controls> How to handle adding new position to this list? When I do: myObject.myList.Add(new Control()); I would like to do something like this in my object: myList.AddingEvent+= HandleAddingEvent And than in my HandleAddingEvent delegate handling adding position to this l...

Multidimensional list(array) reassignment problem

Good day coders and codereses, I am writing a piece of code that goes through a pile of statistical data and returns what I ask from it. To complete its task the method reads from one multidimensional array and writes into another one. The piece of code giving me problems is: writer.variables[variable][:, :, :, :] = reader.variables[va...

How can I measure the performance of a HashTable in C#?

Hello everyone, I am playing around with C# collections and I have decided to write a quick test to measure the performance of different collections. My performance test goes like this: int numOps= (put number here); long start, end, numTicks1, numTicks2; float ratio; start = DateTime.Now.Ticks; for(int i = 0; i < numOps; i++) { /...

Getting a map() to return a list in python 3.1

Hello world, Im trying to map a list into hex, and then use the list elsewhere. In python 2.6, this was easy: A: python 2.6: >>> map(chr,[66,53,0,94]) ['B', '5', '\x00', '^'] However, on 3.1, the above returns a map object. B: python 3.1: >>> map(chr,[66,53,0,94]) <map object at 0x00AF5570> How do i retrieve the mapped list (as...

How to list variables declared in script in bash?

In my script in bash, there are lot of variables, and I have to make something to save them to file. My question is how to list all variables declared in my script and get list like this: VARIABLE1=abc VARIABLE2=def VARIABLE3=ghi Is there some EASY way for this? ...

Get all item from cursor in android

I use this code to get the item from cursor, but it just return one item on my list. So, how can I get all items to my list, this is my code? class MyAdapter extends SimpleCursorAdapter { private Context context; public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, t...

Python: Add list to set?

I have tried this on the python interpreter: >>> a=set('abcde') >>> a set(['a', 'c', 'b', 'e', 'd']) >>> l=['f','g'] >>> l ['f', 'g'] >>> a.add(l) Traceback (most recent call last): File "<pyshell#35>", line 1, in <module> a.add(l) TypeError: list objects are unhashable I think that I can't add the list to the set because there'...

Mapping a nested list with List Comprehension in Python?

I have the following code which I use to map a nested list in Python to produce a list with the same structure. >>> nested_list = [['Hello', 'World'], ['Goodbye', 'World']] >>> [map(str.upper, x) for x in nested_list] [['HELLO', 'WORLD'], ['GOODBYE', 'WORLD']] Can this be done with list comprehension alone (without using the map func...

Dynamic list slicing

Good day code knights, I have a tricky problem that I cannot see a simple solution for. And the history of the humankind states that there is a simple solution for everything (excluding buying presents) Here is the problem: I need an algorithm that takes multidimensional lists and a filter dictionary, processes them and returns lists ...

PLINQ problem / techniques to impliment multi-threaded, lock-free lists (in C#).

Here's the code in question: parentNodes.AsParallel().ForAll(parent => { List<Piece> plist = parent.Field.GetValidOrientations(pieceQueue[parent.Level]); plist.ForEach(p => { TreeNode child = new TreeNode(p, parent); var sco...

Sorted list not sorted when binding

I have a sorted list. When I bind it to a listbox, it does not shows the item in an ordered manner. territoryListBox.BeginUpdate(); this.Text = ((INamedEntity)_currentList[0]).Name; territoryListBox.DataSource = _currentList; territoryListBox.DisplayMember = "Name"; territoryListBox.Sorted = true; territoryListBox.EndUpdate(); The fir...

Most elegant way to extract data from multiple lists into a new one in Java?

This may be a little subjective, but I have often found that it can be very interesting to see how other developers approach certain daily details. I have code which works like this: class A { public List<SomeType> getOneSet() { ... } public List<SomeType> getAnotherSet() { ... } } class B { public static OtherType convert...

How to cast Generic Lists dynamically in C#?

I'm trying to cast List<object> to List<string> dynamically. I've tried several ways, but I can't find a solution. This is a small sample that shows the problem: List<object> listObject = new List<object>(); listObject.Add("ITEM 1"); listObject.Add("ITEM 2"); listObject.Add("ITEM 3"); List<string> listString = ¿¿listObject??; Thanks ...

JAVA Dynamic List Type

Is it possible to make a method return a dynamic List type. Such as a method a(Object b) can return a List<Integer> when the b is Integer type? ...

Python list filtering: remove subsets from list of lists.

Using Python how do you reduce a list of lists by an ordered subset match [[..],[..],..]? In the context of this question a list L is a subset of list M if M contains all members of L, and in the same order. For example, the list [1,2] is a subset of the list [1,2,3], but not of the list [2,1,3]. Example input: a. [[1, 2, 4, 8], [1, ...

What is the most efficient way to add an element to a list only if isn't there yet?

I have the following code in Python: def point_to_index(point): if point not in points: points.append(point) return points.index(point) This code is awfully inefficient, especially since I expect points to grow to hold a few million elements. If the point isn't in the list, I traverse the list 3 times: look for it a...

Combining two lists and removing duplicates, without removing duplicates in original list

I have two lists that i need to combine where the second list has any duplicates of the first list ignored. .. A bit hard to explain, so let me show an example of what the code looks like, and what i want as a result. first_list = [1, 2, 2, 5] second_list = [2, 5, 7, 9] # The result of combining the two lists should result in this lis...