L=[1,2,3,4,5,[5,6,7],85,96] so remove inside list squre bracktes how? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 4

L=[1,2,3,4,5,[5,6,7],85,96] so remove inside list squre bracktes how?

output is [1,2,3,4,5,5,6,7,8,85,96]

5th Jul 2018, 6:56 AM
lokesh reddy
lokesh reddy - avatar
11 ответов
+ 4
Hi Lokesh. Much more advanced way of tackling this problem is to use the recursion method. https://code.sololearn.com/caH5zTZmlcZV/#py # function: list_value_extract # # This function let you extract all the elements values of nested lists into a new list. # @param int|string|list param # @param list ext_list # return void def list_value_extract (param, ext_list = []): # enter if the supplied param is a list if (type (param) is list): # loop through the list and process it for value in param: # if the list element is a list again, then use recursion method if (type (value) is list): list_value_extract (value) else: # if the list element is a simple value, then append to the new list ext_list.append(value) # enter if the supplied is a single value else: ext_list.append(param) # return the extracted values array return ext_list; L = [1,2,3,4,5,[5,[6],7],85,96] print (list_value_extract(L))
5th Jul 2018, 9:20 AM
Vinod Chandran
+ 2
𝕾𝖆𝖌𝖆𝕿𝖍𝖊𝕲𝖗𝖊𝖆𝖙 💯, as requested. This is what KrOW was describing. This will expand the embedded lists in place and will handle multiple layers deep as well. https://code.sololearn.com/cB0Ts19S5KdS/?ref=app
7th Jul 2018, 12:47 AM
1_coder
5th Jul 2018, 7:18 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
+ 1
1) create another empty list 2) loop on original list items 3) for any item if it its not a list add to copy list, else repeat from poin 1) but using this item like original
5th Jul 2018, 7:19 AM
KrOW
KrOW - avatar
+ 1
tqs pulkit..
5th Jul 2018, 7:36 AM
lokesh reddy
lokesh reddy - avatar
+ 1
𝕾𝖆𝖌𝖆𝕿𝖍𝖊𝕲𝖗𝖊𝖆𝖙 💯 Sure... You have to take cure of current index in iteration and "inflate" new items with it Omar Sarr OPEN A NEW DISCUSSION for different problen please
5th Jul 2018, 8:31 AM
KrOW
KrOW - avatar
+ 1
1_coder nice code. You deserve best answer bedge for this question. for making this program more user firendly take input list from user using eval. after that your program is complete. Thanks for the code. A perfect code. 💯
7th Jul 2018, 4:14 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
Pulkit Kamboj KrOW can't we do it on same list?
5th Jul 2018, 8:05 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
By the way Omar Sarr this can give you some idea of what you want to do https://code.sololearn.com/c58a7JighZSz/?ref=app
5th Jul 2018, 9:05 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
A NEW APPROACH one liner✓✓✓✓✓. it can even merge multi-dimensional lists https://code.sololearn.com/cyMbKWg2E504/?ref=app
6th Jul 2018, 11:09 PM
Mr. Bot
Mr. Bot - avatar
- 1
Hello, Please, can someone help me solve this problemWrite a program that will take in a user input, this input will determine how many times the dice will roll, and will use two random numbers to "roll" the dice, after each roll, please output the to random numbers for each roll. Ex. input a 2 and Roll 1, die1= 6 die2=3 Roll 2, die1=3 die2=1 The range of the random numbers should be between 1-6.
5th Jul 2018, 7:58 AM
Omar Sarr