Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The input:

 lst=[ '  22774080.570 7                   1762178.392 7   1346501.808 8  22774088.434 8
',
     '  20194290.688 8                  -2867460.044 8  -2213132.457 9  20194298.629 9
']

The desired output:

['22774080.570 7','none','1762178.392 7','1346501.808 8','22774088.434 8'],
['20194290.688 8','none','-2867460.044 8', .... ]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
132 views
Welcome To Ask or Share your Answers For Others

1 Answer

I've made a few assumptions here, and this is probably not the most elegant solution, but given that your matching None based on the length of space, this will work

[[(None if a.strip() == 'None' else a.strip()) 
    for a in l.replace('                  ', '  None  ').split('  ')  if a != ''
    ] for l in lst]

this is currently implemented as a list comprehension, but could easily be spread over multiple lines for readability as such

new_list = []
for l in lst:
    sub_list = []
    for a in l.replace('                  ', '  None  ').split('  '):
        if a != '':
            item = a.strip()
            sub_list.append(None if item == 'None' else item)
    new_list.append(sub_list)

either method yields the same result:

[
 [
  '22774080.570 7', None, '1762178.392 7', '1346501.808 8', '22774088.434 8'
 ], 
 [
  '20194290.688 8', None, '-2867460.044 8', '-2213132.457 9', '20194298.629 9'
 ]
]

Bear in mind that any changes to the width of spaces used to define 'None' will impact the function of this implementation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...