Recursively append over nested list python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to recursively append to a list while iterating over a nested list. For example, if I have a list [['a',['b',['c','d']]]], I would like a function to return [['a','b','c'],['a','b','d']]. My attempt so far is the following:
def recurse_append(data):
r_list=[]
for elem in data:
if isinstance(elem, list):
r_list.extend(recurse_append(elem))
else:
r_list.append(elem)
return r_list
which for this example returns ['a','b','c','d']. Unlike the example, the nested list that I am writing this for does not contain the same number of elements at each level, so I thought that a recursive function would be the best way to take this into account. Any help towards a solution would be greatly appreciated.

Related

how i can organize and use reverse in a dictionary python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
this is dictionary I don't know how to use sort and reverse.
for some reason don't print the dictionary
disc: {'clave1': ['a','b','c'],
'clave2': ['ship','car','house'],
'clave3': [2,1,3]}
I get that the 'disc' is a variable holding the dictionary.
In this case just sort and reverse the values that are lists.
disc = {'clave1': ['a','b','c'],
'clave2': ['ship','car','house'],
'clave3': [2,1,3]}
for l in disc.values():
l.reverse()
for l in disc.values():
l.sort()

How to use one line code to convert the dict to a string? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
d = {1:'a', 2:'b'}
#s = '1|a;2|b'
s = ';'.join([str(k)+'|'+d[k] for k in d])
Is there a better way to do this conversion?
I'd only make two small changes:
Use f-strings
Use a generator expression instead of a list comprehension, which would remove the need to hold all the values in memory prior to joining. It's not really a big deal unless you have thousaaaands of key/value pairs, though.
s = ';'.join(f'{k}|{d[k]}' for k in d)

How can I sort the a list of strings which contain date as substring [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Below is my list,
['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
And I need to sort the list and filter it to a below format. Please suggest a quicker way to achieve it
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']
When your format is fixed and always starts with "pending" you can use the normal sorted function and count the / in a list comprehension.
>>> values = ['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
>>> sorted(x for x in values if x.count('/') == 2)
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']

How To Remove an Element from a List Using Another List's Placeholder in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to remove a certain element from a list of dictionaries. Unfortunately, the only reference to the object needing removal is a place in another list:
enemy_list[num]
I need to take out enemy_list[num] from everyone.
ii = 0
for i in enemy_list:
print(ii, ':', enemy_list[ii])
ii += 1
num = input("Which of your opponents would you like to eliminate? ")
num = int(num)
del everyone[enemy_list[num]]
I cannot remove it from only enemy_list because it is reset multiple times as everyone in list everyone takes a turn.
I tried this question, but it only worked from removing entire lists from eachother. Is there a way I could change it to work for this case?
Thanks for your time!
everyone.remove(enemy_list[num])

List comprehension example I don't understand [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm having trouble understanding a particular list comprehension example. In the sample code below a list comprehension is used for the parameter of the set method. Most examples I've seen usually look like this "x for x in y". I've read documentation but it isn't clearing it up for me. What exactly is [marks for name] doing. Shouldn't it be [marks for marks]?
marksheet = []
for _ in range(0,int(input())):
marksheet.append([input(), float(input())])
second_highest = sorted(list(set([marks for name, marks in marksheet])))[1]
print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest]))
Your code is using Python's list comprehension to create a list from another list based on the condition.
See more about list comprehensions here:
https://www.programiz.com/python-programming/list-comprehension
The [1] is accessing the item at index 1 (2nd item) of your sorted list.
As for .join, each line in the sorted list is being printed out with a newline (\n) between each one.

Categories