Hi Team
I have a question to split the srr.txt read data it has 1,2,3,4,5,6,7,8,9,10 with is divided by 0 then display first then there next output. i,e 2,4,6,8 oput and 1,3,7,9 is next output. how can I achieve with strip using lambda?
Why does it have to be with lambda? Can’t you define a function to do
it?
Even if you need a one-liner, start by writing a many-line function to
solve the problem, and then compress it down to one-line.
Personally, I would do something like this:
with open('srr.txt') as f:
evens = []
odds = []
for line in f:
line = line.strip()
if not line:
continue
num = int(line)
if num % 2 == 1:
odds.append(num)
else:
evens.append(num)
odds.sort()
evens.sort()
evens.extend(odds)
print(evens) # technically evens followed by odds
Your question is unclear and hard to decipher, but if you just want to separate odds and evens, there is a partition recipe that is already implemented in a library. You can pip install more-itertools or choose to implement the recipe yourself.