I have already build a dictionary.Here is my dictionary:  mydict = {'to': 12, 'and': 35, 'Python': 26, 'for': 49}   I want to sort this dictionary by value (from max to min) and print result like this:  for 49 and 35 Python 26 to 12   I don't know how to do it.Any help will be appreciated! I have tried:    (1)  for key,value in mydict.items():         print(key,sorted(mydict.values(),reverse=True))  #int is not iterable   (2)use dict.keys() and dict.values(), but I don't know how to print correspond keys.  (3)I tried to build a list for value and build a new dictionary from value's list.But I don't know how to do it.  (4)try to convert int to string,but it can't be converted! Thank you so much!
4557
I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.  I can sort on the keys, but how can I sort based on the values?  Note: I have read Stack Overflow question here  and probably could change my code to have a list of dictionaries, but since I do not really need a list of dictionaries I wanted to know if there is a simpler solution to sort either in ascending or descending order.
106
I need a good explanation (references are a plus) on Python's slice notation.   To me, this notation needs a bit of picking up.   It looks extremely powerful, but I haven't quite got my head around it.
1512
I am looking for a generic, bidirectional 1 to 1 Dictionary class in C# (2), ie. a BiDictionaryOneToOne&lt;T, S&gt; which is guaranteed to only contain one of each value and key (up to RefEquals anyway), and which can be searched using either key or value. Anyone know of one, or should I just implement it myself? I can't believe that I'm the first person to need this...  There is a BiDictionary in the answers to , but it is not for unique elements (and also does not implement RemoveByFirst(T t) or RemoveBySecond(S s)).   Thanks!
90263
I have a text column. It's made up of the numbers 1, 2, 3 up to 1650, which are currently not in increasing order. However, it also includes text such as 520a, 639a, 639b, 639c. Sorting on increasing value would produce 1, 10, 100, 1000, etc, rather than 1, 2, 3, ... I know how to convert from text to integer, which would work if my cells did not include alpha characters. How can I sort to end up with 1, 2, 3, ..., 10, ..., 100, ..., 519, 520a, 521, ..., 638, 639a, 639b, 639c, 640, ..., 1650?
19201
What is the best way to extend a dictionary with another one while avoiding the use of a for loop? For instance: &gt;&gt;&gt; a = { &quot;a&quot; : 1, &quot;b&quot; : 2 } &gt;&gt;&gt; b = { &quot;c&quot; : 3, &quot;d&quot; : 4 } &gt;&gt;&gt; a {'a': 1, 'b': 2} &gt;&gt;&gt; b {'c': 3, 'd': 4}  Result: { &quot;a&quot; : 1, &quot;b&quot; : 2, &quot;c&quot; : 3, &quot;d&quot; : 4 }  Something like: a.extend(b)  # This does not work
95963
Let's say that I have two arrays (in Java),  int[] numbers; and int[] colors;  Each ith element of numbers corresponds to its ith element in colors. Ex, numbers = {4,2,1}      colors = {0x11, 0x24, 0x01}; Means that number 4 is color 0x11, number 2 is 0x24, etc.  I want to sort the numbers array, but then still have it so each element matches up with its pair in colors.  Ex. numbers = {1,2,4};     colors = {0x01,0x24,0x11};  What's the cleanest, simplest way to do this? The arrays have a few thousand items, so being in place would be best, but not required. Would it make sense to do an Arrays.sort() and a custom comparator? Using library functions as much as possible is preferable.  Note: I know the "best" solution is to make a class for the two elements and use a custom comparator. This question is meant to ask people for the quickest way to code this. Imagine being at a programming competition, you wouldn't want to be making all these extra classes, anonymous classes for the comparator, etc. Better yet, forget Java; how would you code it in C?
32327
All the documentation I've found so far is to update keys that are already created:  arr['key'] = val;  I have a string like this: &quot; name =  oscar &quot;  And I want to end up with something like this: { name: 'whatever' }  That is, split the string and get the first element, and then put that in a dictionary. Code var text = ' name = oscar ' var dict = new Array(); var keyValuePair = text.split(' = '); dict[ keyValuePair[0] ] = 'whatever'; alert( dict ); // Prints nothing.
80273
If I have a dictionary like:  { 'a': 1, 'b': 2, 'c': 3 }   How can I convert it to this?  [ ('a', 1), ('b', 2), ('c', 3) ]   And how can I convert it to this?  [ (1, 'a'), (2, 'b'), (3, 'c') ]
98138
In Python, I can do:  &gt;&gt;&gt; list = ['a', 'b', 'c'] &gt;&gt;&gt; ', '.join(list) 'a, b, c'   Is there any easy way to do the same when I have a list of objects?  &gt;&gt;&gt; class Obj: ...     def __str__(self): ...         return 'name' ... &gt;&gt;&gt; list = [Obj(), Obj(), Obj()] &gt;&gt;&gt; ', '.join(list) Traceback (most recent call last):   File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: sequence item 0: expected string, instance found   Or do I have to resort to a for loop?
64500
Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter, and then pass them to the string method. My code looks something like this:  class PairEvaluator(HandEvaluator):   def returnArbitrary(self):     return ('ace', 'king')  pe = PairEvaluator() cards = pe.returnArbitrary() print('Two pair, {0}s and {1}s'.format(cards))   When I try to run this code, the compiler gives an IndexError: tuple index out of range. How should I structure my return value to pass it as an argument to .format()?
6052
I have a list of items   John ID Matt ID John ID Scott ID Matt ID John ID Lucas ID   I want to shove them back into a list like so which also means I want to sort by the highest number of duplicates.   John ID 3 Matt ID 2 Scott ID 1 Lucas ID 1   Let me know how I can do this with LINQ and C#.  Thanks All  EDIT 2 Showing Code:      List&lt;game&gt; inventory = new List&lt;game&gt;();     drinkingforDataContext db = new drinkingforDataContext();     foreach (string item in tbTitle.Text.Split(' '))     {          List&lt;game&gt; getItems = (from dfg in db.drinkingfor_Games                                where dfg.game_Name.Contains(tbTitle.Text)                                select new game                                {                                    gameName = dfg.game_Name,                                    gameID = Boomers.Utilities.Guids.Encoder.EncodeURLs(dfg.uid)                                }).ToList&lt;game&gt;();          for (int i = 0; i &lt; getItems.Count(); i++)         {             inventory.Add(getItems[i]);         }     }      var items = (from xx in inventory                  group xx by xx into g                  let count = g.Count()                  orderby count descending                  select new                     {                         Count = count,                         gameName = g.Key.gameName,                         gameID = g.Key.gameID                     });      lvRelatedGames.DataSource = items;     lvRelatedGames.DataBind();   This query displays these results:   1 hello world times 1 hello world times 1 Hello World. 1 hello world times 1 hello world times 1 hello world times 1 Hello World. 1 hello world times   It gives me the count and name, but it doesn't give me the ID of the game....  It should display:   6 hello world times   234234 2 Hello World.  23432432
92311
What is an elegant way to sort objects in PHP? I would love to accomplish something similar to this.  $sortedObjectArary = sort($unsortedObjectArray, $Object-&gt;weight);   Basically specify the array I want to sort as well as the field I want to sort on. I looked into multidimensional array sorting and there might be something useful there, but I don't see anything elegant or obvious.
101984
I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?  Example:    testlist = [1,2,3,5,3,1,2,1,6] for item in testlist:     if item == 1:         print position
37453
I have a list of tuples like this:  [ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ]   I want to iterate through this keying by the first item, so, for example, I could print something like this:  a 1 2 3 b 1 2 c 1   How would I go about doing this without keeping an item to track whether the first item is the same as I loop around the tuples? This feels rather messy (plus I have to sort the list to start with)...
100856
Do you know if there is a built-in function to build a dictionary from an arbitrary object? I'd like to do something like this:  &gt;&gt;&gt; class Foo: ...     bar = 'hello' ...     baz = 'world' ... &gt;&gt;&gt; f = Foo() &gt;&gt;&gt; props(f) { 'bar' : 'hello', 'baz' : 'world' }   NOTE: It should not include methods. Only fields.
114747
I have a Python function which converts Eastings and Northings to Latitude and Longitude if line_count == 0:                     keys = row                     print('keys', keys)                     line_count += 1                 else:                     inProj = Proj(init='epsg:27700') # British National Grid                     outProj = Proj(init='epsg:4326') # WGS84                     x1, y1 = row[1], row[2]  # easting, northing                     x2, y2 = transform(inProj, outProj, x1, y1)                     row[1], row[2] = y2, x2                                          json_doc = {}  At the moment it converts from epsg:27700 (British National Grid) to WGS84, however there is a possibility I will get TM75 Irish Grid too. Is there any way I can get Python to recognised which CRS is applicable for the conversion?
100185
I was looking at   and I wondered if the following could be done from terminal. I did it in python, just want to see if it could be done from terminal, bash scripting or whatever.  Suppose I have a file that looks like so:  2,4,5,14,9 40,3,5,10,1   could it be sorted like so, by rows (lines)  2,4,5,9,14 1,3,5,10,40   or is it too complicated? I did it using python, I just want to know if it could be done so next time I might not use python. What I have done, is creating lists and sorting them.