1

I am trying to rewrite some thing from python to c#, I have managed to recreate the dictionaries I but I am having trouble understanding the dictionary comprehension in c#. here is the python code I would like to recreate in c#

distances={1:.02,30:.001,1000:.001}
minSearch=(min(distances.items(), key=lambda x:x[1]))

this is the dictionary I have in c#

Dictionary<int, double> distDict = new Dictionary<int, double>();

Thanks for the answer here it is implemented

                List<MapPoint> list = new List<MapPoint>();
                list.Add(pointDict[Convert.ToInt32(startOid)]);
                while (pointDict.Count()>1)
                {
                    var shape = pointDict[Convert.ToInt32(startOid)];
                    pointDict.Remove(Convert.ToInt32(startOid));
                    var X = shape.X;
                    var Y = shape.Y;
                    var Z = shape.Z;
                    foreach(KeyValuePair<int,MapPoint> point in pointDict)
                    {
                        var X2 = point.Value.X;
                        var Y2 = point.Value.Y;
                        var Z2 = point.Value.Z;
                        var squaredZDist = Math.Pow((Z - Z2), 2);
                        var squaredDist = Math.Pow(Math.Sqrt(Math.Pow(X - X2, 2) + Math.Pow(Y - Y2, 2)),2);
                        var threeDSquaredDist = Math.Sqrt(squaredDist + squaredZDist);
                        distDict[point.Key] = threeDSquaredDist;
                    }
                    var minValue = distDict.Min(x => x.Value);
                    var minPair = distDict
                                    .Where(x => x.Value == minValue)
                                    .First();
                    startOid = minPair.Key;
                    list.Add(pointDict[minPair.Key]);

                    distDict.Clear();
                }
                Polyline polyline = PolylineBuilder.CreatePolyline(list);
                var pipeLayer = mapView.Map.Layers.OfType<FeatureLayer>()
                                                    .Where(x=>x.Name == "PIPELINE")
                                                    .First();
                MessageBox.Show(pipeLayer.Name.ToString());
                var createFeatures = new EditOperation();
                createFeatures.Name = "Create Polyline";
                createFeatures.Create(pipeLayer, polyline);
                createFeatures.Execute();

2 Answers 2

1

you can duplicate that python using:

var distDict = new Dictionary<int, double> {
  {1,.02},{30,.001},{1000,.001}
};
var minValue = distDict.Min(x => x.Value);
var minPair = distDict
                .Where(x => x.Value == minValue)
                .First();
var expected = new KeyValuePair<int, double>(30, .001);
Assert.Equal(expected, minPair);
Sign up to request clarification or add additional context in comments.

1 Comment

The answer worked perfectly. Thank you very much. I posted the implementation above in the question.
1

We can do this with:

KeyValuePair<int, double> minSearch = distDict.OrderBy(x => x.Value).First()

This is thus a KeyValuePair<int, double> that contains a Key and Value property of the dictionary entry with the smallest Value in the `Dictionary.

C# will not order the collection immediately with .OrderBy(..), but construct an OrderedEnumerable [GitHub]. This will normally result in the fact that the .First() will aim to calculate the smallest item in linear time, so O(n), and like @flakes says, constant memory complexity O(1).

3 Comments

Cool! I never knew that about OrderedEnumerable. I always assumed that OrderBy does a complete sort.
@flakes: well of course if you enumerate over the items, it eventually performs a full sort, but .First() should normally work in linear time.
And O(1) for memory complexity is also super nice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.