I have nested lists as entries to a Python dictionary. How do I print the nested list whose first elements is "S&qu
Date : March 29 2020, 07:55 AM
like below fixes the issue I have a dictionary that looks like this: def first_s(lst):
if not (isinstance(lst, list) and lst):
return None
if lst[0] == ['S']:
return lst
else:
for x in lst:
y = first_s(x)
if y:
return y
>>> print first_s(my_dict.values())
[['S'], [[[['E'], [['A', 'B'], ['C', 'D']]]], [[['G'], [['H', 'J'], [[['E'], [['A', 'B'], ['C', 'F']]]]]]]]]
|
Print nested element of xml with python etree
Date : March 29 2020, 07:55 AM
With these it helps If you are willing to use lxml, then the following is a solution which uses XPath: import re
from lxml.etree import fromstring
with open("ABD6ECF0-DC8E-41DE-89F2-1E36ED9D6535.xml") as f:
xmlstring = f.read()
xmlstring = re.sub(r'\sxmlns="[^"]+"', '', xmlstring, count=1)
doc = fromstring(xmlstring.encode()) # lxml only accepts bytes input, hence we encode
for title in doc.xpath('//title'): # for all title nodes
title_text = title.xpath('./text()') # get text value of the node
# get all text values of the paragraph nodes that appear lower (//paragraph)
# in the hierarchy than the parent (..) of <title>
paragraphs_for_title = title.xpath('..//paragraph/text()')
print(title_text[0] if title_text else '')
for paragraph in paragraphs_for_title:
print(paragraph)
|
how to print dictionary key with first element of nested list
Tag : python , By : user179445
Date : March 29 2020, 07:55 AM
|
Print a List inside a nested List that contains an element
Tag : list , By : Robert MacGregor
Date : March 29 2020, 07:55 AM
This might help you Does it matter that the values are two-dimensional coordinates? Is there an ordering on them that you must respect, or is it simply the ordering of the elements in the list? I will assume the latter. If you want to split a list at some point, the standard append/3 predicate is usually the way to go. For example, assume we want to cut the list [a, b, c, d, e] into a prefix containing the elements before c and a suffix containing the elements after c. Here is how that is done: ?- append(Prefix, [c | Suffix], [a, b, c, d, e]).
Prefix = [a, b],
Suffix = [d, e] ;
false.
?- append(Prefix, [c | Suffix], [a, b, c, d, e]), append(Prefix, [c], UpToAndIncludingC).
Prefix = [a, b],
Suffix = [d, e],
UpToAndIncludingC = [a, b, c] ;
false.
list_pivot_prefix(List, Pivot, Prefix) :-
append(Prefix0, [Pivot | _Suffix], List),
append(Prefix0, [Pivot], Prefix).
find_list(Lists, Element, Prefix) :-
member(List, Lists),
list_pivot_prefix(List, Element, Prefix).
?- find_list([[(1,2),(1,3),(1,4)],[(2,2),(2,3),(2,4)]],(1,3),List2).
List2 = [ (1, 2), (1, 3)] ;
false.
|
Python: Drop duplicate element within nested list, if element is an element within another nested list
Date : October 06 2020, 02:00 AM
I wish this helpful for you If order doesn't matter: use set operations on each pair of nested lists. Subtracting two sets produces a new set with only the elements from the first that don't appear in the second. Use the zip() function to pair up the nested lists from your two input lists. If your output must consist of nested lists again, convert the result of the set operation back to a list, with list() Use a list comprehension to process each pair of nested lists, creating a new list with the results. [list(set(b) - set(a)) for a, b in zip(listA, listB)]
[set(b) - set(a) for a, b in zip(listA, listB)]
>>> listA = [['A', 'B', 'C', 'D', 'E'], [1, 2, 3, 4, 5], ['!', '@', '#', '$', '%']]
>>> listB = [['E', 'A', 'T', 'F', 'W'], [5, 6, 8, 2, 9], ['@', '^', '&', '#', '*']]
>>> [list(set(b) - set(a)) for a, b in zip(listA, listB)]
[['W', 'F', 'T'], [8, 9, 6], ['^', '&', '*']]
>>> [set(b) - set(a) for a, b in zip(listA, listB)] # without list(...)
[{'W', 'F', 'T'}, {8, 9, 6}, {'^', '&', '*'}]
[[v for v in nested_b if v not in set_a] for set_a, nested_b in zip(map(set, listA), listB)]
>>> [[v for v in nested_b if v not in set_a] for set_a, nested_b in zip(map(set, listA), listB)]
[['T', 'F', 'W'], [6, 8, 9], ['^', '&', '*']]
|