Question

In: Computer Science

In Python, Given a list of numbers, return a list where all adjacent == elements have...

In Python,
Given a list of numbers, return a list where all adjacent == elements have been reduced to a single element, so [1,2,2,3,3,2,2,4] returns [1,2,3,2,4]. You may create a new list or modify the passed in list (set function does not work in this case).

Solutions

Expert Solution

list1 = [1,2,2,3,3,2,2,4]
print list1
list2 = []
for i in range(len(list1)):
if i != len(list1)-1 and list1[i] != list1[i+1]:
list2.append(list1[i])
  
if i == len(list1)-1:
if list1[i] != list1[i-1]:
list2.append(list1[i])

  
print list2

Output:

sh-4.3$ python main.py                                                                                                                                                                                                                                 

[1, 2, 2, 3, 3, 2, 2, 4]                                                                                                                                                                                                                               

[1, 2, 3, 2, 4]


Related Solutions

#Write a program in Python that given a list of positive integers, return another list where...
#Write a program in Python that given a list of positive integers, return another list where each element corresponds to the sum of the digits of the elements o#f the given list. #Example: # input # alist=[124, 5, 914, 21, 3421] # output # sum=[7, 5, 14, 3, 18]
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
python coding Suppose a list of positive numbers is given like the following list (remember this...
python coding Suppose a list of positive numbers is given like the following list (remember this is only an example and the list could be any list of positive numbers) exampleList: 15 19 10 11 8 7 3 3 1 We would like to know the “prime visibility” of each index of the list. The “prime visibility” of a given index shows how many numbers in the list with indexes lower than the given index are prime. For instance, in...
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
python3: Given a list of numbers in order order, return a new list sorted in non-decreasing...
python3: Given a list of numbers in order order, return a new list sorted in non-decreasing order, and leave the original list unchanged. function only def mergeSort(inputArray): #code here a = [3,4,6,1,21,11,9,8] b = mergeSort(a) print('original array is: ', a) print('sorted array is: ', b)
Given a list x = [2,3,5,6,7,8,9], write a python program that find those numbers which are...
Given a list x = [2,3,5,6,7,8,9], write a python program that find those numbers which are divisible by 3. Arrange these numbers in a list called “result”. Hint: similar to page 9 in the slides.
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal to n, such that the digit to the right is greater than the left digit (ai+1 > ai). E.g. if n=3 (123,124,125,……129,234,…..789)
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list. Write a while loop that prints the numbers from 1 to 10. Convert your previous loop into a for loop. Rewrite your for loop to only have one number in the range. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list. Write a while loop that prints the numbers from 1 to 10. Convert your previous loop into a for loop. Rewrite your for loop to only have one number in the range. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by...
Write a program in Python where you can swap only two consecutive elements. You have to...
Write a program in Python where you can swap only two consecutive elements. You have to show all steps to convert a string into another string (both strings will be anagrams of each other). E.g. GUM to MUG GUM GMU MGU MUG
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT