I'm trying to use the new assignment expression for the first time and could use some help.
Given three lines of log outputs:
sin = """Writing 93 records to /data/newstates-900.03-07_07/top100.newstates-900.03-07_07/Russia.seirdc.March6-900.12.csv ..
Writing 100 records to /data/newstates-900.03-07_07/top100.newstates-900.03-07_07/India.seirdc.March6-900.6.csv ..
Writing 100 records to /data/newstates-900.03-07_07/top100.newstates-900.03-07_07/US.seirdc.March6-900.15.csv ..
"""
The intent is to extract just the State
(Russia, India and US) and the record count (93,100,100) . So the desired result is:
[['Russia',93],['India',100],['US',100]]
This requires the following steps to be translated into Python:
['Writing', '93', 'records', 'to', '/data/newstates-900.03-07_07/top100.newstates-900.03-07_07/Russia.seirdc.March6-900.12.csv', '..']
Russia.seirdc.March6-900.12.csv
Russia
Here is my incorrect attempt:
import fileinput
y = [[ z[4].split('/')[-1].split('.')[0],z[1]]
for (z:=x.split(' ')) in
(x:=sin if sin else fileinput.input()).splitlines())]
Is this good enough?
[[(wrds := line.split())[4].split("/")[-1].split('.')[0], wrds[1]] for line in sin.splitlines()]
I find using assignment expression redundant. You can also do this:
[[line.split('/')[-1].split('.')[0], line.split()[1]] for line in sin.splitlines()]