14 Replies - 406 Views - Last Post: 03 November 2017 - 12:50 PM Rate Topic: -----

#1 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 20-March 15

testing for the presence adjacent duplicates in a list

Posted 01 November 2017 - 04:55 PM

Okay, so I'm doing an exercise that will look at a list and see if there are 2 adjacent entries with the same value. If there are two entries with the same value assign True to 'duplicates'. Okay, simple and straight forward. if list[k] == list[k+1] then set duplicates to True, otherwise false, increment k and repeat. Simple right? I thought so.

I came up with:

if len(zipcode_list) != len(set(zipcode_list))
    duplicates = True
else:
    duplicates = False



the actual zipcode_list list is supplied by the MyProgrammingLab system.

Okay, well that wasn't the answer because it only checks for the presence of duplicates, not weather they are adjacent or not. So did some plotting and came up with:

while k < len(zipcode_list):
    duplicates = False
    k += 1
    if zipcode_list[k] == zipcode_list[k+1]:
        duplicates = True
    else:
        duplicates = False



as long as k is less then the length of the zipcode_list list, check each index with the next one in sequence to see if they contain the same value. increment K and repeat. Seemed like a winner.

Then MPL gave me the following "hints":
⇒ We think you might want to consider using: for
⇒ We think you might want to consider using: in
⇒ We think you might want to consider using: range

okay, it wants me to use a for loop. No biggie. Would have been nice to be told that up front but no biggie.

for k in range(len(zipcode_list)):
    duplicates = False
    k += 1
    if zipcode_list[k] == zipcode_list[k+1]:
        duplicates = True
    else:



Well this time MPL gave the following 'hints'
Remarks:
⇒ Unexpected identifiers: True, in, range

um... why are those unexpected? is this a syntax issue or is MPL suggesting I use a different approach to this problem?

Just for information, here is the problem as presented:

Quote

You are given a variable zipcode_list that refers to a list.

Write some code that assigns True to a variable duplicates if there are two adjacent elements in the list that have the same value, but that otherwise assigns False to duplicates otherwise. In order to accomplish this, you may, if you wish, use one other variable, k.

Use only k, zipcode_list, and duplicates.


Is This A Good Question/Topic? 0
  • +

Replies To: testing for the presence adjacent duplicates in a list

#2 woooee  Icon User is offline

  • D.I.C Head

Reputation: 45
  • View blog
  • Posts: 168
  • Joined: 21-November 12

Re: testing for the presence adjacent duplicates in a list

Posted 01 November 2017 - 10:34 PM

This does nothing as the for() increments it independent of anything like this statement
k += 1
Please don't post code that won't run. If you run this code you will get an out of bounds error on
if zipcode_list[k] == zipcode_list[k+1]:
The code posted only tests for the final element in the list being True or False.
if zipcode_list[k] == zipcode_list[k+1]:
    duplicates = True
else:
    duplicates = False 

This post has been edited by woooee: 02 November 2017 - 08:59 AM

Was This Post Helpful? 1
  • +
  • -

#3 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 318
  • View blog
  • Posts: 1,048
  • Joined: 27-December 13

Re: testing for the presence adjacent duplicates in a list

Posted 02 November 2017 - 12:37 AM

Let me point to some problems with your code (in-line comment):
for k in range(len(zipcode_list)):  # you want to compare two adjacent elements, maybe you should consider the range...
    duplicates = False  # for each pass of the loop 'duplicates' is reset to False - overwriting a possible True value
    k += 1  # not working, see woooee's comment
    if zipcode_list[k] == zipcode_list[k+1]:  # ok
        duplicates = True  # ok, but at this point you should break the loop
    else:  # skip

I hope this will be of help.
Was This Post Helpful? 1
  • +
  • -

#4 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 20-March 15

Re: testing for the presence adjacent duplicates in a list

Posted 02 November 2017 - 07:39 AM

okay, clearly I'm not understanding how a for loop works then. Why would
if zipcode_list[k] == zipcode_list[k+1]:

only test the final element? I thought the for loop would increment k, thus since k is being referenced to the index of zipcode_list it would compare that index. so if k is incremented to 2 it would read zipcode_list[2], and then I would compare to that to the next index, in this example zipcode_list[2]+1. The loop completes, then increments k to become 3 and it repeats.




View PostDK3250, on 02 November 2017 - 12:37 AM, said:

Let me point to some problems with your code (in-line comment):
for k in range(len(zipcode_list)):  # you want to compare two adjacent elements, maybe you should consider the range...
    duplicates = False  # for each pass of the loop 'duplicates' is reset to False - overwriting a possible True value
    k += 1  # not working, see woooee's comment
    if zipcode_list[k] == zipcode_list[k+1]:  # ok
        duplicates = True  # ok, but at this point you should break the loop
    else:  # skip

I hope this will be of help.


It helps me make the following (k and zipcode_list included for testing):

zipcode_list = [11111,11118,11112,11113,11114,11115,11116,11117,11111]
k = 0
while k <= len(zipcode_list):
    duplicates = False
    if zipcode_list[k] == zipcode_list[k+1]:
        duplicates = True
    else:
    break



only catch is I can't run the python interpreter on this computer and MPL is down for maintainance (why at 10am on a thursday?). So... does the code look good?

actually follow-up thought, is there an online Python system I could use to test the code? found repl.it. Do you have any thoughts about that? Is there another one you would suggest I use?

This post has been edited by Torroes.Prime: 02 November 2017 - 07:42 AM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6375
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: testing for the presence adjacent duplicates in a list

Posted 02 November 2017 - 07:49 AM

ideone.com is one that I tend to use, but there are others.
Was This Post Helpful? 1
  • +
  • -

#6 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 20-March 15

Re: testing for the presence adjacent duplicates in a list

Posted 02 November 2017 - 08:04 AM

View Postandrewsw, on 02 November 2017 - 07:49 AM, said:

ideone.com is one that I tend to use, but there are others.


checking ideone.com out now. What is you like about it? It seems very clunky and slow.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6375
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: testing for the presence adjacent duplicates in a list

Posted 02 November 2017 - 08:12 AM

Well, actually, I use it because it works for so many languages but, looking around, there are a number that are more specific to Python. Try some out ;)

(To me, they all do pretty much the same job, but some are prettier than others.)

Someone might come along with a more Python-esque recommendation ;)
Was This Post Helpful? 1
  • +
  • -

#8 Torroes.Prime  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 20-March 15

Re: testing for the presence adjacent duplicates in a list

Posted 02 November 2017 - 08:22 AM

View Postandrewsw, on 02 November 2017 - 08:12 AM, said:

Well, actually, I use it because it works for so many languages


Hey, that's a valid reason. I'm just starting out with programming, python is just the language I'm working with now.
Was This Post Helpful? 0
  • +
  • -

#9 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 318
  • View blog
  • Posts: 1,048
  • Joined: 27-December 13

Re: testing for the presence adjacent duplicates in a list

Posted 03 November 2017 - 10:21 AM

This thread took a diversion around running Python online.
Did you get any closer to solving the original question?
Was This Post Helpful? 0
  • +
  • -

#10 woooee  Icon User is offline

  • D.I.C Head

Reputation: 45
  • View blog
  • Posts: 168
  • Joined: 21-November 12

Re: testing for the presence adjacent duplicates in a list

Posted 03 November 2017 - 11:33 AM

Quote

okay, clearly I'm not understanding how a for loop works then. Why would
if zipcode_list[k] == zipcode_list[k+1]:

only test the final element?
It is not this code, but re-setting duplicates to True or False on every pass. No matter what the previous test did, the final element will determine whether duplicates is True or False
if zipcode_list[k] == zipcode_list[k+1]:
    duplicates = True
else:
    duplicates = False 

This post has been edited by woooee: 03 November 2017 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10681
  • View blog
  • Posts: 18,289
  • Joined: 19-March 11

Re: testing for the presence adjacent duplicates in a list

Posted 03 November 2017 - 11:42 AM

View Postandrewsw, on 02 November 2017 - 10:12 AM, said:

Someone might come along with a more Python-esque recommendation


Yep. They might. :)/>>

what about
any([i==j for i, j in zip(lst, lst[1:])


as in
>>> lst_no = [1, 2, 3, 4]
>>> lst_yes = [1, 2, 3, 3, 4]
>>> def adjacent_duplicates_p(lst):
...   return any([i==j for (i, j) in zip(lst, lst[1:])])
... 
>>> adjacent_duplicates_p(lst_no)
False
>>> adjacent_duplicates_p(lst_yes)
True
>>> 


I mean, now that you understand the for-loop version, this is a little slicker. (make sure you understand the pieces, though, there's a few python tricks here)
Was This Post Helpful? 0
  • +
  • -

#12 woooee  Icon User is offline

  • D.I.C Head

Reputation: 45
  • View blog
  • Posts: 168
  • Joined: 21-November 12

Re: testing for the presence adjacent duplicates in a list

Posted 03 November 2017 - 11:55 AM

And it is not even necessary to use zip, although readability might dictate a for() instead (4 closing parens at end is too many=confusing)
list_no = [1, 2, 3, 4]
list_yes = [1, 2, 3, 3, 4]

for this_list in [list_no, list_yes]:
    print(any(this_list[x]==this_list[x-1]
          for x in range(1, len(this_list)))) 

This post has been edited by woooee: 03 November 2017 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10681
  • View blog
  • Posts: 18,289
  • Joined: 19-March 11

Re: testing for the presence adjacent duplicates in a list

Posted 03 November 2017 - 12:24 PM

The basic for construction in python is for item in container not for number in sequence, and there's a reason for that. In python, you should generally prefer to avoid thinking about the indices of a list, and instead think about the contents of the list. This is less confusing and eliminates all sorts of indexing errors.
This is why I used the zip idiom, which is one that's worth getting familiar with.
Was This Post Helpful? 1
  • +
  • -

#14 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 318
  • View blog
  • Posts: 1,048
  • Joined: 27-December 13

Re: testing for the presence adjacent duplicates in a list

Posted 03 November 2017 - 12:34 PM

@jon.kiparsky: If you ever come to Denmark, let me know; I'd like to buy you a beer and discuss some Python...

This post has been edited by DK3250: 03 November 2017 - 12:35 PM

Was This Post Helpful? 1
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10681
  • View blog
  • Posts: 18,289
  • Joined: 19-March 11

Re: testing for the presence adjacent duplicates in a list

Posted 03 November 2017 - 12:50 PM

Sounds good, I'm game. I haven't been to Denmark, so I guess I'm about due for a visit...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1