Question

In DrRacket, is there any way I can change (list 1 (list 2 (list 3 4...

In DrRacket, is there any way I can change (list 1 (list 2 (list 3 4 5) 6 )7) to (list 1 2 3 4 5 6 7)?

Homework Answers

Answer #1

Solution to given question:

Yes, there are multiple ways to convert any nested list to normal form

Three possible ways mnetioned here:

=======================================

1) Code

import collections

from itertools import chain

#input list called L

L = [1,2,[3,4,5],6,7]

#if an item in the list is not already a list, then put it in one.

a = [i if isinstance(i, collections.Iterable) else [i,] for i in L]

#flattens out a list of iterators

b = list(chain.from_iterable(a))

print b

#[1, 2, 3, 4, 5, 6, 7]

===============================================================

2)

L = [1,2,[3,4,5],6,7]

new_list = []

[new_list.extend(x) if type(x) is list else new_list.append(x) for x in L]

print new_list

(or)

3)

new_list2 = []

for item in L:

    if type(item) is list:

        new_list2.extend(item)

    else:

        new_list2.append(item)

print new_list2

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(Python 3) If I have a list of keys and 3 lists of values, how can...
(Python 3) If I have a list of keys and 3 lists of values, how can I append these values into an existing key, value pair in a dictionary? Say I have: mykeys = ["John", "Sarah", "Lexi, "Cass"] values1 = [3, 5, 2, 6] values2 = [17, 18, 12, 21] values3 = [4, 7, 3, 0] How can I make it so my dictionary contains all three values in each of the keys. i.e.: {"John" : [3, 17, 4], "Sarah":...
is there any way that I can capitalize argv, such as argv[1] = hello, and argv[2]...
is there any way that I can capitalize argv, such as argv[1] = hello, and argv[2] = world to argv[1] = HELLO, argv[2] = WORLD without using strlen or toupper or copy them into new arry?
Suppose, I = {set of locations for establishing a hospital} = {1, 2, 3, 4, 5,...
Suppose, I = {set of locations for establishing a hospital} = {1, 2, 3, 4, 5, 6, 7} xi is a decision variable which equals 1 if a hospital is set up at location i; otherwise, xi = 0. The following are a list of constraints. I'd like to know how to formulate them in terms of x. Constraint 1: If locations 6 and 7 are selected, then location 3 is also selected Constraint 2: If 6 or 7 are...
1) a) From the set {-8, -2/3, 5i, √(-9), √2, 0, 3+3i, -2.35, 7} i) List...
1) a) From the set {-8, -2/3, 5i, √(-9), √2, 0, 3+3i, -2.35, 7} i) List the set of Natural Numbers ii) List the set of Integers iii) List of the set of Rational Numbers vi) List the set of Real Numbers 2)Solve the following pairs of simultaneous equations 3x + y = 7 and 2x - 2y = 2 b) i) -30 ÷ -6 - (-12 + 8) – 4 x 3 = c)Calculate the simple interest earned if...
Can you pls do the uncertainty for 1. 54.52cm 2. 0.1209m 2. 2.690g 3. 43.07cm 4....
Can you pls do the uncertainty for 1. 54.52cm 2. 0.1209m 2. 2.690g 3. 43.07cm 4. 6,352,001g 5. 7000mm 6. 7000.0mm 7 . 0.7000mm And please type your answer so I can understand what you are writing and is with measuremnt
I have a 6 sided die with the numbers 1, 2, 3, 3, 4, 5 on...
I have a 6 sided die with the numbers 1, 2, 3, 3, 4, 5 on it. I also have a hat filled with numbers. When I pick a number out of the hat, I get a number between 1 and 10. The hat number has a mean of 2 and a standard deviation of 2.5. I make a new random variable by combining these two previous random variables. The new random variable is made by taking the number I...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Average number of coronavirus patients received in Neftchilar Hospital per hour is equal to the sum of the first 3 values in your dataset. Find the probability that the number of patients received in any 30 minutes interval will be at least 2?
1. A researcher wants to test whether color can influence recall of words in a list....
1. A researcher wants to test whether color can influence recall of words in a list. To test this, the researcher displays 20 words on a computer screen. Ten words are in color, and 10 words are in black. All words are presented on a white background. Participants are given 1 minute to view the list, and then the list is taken away and participants are allowed 1 additional minute to write down as many words as they can recall....
   4.     a) i) If 4 individuals are selected from a group of 7 people,                    how...
   4.     a) i) If 4 individuals are selected from a group of 7 people,                    how many possible selection option could be made?                                 ii) In a competition prizes are given to 6 of the 10 participants.                  How many possible prize lists could be made if they are given different prizes?                                                                                                                                 b) In how many ways can the letters of the word SEMESTER be rearranged?                                                                                                             5) If the fifth term of a AP is...
Find the Mean, Median, and the Mode of the following values: i 1 2 3 4...
Find the Mean, Median, and the Mode of the following values: i 1 2 3 4 5 6 7 8 9 10 x 10 15 15 17 21 24   24   25 28 32
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT