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

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 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...
a. What is the difference between atoms and molecules? b. List all elements that cannot have...
a. What is the difference between atoms and molecules? b. List all elements that cannot have an "expanded octet."
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
List Customer and Product Without Sale In one list return, all customers who do not have...
List Customer and Product Without Sale In one list return, all customers who do not have an invoice and all products that were not sold. Use the UNION operator in this query. Return 3 values: - category - Is this line related to "customer" or "product"? Print "customer" or "product" without quotes. - id - customer.id (category="customer") or product.id (category="product") - name - customer.customer_name (category="customer") or product.product_name (category="product") Table definitions and a data sample are given below. Schema Table: customer...
Python: I am currently a function that will return a list of lists, with the frequency...
Python: I am currently a function that will return a list of lists, with the frequency and the zip code. However, I am having a difficult time organizing the list in decreasing order of frequency. We are asked to use sort () and sorted () instead of lambda. The function will find 5 digit zip codes, and when it sees a 9-digit zip code, the function needs to truncate the last 4 digits. The following is an example of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT