Convert given list to nested list - Python3.x - python-3.x

I have a list in the below format :
input = [['Monday', 'Tuesday', 'Wednesday', '', '1', '2', 'Black', '']]
I would like to convert this to a nested list like below : (See, nested list breaks at null element)
output = [['Monday', 'Tuesday', 'Wednesday'], ['1', '2', 'Black']]
Is there a way to do this? I tried hard to think but could not come up with a solution.
More Examples :
input = [['', 'word1', 'word2', '', '1', '2', 'orange', '']]
output = [['word1', 'word2'],['1', '2', 'orange']]
input = [['', '', 'word1', 'word2', '', '1', '2', 'word3', '', '']]
output = [['word1', 'word2'],['1', '2', 'word3']]
Thanks in advance!!!

That should work:
import itertools
input_ = [['Monday', 'Tuesday', 'Wednesday', '', '1', '2', 'Black', '']]
output = [list(g) for item in input_ for k, g in itertools.groupby(item, bool) if k]
Longer solution:
input_ = [['Monday', 'Tuesday', 'Wednesday', '', '1', '2', 'Black', '']]
output = []
sublist = []
for item in input_:
for subitem in item:
if subitem:
sublist.append(subitem)
continue
if sublist:
output.append(sublist)
sublist = []

Here is a way to do it:
x = [['', '', 'word1', 'word2', '', '1', '2', 'word3', '', '']]
output = []
temp = []
for idx, el in enumerate(x[0]):
if idx == 0 and el == "":
continue
if el != '':
temp.append(el)
elif temp != []:
output.append(temp)
temp = []
Output
[['word1', 'word2'], ['1', '2', 'word3']]

Related

Need to generate a dictionary using comprehension

I have this code in python:
layers = ['two', 'three', 'five', 'six']
dict_toggle_bullet = {}
for x, y in enumerate(layers):
dict_toggle_bullet[y] = ["active" if j == y else "" for _, j in enumerate(layers)]
The output is:
{'two': ['active', '', '', ''], 'three': ['', 'active', '', ''], 'five': ['', '', 'active', ''], 'six': ['', '', '', 'active']}
Is there a way to convert this using dictionary comprehension in a single line?
This is what I came up with:
lst = ['two','three','five','six']
d = {lst[i]:['' if j != i else 'active' for j in range(4)] for i in range(4)}
However, I wouldn't advice you to do this as it seems a bit complicated and difficult to understand, so the direct method looks to be better.

How to compare particular element in list python3?

l1= [['1', 'apple', '1', '2', '1', '0', '0', '0'], ['1',
'cherry', '1', '1', '1', '0', '0', '0']]
l2 = [['1', 'cherry', '2', '1'],
['1', 'plums', '2', '15'],
['1', 'orange', '2', '15'],
['1', 'cherry', '2', '1'],
['1', 'cherry', '2', '1']]
output = []
for i in l1:
for j in l2:
if i[1] != j[1]:
output.append(j)
break
print(output)
Expected Output:
[['1', 'plums', '2', '15'], ['1', 'orange', '2', '15']]
How to stop iteration and find unique elements and get the sublist?
How to stop iteration and find unique elements and get the sublist?
To find the elements in L2 that are not in L1 based on the fruit name:
l1= [[1,'apple',3],[1,'cherry',4]]
l2 = [[1,'apple',3],[1,'plums',4],[1,'orange',3],[1,'apple',4]]
output = []
for e in l2:
if not e[1] in [f[1] for f in l1]: # search by matching fruit
output.append(e)
print(output)
Output
[[1, 'plums', 4], [1, 'orange', 3]]
You can store all the unique elements from list1 in a new list, then check for list2 if that element exists in the new list. Something like:
newlist = []
for item in l1:
if item[1] not in newlist:
newlist.append(item)
output = []
for item in l2:
if item[1] not in newlist:
output.append(item)
print(output)
This is slightly inefficient but really straightforward to understand.

Adding a string to an array adds all characters separately

Any suggestion how to merge it better so the dual digits numbers does not split?
Sorry for bad english.
def merge(strArr):
newList = []
for x in range(len(strArr)):
newList += strArr[x]
return newList
array_test = ["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"]
print(merge(array_test))
output =['1', ',', ' ', '3', ',', ' ', '4', ',', ' ', '7', ',', ' ', '1', '3', '1', ',', ' ', '2', ',', ' ', '4', ',', ' ', '1', '3', ',', ' ', '1', '5']`
expected output= [1,2,3,4,7,13,1,2,4,13,15]
Using list comprehension:
merged_arr = [n for s in array_test for n in s.split(", ")]
print(merged_arr)
This prints:
['1', '3', '4', '7', '13', '1', '2', '4', '13', '15']
It merges this way because for lists += is an array concatenation and that in this context your string object is interpreted as an array of characters:
[] += "Hello"
# Equivalent to
[] += ["H", "e", "l", "l", "o"]
If you want to join strings you can do:
out = "".join(array_test)
Your result becomes the way it is, because you take each inner string and add each character of it to your return-list without removing any spaces or commas.
You can change your code to:
def merge(strArr):
new_list = []
for inner in strArr:
new_list.extend(inner.split(", ")) # split and extend instead of += (== append)
return new_list
array_test =["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"]
merged = merge(array_test)
as_int = list(map(int,merged))
print(merged)
print(as_int)
Output:
['1', '3', '4', '7', '13', '1', '2', '4', '13', '15']
[1, 3, 4, 7, 13, 1, 2, 4, 13, 15]
Without as_int()you will still hav strings in your list, you need to convert them into integers.

Remove sequential duplicate word separated by delimiter

I am trying to remove sequential duplicate separated by delimiter '>' from journey column and also aggregate values under column uu and conv. I've tried
INPUT
a=[['journey', 'uu', 'convs'],
['Ct', '10', '2'],
['Ct>Ct', '100', '3'],
['Ct>Pt>Ct', '200', '10'],
['Ct>Pt>Ct>Ct', '40', '5'],
['Ct>Pt>Bu', '1000', '8']]
OUTPUT
a=[['journey', 'uu', 'convs'],
['Ct', '110', '5'],
['Ct>Pt>Ct', '240', '15'],
['Ct>Pt>Bu', '1000', '8']]
I tried below to split but it didn't work
a='>'.join(set(a.split()))
You need to split your string by > and then you could use groupby to eliminate duplicate items in your string. For example:
x = ['Ct>Pt>Ct>Ct', '40', '5']
print(">".join([i for i, _ in groupby(x[0].split(">"))]))
# 'Ct>Pt>Ct'
You could use this as a lambda function in another groupby to aggregate the lists. Then sum each element of the same index by using zip. Check it out:
a=[['journey', 'uu', 'convs'],
['Ct', '10', '2'],
['Ct>Ct', '100', '3'],
['Ct>Pt>Ct', '200', '10'],
['Ct>Pt>Ct>Ct', '40', '5'],
['Ct>Pt>Bu', '1000', '8']]
from itertools import groupby
result = [a[0]] # Add header
groups = groupby(
a[1:],
key=lambda x: ">".join([i for i, _ in groupby(x[0].split(">"))])
)
# groups:
# ['Ct, '[['Ct', '10', '2'], ['Ct>Ct', '100', '3']]]
# ['Ct>Pt>Ct', [['Ct>Pt>Ct', '200', '10'], ['Ct>Pt>Ct>Ct', '40', '5']]]
# ['Ct>Pt>Bu', [['Ct>Pt>Bu', '1000', '8']]]
for key, items in groups:
row = [key]
for i in zip(*items):
if i[0].isdigit():
row.append(str(sum(map(int, i))))
result.append(row)
print(result)
Prints:
[['journey', 'uu', 'convs'],
['Ct', '110', '5'],
['Ct>Pt>Ct', '240', '15'],
['Ct>Pt>Bu', '1000', '8']]

I want to find the highest number using import re .txt

I want to be able to just search for all digits in a text file and find the max number, using the re module. Where do I need to edit my code to accomplish this goal?
The random.txt looks somthing like the following:
Datq15UxkNwMN5zUQhd46J8WeF9RjAq214TlJiQ8EkZvmdOpmBOdd365mICKC67GGvqwbLqV2Gox3n3E5WC1Vq8C22lZ6sL3Ip24untQyw46g2219WlA07GP30PNvc8o3hCb2d283l68mh86RH6gDNbN7kIXmdO4a84hUz73905o3BlR71YCQF985JTz54FRoN32pM8N23YcYd7jv9Ys575UzaH9RZ7sosMdeqnTgnVt0bH99b2P5ilvJ33QaJ6G76VU8vPN
import re
with open('content.txt', 'r',) as f:
contents = f.read()
number = 0
pattern = re.compile(r'\d')
matches = pattern.finditer('content.txt')
for match in matches:
n = int(match)
if saved <= n:
number = int(match)
print(number)
the file just ran once and gave me the answer 0
Try this,
import re
with open('file1.txt', 'r') as f:
data = f.read()
list_of_numbers = re.findall(r'(?:[\d]+)',data)
list_of_numbers = map(int, list_of_numbers)
print(max(list_of_numbers))
Output:
73905 # max number
your list_of_numbers look like this
['15', '5', '46', '8', '9', '214', '8', '365', '67', '2', '3', '3', '5', '1',
'8', '22', '6', '3', '24', '46', '2219', '07', '30', '8', '3', '2', '283', '68',
'86', '6', '7', '4', '84', '73905', '3', '71', '985', '54', '32', '8', '23', '7',
'9', '575', '9', '7', '0', '99', '2', '5', '33', '6', '76', '8']
You are looking for matches within "content.txt" string so there is no match. Also a MatchObject can't be converted to int:
import re
with open('content.txt', 'r',) as f:
contents = f.read()
number = 0
pattern = re.compile(r'\d+')
matches = pattern.finditer(contents)
for match in matches:
n = int(match.group(0))
if number <= n:
number = int(match.group(0))
print(number)

Resources