Unable to transform the following nested for loop to a list comprehension:
for row in rows:
elements = row.strip().split('\t')
for element in elements:
print(element)
Input Data is tab delimited:
ola olb olc old
ole olf olg olh
oli olj olk olk
oll olm oln ooo
Desired Output:
ola
olb
olc
old
ole
olf
olg
olh
oli
olj
olk
olk
oll
olm
oln
ooo
Like this
with open('tabdelim.txt') as rows:
lstcmp = [item for row in rows for item in row.strip().split('\t')]
print('\n'.join(lstcmp))
sum([row.strip().split('\t') for row in rows],[])
内置sum
对于拉平列表列表非常有用。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.