How to auto increment duplicate values by a specific number in Python?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I am expecting the list to be sorted and then duplicates will be spaced by increments of 0.1. Why is my code below not working? Here is what I am expecting to get versus what my program is returning:




Expected Output: [11, 15, 15.1, 20, 20.1, 20.2, 20.3, 20.4, 30,
30.1, 40, 40.1, 50, 50.1]



Actual output:[11, 15, 15.1, 20, 20.1, 20.1, 20.1, 20.1, 30, 30.1, 40,
40.1, 50, 50.1]




Python Code:



my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
my_list.sort()
dup_list =


for i in range (len(my_list)):
if my_list[i] not in dup_list:
dup_list.append(my_list[i])
else:
my_list[i] = my_list[i] + 0.10

dup_list.append(my_list[i])









share|improve this question




















  • 2





    What should happen if there are more than 10 duplicates for a given number ?

    – Akshay L Aradhya
    Jan 4 at 10:36






  • 1





    Have you tried to debug it ? The problem is you do not track how many times a number is duplicated. For example every time you read 20, your code don't know if it's the second time or the fourth, so it only add 0.10.

    – vincrichaud
    Jan 4 at 10:40


















2















I am expecting the list to be sorted and then duplicates will be spaced by increments of 0.1. Why is my code below not working? Here is what I am expecting to get versus what my program is returning:




Expected Output: [11, 15, 15.1, 20, 20.1, 20.2, 20.3, 20.4, 30,
30.1, 40, 40.1, 50, 50.1]



Actual output:[11, 15, 15.1, 20, 20.1, 20.1, 20.1, 20.1, 30, 30.1, 40,
40.1, 50, 50.1]




Python Code:



my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
my_list.sort()
dup_list =


for i in range (len(my_list)):
if my_list[i] not in dup_list:
dup_list.append(my_list[i])
else:
my_list[i] = my_list[i] + 0.10

dup_list.append(my_list[i])









share|improve this question




















  • 2





    What should happen if there are more than 10 duplicates for a given number ?

    – Akshay L Aradhya
    Jan 4 at 10:36






  • 1





    Have you tried to debug it ? The problem is you do not track how many times a number is duplicated. For example every time you read 20, your code don't know if it's the second time or the fourth, so it only add 0.10.

    – vincrichaud
    Jan 4 at 10:40














2












2








2








I am expecting the list to be sorted and then duplicates will be spaced by increments of 0.1. Why is my code below not working? Here is what I am expecting to get versus what my program is returning:




Expected Output: [11, 15, 15.1, 20, 20.1, 20.2, 20.3, 20.4, 30,
30.1, 40, 40.1, 50, 50.1]



Actual output:[11, 15, 15.1, 20, 20.1, 20.1, 20.1, 20.1, 30, 30.1, 40,
40.1, 50, 50.1]




Python Code:



my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
my_list.sort()
dup_list =


for i in range (len(my_list)):
if my_list[i] not in dup_list:
dup_list.append(my_list[i])
else:
my_list[i] = my_list[i] + 0.10

dup_list.append(my_list[i])









share|improve this question
















I am expecting the list to be sorted and then duplicates will be spaced by increments of 0.1. Why is my code below not working? Here is what I am expecting to get versus what my program is returning:




Expected Output: [11, 15, 15.1, 20, 20.1, 20.2, 20.3, 20.4, 30,
30.1, 40, 40.1, 50, 50.1]



Actual output:[11, 15, 15.1, 20, 20.1, 20.1, 20.1, 20.1, 30, 30.1, 40,
40.1, 50, 50.1]




Python Code:



my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
my_list.sort()
dup_list =


for i in range (len(my_list)):
if my_list[i] not in dup_list:
dup_list.append(my_list[i])
else:
my_list[i] = my_list[i] + 0.10

dup_list.append(my_list[i])






python python-3.x algorithm python-2.7 math






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 5 at 16:53









Jack Moody

8702925




8702925










asked Jan 4 at 10:34









Parth BhodiaParth Bhodia

132




132








  • 2





    What should happen if there are more than 10 duplicates for a given number ?

    – Akshay L Aradhya
    Jan 4 at 10:36






  • 1





    Have you tried to debug it ? The problem is you do not track how many times a number is duplicated. For example every time you read 20, your code don't know if it's the second time or the fourth, so it only add 0.10.

    – vincrichaud
    Jan 4 at 10:40














  • 2





    What should happen if there are more than 10 duplicates for a given number ?

    – Akshay L Aradhya
    Jan 4 at 10:36






  • 1





    Have you tried to debug it ? The problem is you do not track how many times a number is duplicated. For example every time you read 20, your code don't know if it's the second time or the fourth, so it only add 0.10.

    – vincrichaud
    Jan 4 at 10:40








2




2





What should happen if there are more than 10 duplicates for a given number ?

– Akshay L Aradhya
Jan 4 at 10:36





What should happen if there are more than 10 duplicates for a given number ?

– Akshay L Aradhya
Jan 4 at 10:36




1




1





Have you tried to debug it ? The problem is you do not track how many times a number is duplicated. For example every time you read 20, your code don't know if it's the second time or the fourth, so it only add 0.10.

– vincrichaud
Jan 4 at 10:40





Have you tried to debug it ? The problem is you do not track how many times a number is duplicated. For example every time you read 20, your code don't know if it's the second time or the fourth, so it only add 0.10.

– vincrichaud
Jan 4 at 10:40












7 Answers
7






active

oldest

votes


















0














here is the solution according to your code.
your code is right what you were lacking was the case when my_list[i]+0.1*i
value is already present. ie in example when 20 is there you increase it to
20.1 (happening) but you miss that but when 20.1 is there . you just checking
only for 20 not for 20.1 . that is why 20.1 is coming in your solution not 20.2.



my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
my_list.sort()
dup_list =


for i in range (len(my_list)):
if my_list[i] not in dup_list:
dup_list.append(my_list[i])
else:
j=1
res = True
while res:
val = my_list[i]+j*0.1
if val not in dup_list:
dup_list.append(val)
res = False
j+=1

print(dup_list)

#output [11, 15, 15.1, 20, 20.1, 20.2, 20.3, 20.4, 30, 30.1, 40, 40.1, 50, 50.1]





share|improve this answer

































    4














    You could use itertools.groupby to group equal consecutive elements:



    from itertools import groupby

    my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
    my_list.sort()
    result = [g + i * 0.1 for k, group in groupby(my_list) for i, g in enumerate(group)]
    print(result)


    Output



    [11.0, 15.0, 15.1, 20.0, 20.1, 20.2, 20.3, 20.4, 30.0, 30.1, 40.0, 40.1, 50.0, 50.1]





    share|improve this answer































      2














      I can propose simple fix to the original code:



      my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
      my_list.sort()
      dup_list =

      for i in range (len(my_list)):
      if my_list[i] not in dup_list:
      dup_list.append(my_list[i])
      else:
      dup_list.append(dup_list[i-1]+0.1)





      share|improve this answer
























      • Your solution leads to small inaccuracies because you're adding 0.1 multiple times instead of adding 0.X (where X is the number of occurances) once. Try 20 + 0.1 + 0.1 + 0.1 and 20 + 0.1 * 3 in the interpreter to see the difference.

        – Felix
        Jan 4 at 11:00



















      1














      The problem is you are only incrementing it once. You are not keeping a count of how many times a number has appeared before.



      What you need is some kind of frequency dictionary that will store how many times this number has appeared. Using that frequency f you add f-1 increments to the number.



      my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
      my_list.sort()
      dup_list =
      feq = {}

      for i in range (len(my_list)):
      if my_list[i] not in feq:
      feq[my_list[i]] = 1
      else:
      feq[my_list[i]] += 1

      dup_list.append(my_list[i] + (feq[my_list[i]]-1)*0.1)





      share|improve this answer































        1














        Try this improved code:



        my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
        my_list.sort()
        out_list =

        for value in my_list:
        if value in out_list:
        while value in out_list:
        value += .1
        out_list.append(value)





        share|improve this answer































          0














          Using defaultdict:



          from collections import defaultdict

          my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
          my_list.sort()
          dup_list =
          occurrences = defaultdict(int)

          for elmt in my_list:
          dup_list.append(elmt + occurrences[elmt] * 0.10)
          occurrences[elmt] += 1


          Output:



          [11.0, 15.0, 15.1, 20.0, 20.1, 20.2, 20.3, 20.4, 30.0, 30.1, 40.0, 40.1, 50.0, 50.1]


          If you need the original values to still be integers, comment below and I'll change that.






          share|improve this answer































            0














            Another (more advanced) option is to write a cusom generator:



            from itertools import count

            def gen(value):
            """Returns a generator that first yields `value` and then `value + x * 0.10` (where x is 1, 2, ...).
            """
            yield value
            yield from map(lambda x: value + x * 0.10, count(1))

            my_list = [20, 20, 20, 30, 20, 30, 40, 50, 15, 11, 20, 40, 50, 15]

            # create a generator for each distinct value in my_list
            generators = {k: gen(k) for k in set(my_list)}

            # calculate the result list
            dup_list = [next(generators[elmt]) for elmt in sorted(my_list)]

            print(dup_list)


            IMO, this is not the easiest solution. I'm sharing it anyways because it might help others understand generators, especially yield from.






            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%2f54037231%2fhow-to-auto-increment-duplicate-values-by-a-specific-number-in-python%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              7 Answers
              7






              active

              oldest

              votes








              7 Answers
              7






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              here is the solution according to your code.
              your code is right what you were lacking was the case when my_list[i]+0.1*i
              value is already present. ie in example when 20 is there you increase it to
              20.1 (happening) but you miss that but when 20.1 is there . you just checking
              only for 20 not for 20.1 . that is why 20.1 is coming in your solution not 20.2.



              my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
              my_list.sort()
              dup_list =


              for i in range (len(my_list)):
              if my_list[i] not in dup_list:
              dup_list.append(my_list[i])
              else:
              j=1
              res = True
              while res:
              val = my_list[i]+j*0.1
              if val not in dup_list:
              dup_list.append(val)
              res = False
              j+=1

              print(dup_list)

              #output [11, 15, 15.1, 20, 20.1, 20.2, 20.3, 20.4, 30, 30.1, 40, 40.1, 50, 50.1]





              share|improve this answer






























                0














                here is the solution according to your code.
                your code is right what you were lacking was the case when my_list[i]+0.1*i
                value is already present. ie in example when 20 is there you increase it to
                20.1 (happening) but you miss that but when 20.1 is there . you just checking
                only for 20 not for 20.1 . that is why 20.1 is coming in your solution not 20.2.



                my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                my_list.sort()
                dup_list =


                for i in range (len(my_list)):
                if my_list[i] not in dup_list:
                dup_list.append(my_list[i])
                else:
                j=1
                res = True
                while res:
                val = my_list[i]+j*0.1
                if val not in dup_list:
                dup_list.append(val)
                res = False
                j+=1

                print(dup_list)

                #output [11, 15, 15.1, 20, 20.1, 20.2, 20.3, 20.4, 30, 30.1, 40, 40.1, 50, 50.1]





                share|improve this answer




























                  0












                  0








                  0







                  here is the solution according to your code.
                  your code is right what you were lacking was the case when my_list[i]+0.1*i
                  value is already present. ie in example when 20 is there you increase it to
                  20.1 (happening) but you miss that but when 20.1 is there . you just checking
                  only for 20 not for 20.1 . that is why 20.1 is coming in your solution not 20.2.



                  my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                  my_list.sort()
                  dup_list =


                  for i in range (len(my_list)):
                  if my_list[i] not in dup_list:
                  dup_list.append(my_list[i])
                  else:
                  j=1
                  res = True
                  while res:
                  val = my_list[i]+j*0.1
                  if val not in dup_list:
                  dup_list.append(val)
                  res = False
                  j+=1

                  print(dup_list)

                  #output [11, 15, 15.1, 20, 20.1, 20.2, 20.3, 20.4, 30, 30.1, 40, 40.1, 50, 50.1]





                  share|improve this answer















                  here is the solution according to your code.
                  your code is right what you were lacking was the case when my_list[i]+0.1*i
                  value is already present. ie in example when 20 is there you increase it to
                  20.1 (happening) but you miss that but when 20.1 is there . you just checking
                  only for 20 not for 20.1 . that is why 20.1 is coming in your solution not 20.2.



                  my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                  my_list.sort()
                  dup_list =


                  for i in range (len(my_list)):
                  if my_list[i] not in dup_list:
                  dup_list.append(my_list[i])
                  else:
                  j=1
                  res = True
                  while res:
                  val = my_list[i]+j*0.1
                  if val not in dup_list:
                  dup_list.append(val)
                  res = False
                  j+=1

                  print(dup_list)

                  #output [11, 15, 15.1, 20, 20.1, 20.2, 20.3, 20.4, 30, 30.1, 40, 40.1, 50, 50.1]






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 4 at 11:05

























                  answered Jan 4 at 10:58









                  prashant ranaprashant rana

                  1,1691920




                  1,1691920

























                      4














                      You could use itertools.groupby to group equal consecutive elements:



                      from itertools import groupby

                      my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                      my_list.sort()
                      result = [g + i * 0.1 for k, group in groupby(my_list) for i, g in enumerate(group)]
                      print(result)


                      Output



                      [11.0, 15.0, 15.1, 20.0, 20.1, 20.2, 20.3, 20.4, 30.0, 30.1, 40.0, 40.1, 50.0, 50.1]





                      share|improve this answer




























                        4














                        You could use itertools.groupby to group equal consecutive elements:



                        from itertools import groupby

                        my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                        my_list.sort()
                        result = [g + i * 0.1 for k, group in groupby(my_list) for i, g in enumerate(group)]
                        print(result)


                        Output



                        [11.0, 15.0, 15.1, 20.0, 20.1, 20.2, 20.3, 20.4, 30.0, 30.1, 40.0, 40.1, 50.0, 50.1]





                        share|improve this answer


























                          4












                          4








                          4







                          You could use itertools.groupby to group equal consecutive elements:



                          from itertools import groupby

                          my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                          my_list.sort()
                          result = [g + i * 0.1 for k, group in groupby(my_list) for i, g in enumerate(group)]
                          print(result)


                          Output



                          [11.0, 15.0, 15.1, 20.0, 20.1, 20.2, 20.3, 20.4, 30.0, 30.1, 40.0, 40.1, 50.0, 50.1]





                          share|improve this answer













                          You could use itertools.groupby to group equal consecutive elements:



                          from itertools import groupby

                          my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                          my_list.sort()
                          result = [g + i * 0.1 for k, group in groupby(my_list) for i, g in enumerate(group)]
                          print(result)


                          Output



                          [11.0, 15.0, 15.1, 20.0, 20.1, 20.2, 20.3, 20.4, 30.0, 30.1, 40.0, 40.1, 50.0, 50.1]






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 4 at 10:39









                          Daniel MesejoDaniel Mesejo

                          18.8k21533




                          18.8k21533























                              2














                              I can propose simple fix to the original code:



                              my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                              my_list.sort()
                              dup_list =

                              for i in range (len(my_list)):
                              if my_list[i] not in dup_list:
                              dup_list.append(my_list[i])
                              else:
                              dup_list.append(dup_list[i-1]+0.1)





                              share|improve this answer
























                              • Your solution leads to small inaccuracies because you're adding 0.1 multiple times instead of adding 0.X (where X is the number of occurances) once. Try 20 + 0.1 + 0.1 + 0.1 and 20 + 0.1 * 3 in the interpreter to see the difference.

                                – Felix
                                Jan 4 at 11:00
















                              2














                              I can propose simple fix to the original code:



                              my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                              my_list.sort()
                              dup_list =

                              for i in range (len(my_list)):
                              if my_list[i] not in dup_list:
                              dup_list.append(my_list[i])
                              else:
                              dup_list.append(dup_list[i-1]+0.1)





                              share|improve this answer
























                              • Your solution leads to small inaccuracies because you're adding 0.1 multiple times instead of adding 0.X (where X is the number of occurances) once. Try 20 + 0.1 + 0.1 + 0.1 and 20 + 0.1 * 3 in the interpreter to see the difference.

                                – Felix
                                Jan 4 at 11:00














                              2












                              2








                              2







                              I can propose simple fix to the original code:



                              my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                              my_list.sort()
                              dup_list =

                              for i in range (len(my_list)):
                              if my_list[i] not in dup_list:
                              dup_list.append(my_list[i])
                              else:
                              dup_list.append(dup_list[i-1]+0.1)





                              share|improve this answer













                              I can propose simple fix to the original code:



                              my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                              my_list.sort()
                              dup_list =

                              for i in range (len(my_list)):
                              if my_list[i] not in dup_list:
                              dup_list.append(my_list[i])
                              else:
                              dup_list.append(dup_list[i-1]+0.1)






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 4 at 10:42









                              msi_gervamsi_gerva

                              763721




                              763721













                              • Your solution leads to small inaccuracies because you're adding 0.1 multiple times instead of adding 0.X (where X is the number of occurances) once. Try 20 + 0.1 + 0.1 + 0.1 and 20 + 0.1 * 3 in the interpreter to see the difference.

                                – Felix
                                Jan 4 at 11:00



















                              • Your solution leads to small inaccuracies because you're adding 0.1 multiple times instead of adding 0.X (where X is the number of occurances) once. Try 20 + 0.1 + 0.1 + 0.1 and 20 + 0.1 * 3 in the interpreter to see the difference.

                                – Felix
                                Jan 4 at 11:00

















                              Your solution leads to small inaccuracies because you're adding 0.1 multiple times instead of adding 0.X (where X is the number of occurances) once. Try 20 + 0.1 + 0.1 + 0.1 and 20 + 0.1 * 3 in the interpreter to see the difference.

                              – Felix
                              Jan 4 at 11:00





                              Your solution leads to small inaccuracies because you're adding 0.1 multiple times instead of adding 0.X (where X is the number of occurances) once. Try 20 + 0.1 + 0.1 + 0.1 and 20 + 0.1 * 3 in the interpreter to see the difference.

                              – Felix
                              Jan 4 at 11:00











                              1














                              The problem is you are only incrementing it once. You are not keeping a count of how many times a number has appeared before.



                              What you need is some kind of frequency dictionary that will store how many times this number has appeared. Using that frequency f you add f-1 increments to the number.



                              my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                              my_list.sort()
                              dup_list =
                              feq = {}

                              for i in range (len(my_list)):
                              if my_list[i] not in feq:
                              feq[my_list[i]] = 1
                              else:
                              feq[my_list[i]] += 1

                              dup_list.append(my_list[i] + (feq[my_list[i]]-1)*0.1)





                              share|improve this answer




























                                1














                                The problem is you are only incrementing it once. You are not keeping a count of how many times a number has appeared before.



                                What you need is some kind of frequency dictionary that will store how many times this number has appeared. Using that frequency f you add f-1 increments to the number.



                                my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                                my_list.sort()
                                dup_list =
                                feq = {}

                                for i in range (len(my_list)):
                                if my_list[i] not in feq:
                                feq[my_list[i]] = 1
                                else:
                                feq[my_list[i]] += 1

                                dup_list.append(my_list[i] + (feq[my_list[i]]-1)*0.1)





                                share|improve this answer


























                                  1












                                  1








                                  1







                                  The problem is you are only incrementing it once. You are not keeping a count of how many times a number has appeared before.



                                  What you need is some kind of frequency dictionary that will store how many times this number has appeared. Using that frequency f you add f-1 increments to the number.



                                  my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                                  my_list.sort()
                                  dup_list =
                                  feq = {}

                                  for i in range (len(my_list)):
                                  if my_list[i] not in feq:
                                  feq[my_list[i]] = 1
                                  else:
                                  feq[my_list[i]] += 1

                                  dup_list.append(my_list[i] + (feq[my_list[i]]-1)*0.1)





                                  share|improve this answer













                                  The problem is you are only incrementing it once. You are not keeping a count of how many times a number has appeared before.



                                  What you need is some kind of frequency dictionary that will store how many times this number has appeared. Using that frequency f you add f-1 increments to the number.



                                  my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                                  my_list.sort()
                                  dup_list =
                                  feq = {}

                                  for i in range (len(my_list)):
                                  if my_list[i] not in feq:
                                  feq[my_list[i]] = 1
                                  else:
                                  feq[my_list[i]] += 1

                                  dup_list.append(my_list[i] + (feq[my_list[i]]-1)*0.1)






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jan 4 at 10:44









                                  Akshay L AradhyaAkshay L Aradhya

                                  1,05811128




                                  1,05811128























                                      1














                                      Try this improved code:



                                      my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                                      my_list.sort()
                                      out_list =

                                      for value in my_list:
                                      if value in out_list:
                                      while value in out_list:
                                      value += .1
                                      out_list.append(value)





                                      share|improve this answer




























                                        1














                                        Try this improved code:



                                        my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                                        my_list.sort()
                                        out_list =

                                        for value in my_list:
                                        if value in out_list:
                                        while value in out_list:
                                        value += .1
                                        out_list.append(value)





                                        share|improve this answer


























                                          1












                                          1








                                          1







                                          Try this improved code:



                                          my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                                          my_list.sort()
                                          out_list =

                                          for value in my_list:
                                          if value in out_list:
                                          while value in out_list:
                                          value += .1
                                          out_list.append(value)





                                          share|improve this answer













                                          Try this improved code:



                                          my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                                          my_list.sort()
                                          out_list =

                                          for value in my_list:
                                          if value in out_list:
                                          while value in out_list:
                                          value += .1
                                          out_list.append(value)






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jan 4 at 10:45









                                          GeeTransitGeeTransit

                                          694316




                                          694316























                                              0














                                              Using defaultdict:



                                              from collections import defaultdict

                                              my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                                              my_list.sort()
                                              dup_list =
                                              occurrences = defaultdict(int)

                                              for elmt in my_list:
                                              dup_list.append(elmt + occurrences[elmt] * 0.10)
                                              occurrences[elmt] += 1


                                              Output:



                                              [11.0, 15.0, 15.1, 20.0, 20.1, 20.2, 20.3, 20.4, 30.0, 30.1, 40.0, 40.1, 50.0, 50.1]


                                              If you need the original values to still be integers, comment below and I'll change that.






                                              share|improve this answer




























                                                0














                                                Using defaultdict:



                                                from collections import defaultdict

                                                my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                                                my_list.sort()
                                                dup_list =
                                                occurrences = defaultdict(int)

                                                for elmt in my_list:
                                                dup_list.append(elmt + occurrences[elmt] * 0.10)
                                                occurrences[elmt] += 1


                                                Output:



                                                [11.0, 15.0, 15.1, 20.0, 20.1, 20.2, 20.3, 20.4, 30.0, 30.1, 40.0, 40.1, 50.0, 50.1]


                                                If you need the original values to still be integers, comment below and I'll change that.






                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  Using defaultdict:



                                                  from collections import defaultdict

                                                  my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                                                  my_list.sort()
                                                  dup_list =
                                                  occurrences = defaultdict(int)

                                                  for elmt in my_list:
                                                  dup_list.append(elmt + occurrences[elmt] * 0.10)
                                                  occurrences[elmt] += 1


                                                  Output:



                                                  [11.0, 15.0, 15.1, 20.0, 20.1, 20.2, 20.3, 20.4, 30.0, 30.1, 40.0, 40.1, 50.0, 50.1]


                                                  If you need the original values to still be integers, comment below and I'll change that.






                                                  share|improve this answer













                                                  Using defaultdict:



                                                  from collections import defaultdict

                                                  my_list = [20,20,20,30,20,30,40,50,15,11,20,40,50,15]
                                                  my_list.sort()
                                                  dup_list =
                                                  occurrences = defaultdict(int)

                                                  for elmt in my_list:
                                                  dup_list.append(elmt + occurrences[elmt] * 0.10)
                                                  occurrences[elmt] += 1


                                                  Output:



                                                  [11.0, 15.0, 15.1, 20.0, 20.1, 20.2, 20.3, 20.4, 30.0, 30.1, 40.0, 40.1, 50.0, 50.1]


                                                  If you need the original values to still be integers, comment below and I'll change that.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Jan 4 at 10:54









                                                  FelixFelix

                                                  3,2612930




                                                  3,2612930























                                                      0














                                                      Another (more advanced) option is to write a cusom generator:



                                                      from itertools import count

                                                      def gen(value):
                                                      """Returns a generator that first yields `value` and then `value + x * 0.10` (where x is 1, 2, ...).
                                                      """
                                                      yield value
                                                      yield from map(lambda x: value + x * 0.10, count(1))

                                                      my_list = [20, 20, 20, 30, 20, 30, 40, 50, 15, 11, 20, 40, 50, 15]

                                                      # create a generator for each distinct value in my_list
                                                      generators = {k: gen(k) for k in set(my_list)}

                                                      # calculate the result list
                                                      dup_list = [next(generators[elmt]) for elmt in sorted(my_list)]

                                                      print(dup_list)


                                                      IMO, this is not the easiest solution. I'm sharing it anyways because it might help others understand generators, especially yield from.






                                                      share|improve this answer




























                                                        0














                                                        Another (more advanced) option is to write a cusom generator:



                                                        from itertools import count

                                                        def gen(value):
                                                        """Returns a generator that first yields `value` and then `value + x * 0.10` (where x is 1, 2, ...).
                                                        """
                                                        yield value
                                                        yield from map(lambda x: value + x * 0.10, count(1))

                                                        my_list = [20, 20, 20, 30, 20, 30, 40, 50, 15, 11, 20, 40, 50, 15]

                                                        # create a generator for each distinct value in my_list
                                                        generators = {k: gen(k) for k in set(my_list)}

                                                        # calculate the result list
                                                        dup_list = [next(generators[elmt]) for elmt in sorted(my_list)]

                                                        print(dup_list)


                                                        IMO, this is not the easiest solution. I'm sharing it anyways because it might help others understand generators, especially yield from.






                                                        share|improve this answer


























                                                          0












                                                          0








                                                          0







                                                          Another (more advanced) option is to write a cusom generator:



                                                          from itertools import count

                                                          def gen(value):
                                                          """Returns a generator that first yields `value` and then `value + x * 0.10` (where x is 1, 2, ...).
                                                          """
                                                          yield value
                                                          yield from map(lambda x: value + x * 0.10, count(1))

                                                          my_list = [20, 20, 20, 30, 20, 30, 40, 50, 15, 11, 20, 40, 50, 15]

                                                          # create a generator for each distinct value in my_list
                                                          generators = {k: gen(k) for k in set(my_list)}

                                                          # calculate the result list
                                                          dup_list = [next(generators[elmt]) for elmt in sorted(my_list)]

                                                          print(dup_list)


                                                          IMO, this is not the easiest solution. I'm sharing it anyways because it might help others understand generators, especially yield from.






                                                          share|improve this answer













                                                          Another (more advanced) option is to write a cusom generator:



                                                          from itertools import count

                                                          def gen(value):
                                                          """Returns a generator that first yields `value` and then `value + x * 0.10` (where x is 1, 2, ...).
                                                          """
                                                          yield value
                                                          yield from map(lambda x: value + x * 0.10, count(1))

                                                          my_list = [20, 20, 20, 30, 20, 30, 40, 50, 15, 11, 20, 40, 50, 15]

                                                          # create a generator for each distinct value in my_list
                                                          generators = {k: gen(k) for k in set(my_list)}

                                                          # calculate the result list
                                                          dup_list = [next(generators[elmt]) for elmt in sorted(my_list)]

                                                          print(dup_list)


                                                          IMO, this is not the easiest solution. I'm sharing it anyways because it might help others understand generators, especially yield from.







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jan 4 at 11:24









                                                          FelixFelix

                                                          3,2612930




                                                          3,2612930






























                                                              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%2f54037231%2fhow-to-auto-increment-duplicate-values-by-a-specific-number-in-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

                                                              Monofisismo

                                                              Angular Downloading a file using contenturl with Basic Authentication

                                                              Olmecas