How to make dict from list components - python












2















I have a list of dates:



dates = ['2018-11-13 ', '2018-11-14 ']


and I have a list of weather data for various cities:



weather_data = [('Carbondale', 1875.341, '2018-11-13 '), ('Carbondale', 1286.16, '2018-11-14 '), ('Davenport', 708.5, '2018-11-13 '), ('Davenport', 506.1, '2018-11-14 ')]


i[1] in weather_data is a climate score, based on climatic info for each day. I have shortened the above lists for the sake of this example.



My goal is to find the city with the lowest climate score for each day. I thought a good way to do that would be to put them in a dictionary.



An example of what I want is...



conditions_dict = {'2018-11-13': ('Carbondale',1875.341), ('Davenport', 708.5)}


and my end output would be...



The best weather on 2018-11-13 is in Davenport with a value of 708.5


Basically, if I had a dict with a date as the key, and (city,value) as the value, I could then easily find the lowest value by city for each day.



However, I cannot figure how to make my dictionary look like this. The part I am really struggling with is how to match the date to multiple readings for various cities on one day.



Is using a dictionary even a good way to do this?










share|improve this question





























    2















    I have a list of dates:



    dates = ['2018-11-13 ', '2018-11-14 ']


    and I have a list of weather data for various cities:



    weather_data = [('Carbondale', 1875.341, '2018-11-13 '), ('Carbondale', 1286.16, '2018-11-14 '), ('Davenport', 708.5, '2018-11-13 '), ('Davenport', 506.1, '2018-11-14 ')]


    i[1] in weather_data is a climate score, based on climatic info for each day. I have shortened the above lists for the sake of this example.



    My goal is to find the city with the lowest climate score for each day. I thought a good way to do that would be to put them in a dictionary.



    An example of what I want is...



    conditions_dict = {'2018-11-13': ('Carbondale',1875.341), ('Davenport', 708.5)}


    and my end output would be...



    The best weather on 2018-11-13 is in Davenport with a value of 708.5


    Basically, if I had a dict with a date as the key, and (city,value) as the value, I could then easily find the lowest value by city for each day.



    However, I cannot figure how to make my dictionary look like this. The part I am really struggling with is how to match the date to multiple readings for various cities on one day.



    Is using a dictionary even a good way to do this?










    share|improve this question



























      2












      2








      2


      1






      I have a list of dates:



      dates = ['2018-11-13 ', '2018-11-14 ']


      and I have a list of weather data for various cities:



      weather_data = [('Carbondale', 1875.341, '2018-11-13 '), ('Carbondale', 1286.16, '2018-11-14 '), ('Davenport', 708.5, '2018-11-13 '), ('Davenport', 506.1, '2018-11-14 ')]


      i[1] in weather_data is a climate score, based on climatic info for each day. I have shortened the above lists for the sake of this example.



      My goal is to find the city with the lowest climate score for each day. I thought a good way to do that would be to put them in a dictionary.



      An example of what I want is...



      conditions_dict = {'2018-11-13': ('Carbondale',1875.341), ('Davenport', 708.5)}


      and my end output would be...



      The best weather on 2018-11-13 is in Davenport with a value of 708.5


      Basically, if I had a dict with a date as the key, and (city,value) as the value, I could then easily find the lowest value by city for each day.



      However, I cannot figure how to make my dictionary look like this. The part I am really struggling with is how to match the date to multiple readings for various cities on one day.



      Is using a dictionary even a good way to do this?










      share|improve this question
















      I have a list of dates:



      dates = ['2018-11-13 ', '2018-11-14 ']


      and I have a list of weather data for various cities:



      weather_data = [('Carbondale', 1875.341, '2018-11-13 '), ('Carbondale', 1286.16, '2018-11-14 '), ('Davenport', 708.5, '2018-11-13 '), ('Davenport', 506.1, '2018-11-14 ')]


      i[1] in weather_data is a climate score, based on climatic info for each day. I have shortened the above lists for the sake of this example.



      My goal is to find the city with the lowest climate score for each day. I thought a good way to do that would be to put them in a dictionary.



      An example of what I want is...



      conditions_dict = {'2018-11-13': ('Carbondale',1875.341), ('Davenport', 708.5)}


      and my end output would be...



      The best weather on 2018-11-13 is in Davenport with a value of 708.5


      Basically, if I had a dict with a date as the key, and (city,value) as the value, I could then easily find the lowest value by city for each day.



      However, I cannot figure how to make my dictionary look like this. The part I am really struggling with is how to match the date to multiple readings for various cities on one day.



      Is using a dictionary even a good way to do this?







      python python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 20:02









      blhsing

      29.4k41336




      29.4k41336










      asked Nov 12 '18 at 19:45









      Erich PurpurErich Purpur

      1407




      1407
























          2 Answers
          2






          active

          oldest

          votes


















          2














          You don't really need an intermediate dict with all cities and scores for each date if your goal is find the minimum score and city of each date since you can simply iterate through weather_data and keep track of the lowest score so far and its associated city for each date in a dict:



          min_score_of_date = {}
          for city, score, date in weather_data:
          if date not in min_score_of_date or score < min_score_of_date.get(date)[1]:
          min_score_of_date[date] = (city, score)


          Given your sample input, min_score_of_date would become:



          {'2018-11-13 ': ('Davenport', 708.5), '2018-11-14 ': ('Davenport', 506.1)}





          share|improve this answer



















          • 1





            yes, this is perfect. Thanks so much!

            – Erich Purpur
            Nov 12 '18 at 20:08



















          1














          This is another way you can go about it if the lowest temperature dates haven't already been filtered for you.



          # each date has a tuple of cities and their temperature
          conditions = {
          '2018-11-13': (
          ('Carbondale',1875.341),
          ('Davenport', 708.5)
          )
          }

          # loop through every date
          for date, cities in conditions.items():
          # for every date, loop through its values
          # grab its temperateure and add to the list
          # them find the minimun temperature

          # get all tempertures
          tempertures = [_[1] for _ in cities]
          # get minimum temperature
          min_temperture = min(tempertures)

          # loop throught all cities
          for city in cities:
          # if a city matches min_temperature do whats bellow
          if min_temperture in city:
          # city name
          name = city[0]
          # city temperture
          temperture = str(city[1])

          print(
          "The best weather on "
          + date
          + "is in "
          + name + " with a value of "
          + temperture
          )





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269072%2fhow-to-make-dict-from-list-components-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            You don't really need an intermediate dict with all cities and scores for each date if your goal is find the minimum score and city of each date since you can simply iterate through weather_data and keep track of the lowest score so far and its associated city for each date in a dict:



            min_score_of_date = {}
            for city, score, date in weather_data:
            if date not in min_score_of_date or score < min_score_of_date.get(date)[1]:
            min_score_of_date[date] = (city, score)


            Given your sample input, min_score_of_date would become:



            {'2018-11-13 ': ('Davenport', 708.5), '2018-11-14 ': ('Davenport', 506.1)}





            share|improve this answer



















            • 1





              yes, this is perfect. Thanks so much!

              – Erich Purpur
              Nov 12 '18 at 20:08
















            2














            You don't really need an intermediate dict with all cities and scores for each date if your goal is find the minimum score and city of each date since you can simply iterate through weather_data and keep track of the lowest score so far and its associated city for each date in a dict:



            min_score_of_date = {}
            for city, score, date in weather_data:
            if date not in min_score_of_date or score < min_score_of_date.get(date)[1]:
            min_score_of_date[date] = (city, score)


            Given your sample input, min_score_of_date would become:



            {'2018-11-13 ': ('Davenport', 708.5), '2018-11-14 ': ('Davenport', 506.1)}





            share|improve this answer



















            • 1





              yes, this is perfect. Thanks so much!

              – Erich Purpur
              Nov 12 '18 at 20:08














            2












            2








            2







            You don't really need an intermediate dict with all cities and scores for each date if your goal is find the minimum score and city of each date since you can simply iterate through weather_data and keep track of the lowest score so far and its associated city for each date in a dict:



            min_score_of_date = {}
            for city, score, date in weather_data:
            if date not in min_score_of_date or score < min_score_of_date.get(date)[1]:
            min_score_of_date[date] = (city, score)


            Given your sample input, min_score_of_date would become:



            {'2018-11-13 ': ('Davenport', 708.5), '2018-11-14 ': ('Davenport', 506.1)}





            share|improve this answer













            You don't really need an intermediate dict with all cities and scores for each date if your goal is find the minimum score and city of each date since you can simply iterate through weather_data and keep track of the lowest score so far and its associated city for each date in a dict:



            min_score_of_date = {}
            for city, score, date in weather_data:
            if date not in min_score_of_date or score < min_score_of_date.get(date)[1]:
            min_score_of_date[date] = (city, score)


            Given your sample input, min_score_of_date would become:



            {'2018-11-13 ': ('Davenport', 708.5), '2018-11-14 ': ('Davenport', 506.1)}






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 '18 at 20:00









            blhsingblhsing

            29.4k41336




            29.4k41336








            • 1





              yes, this is perfect. Thanks so much!

              – Erich Purpur
              Nov 12 '18 at 20:08














            • 1





              yes, this is perfect. Thanks so much!

              – Erich Purpur
              Nov 12 '18 at 20:08








            1




            1





            yes, this is perfect. Thanks so much!

            – Erich Purpur
            Nov 12 '18 at 20:08





            yes, this is perfect. Thanks so much!

            – Erich Purpur
            Nov 12 '18 at 20:08













            1














            This is another way you can go about it if the lowest temperature dates haven't already been filtered for you.



            # each date has a tuple of cities and their temperature
            conditions = {
            '2018-11-13': (
            ('Carbondale',1875.341),
            ('Davenport', 708.5)
            )
            }

            # loop through every date
            for date, cities in conditions.items():
            # for every date, loop through its values
            # grab its temperateure and add to the list
            # them find the minimun temperature

            # get all tempertures
            tempertures = [_[1] for _ in cities]
            # get minimum temperature
            min_temperture = min(tempertures)

            # loop throught all cities
            for city in cities:
            # if a city matches min_temperature do whats bellow
            if min_temperture in city:
            # city name
            name = city[0]
            # city temperture
            temperture = str(city[1])

            print(
            "The best weather on "
            + date
            + "is in "
            + name + " with a value of "
            + temperture
            )





            share|improve this answer




























              1














              This is another way you can go about it if the lowest temperature dates haven't already been filtered for you.



              # each date has a tuple of cities and their temperature
              conditions = {
              '2018-11-13': (
              ('Carbondale',1875.341),
              ('Davenport', 708.5)
              )
              }

              # loop through every date
              for date, cities in conditions.items():
              # for every date, loop through its values
              # grab its temperateure and add to the list
              # them find the minimun temperature

              # get all tempertures
              tempertures = [_[1] for _ in cities]
              # get minimum temperature
              min_temperture = min(tempertures)

              # loop throught all cities
              for city in cities:
              # if a city matches min_temperature do whats bellow
              if min_temperture in city:
              # city name
              name = city[0]
              # city temperture
              temperture = str(city[1])

              print(
              "The best weather on "
              + date
              + "is in "
              + name + " with a value of "
              + temperture
              )





              share|improve this answer


























                1












                1








                1







                This is another way you can go about it if the lowest temperature dates haven't already been filtered for you.



                # each date has a tuple of cities and their temperature
                conditions = {
                '2018-11-13': (
                ('Carbondale',1875.341),
                ('Davenport', 708.5)
                )
                }

                # loop through every date
                for date, cities in conditions.items():
                # for every date, loop through its values
                # grab its temperateure and add to the list
                # them find the minimun temperature

                # get all tempertures
                tempertures = [_[1] for _ in cities]
                # get minimum temperature
                min_temperture = min(tempertures)

                # loop throught all cities
                for city in cities:
                # if a city matches min_temperature do whats bellow
                if min_temperture in city:
                # city name
                name = city[0]
                # city temperture
                temperture = str(city[1])

                print(
                "The best weather on "
                + date
                + "is in "
                + name + " with a value of "
                + temperture
                )





                share|improve this answer













                This is another way you can go about it if the lowest temperature dates haven't already been filtered for you.



                # each date has a tuple of cities and their temperature
                conditions = {
                '2018-11-13': (
                ('Carbondale',1875.341),
                ('Davenport', 708.5)
                )
                }

                # loop through every date
                for date, cities in conditions.items():
                # for every date, loop through its values
                # grab its temperateure and add to the list
                # them find the minimun temperature

                # get all tempertures
                tempertures = [_[1] for _ in cities]
                # get minimum temperature
                min_temperture = min(tempertures)

                # loop throught all cities
                for city in cities:
                # if a city matches min_temperature do whats bellow
                if min_temperture in city:
                # city name
                name = city[0]
                # city temperture
                temperture = str(city[1])

                print(
                "The best weather on "
                + date
                + "is in "
                + name + " with a value of "
                + temperture
                )






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 '18 at 20:35









                BaryonBaryon

                365




                365






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269072%2fhow-to-make-dict-from-list-components-python%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Full-time equivalent

                    Bicuculline

                    さくらももこ