Python Forum
clean up list elements and replace
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
clean up list elements and replace
#1
Dear Python Experts,

I am looking for a loop that goes through my elements in the list and removes
the comma , and ' ' signs. All other strings should remain and occurences like "March" be replaced by a number like 3/.
Is there a neat way to do it?

mylist = [[('','03/25/93','','','','','','','','','','','','','','','')],
[('', '', '', 'March 1976', '', '', '', '', '', '', '', '', '', '', '', '', '')]]
Exptected output:
mylist = [['03/25/93'],['3/1976']]


I would appreciate any help.
Reply
#2
We would appreciate you showing us what you had tried so far and explaining how it wasn't working. Then we would be glad to help.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Is there only one date formatting ('monthname yearno') which must be converted? If so, following code should work (requires Python 3.6 or newer due to f-string):

import calendar                        # to convert month name to month num


my_new_list = []  
                     
for row in mylist: 
    my_new_list.append([])             
    dates = filter(str.strip, row[0])   # get rid of empty strings in rows tuple
    for date in dates:
        converted = date.split()
        if len(converted) == 2:
            monthnum = list(calendar.month_name).index(converted[0])
            my_new_list[-1].append(f'{monthnum}/{converted[1]}')
        else:
            my_new_list[-1].append(date)
        
Desired values should be in my_new_list
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Hi perfringo,

thanks for your suggestion.

The dates are of various formats, I wish I could do date conversion in a second, seperate step.
Right now, what worries me most is the clean up.
I tried the following:

mylist = [[('','03/25/93','','','','','','','','','','','','','','','')],
        [('', '', '', 'March 1976', '', '', '', '', '', '', '', '', '', '', '', '', '')]]
mylist2 = [_ for _ in mylist if '' not in _]

#or

mylist2 = list(filter(lambda t: '' not in t, mylist))
        
No luck. Just doesnt work.
Reply
#5
(Jul-25-2018, 01:50 PM)metalray Wrote: Right now, what worries me most is the clean up.
>>> lst = [set(i[0]) for i in mylist]
>>> lst
[{'', '03/25/93'}, {'', 'March 1976'}]
>>> [i.discard('') for i in lst]
[None, None]
>>> my_list = [i.pop() for i in lst]
>>> my_list
['03/25/93', 'March 1976']
pip install python-dateutil
>>> from dateutil import parser
>>> 
>>> dt = parser.parse(my_list[1])
>>> dt
datetime.datetime(1976, 3, 25, 0, 0)
>>> print(f'Month is {dt.month} and year {dt.year}')
Month is 3 and year 1976
Reply
#6
(Jul-25-2018, 01:50 PM)metalray Wrote: Hi perfringo,

Right now, what worries me most is the clean up.

In snippet I provided there is row 8 which conveniently commented: # get rid of empty strings in rows tuple.

I walk you through some possible solutions to get rid of empty strings (written like in Python interactive interpreter).

Easiest way is to modify code in snippet provided, get rid parts of code where conversion is taking place:

>>> mylist = [[('','03/25/93','','','','','','','','','','','','','','','')],
... [('', '', '', 'March 1976', '', '', '', '', '', '', '', '', '', '', '', '', '')]]
>>> my_new_list = []
>>> for row in mylist:
...     my_new_list.append(filter(str.strip, row[0]))
...
>>> my_new_list
[<filter object at 0x105aac5c0>, <filter object at 0x105aac6a0>]
Filter objects are iterators and their location shown here are abritrary in specific computer. If you are not comfortable to work with filter objects / iterators (but you should) you can alternatively use list comprehension and do following:

>>> my_new_list = []
>>> for row in mylist:
...     my_new_list.append([date for date in row[0] if date])
...
>>> my_new_list
[['03/25/93'], ['March 1976]]
There is logical step from here. You can fit it all into one-liner with nested list comprehension:

>>> [[date for date in row[0] if date] for row in mylist]
[['03/25/93'], ['March 1976']]
This is datastructure you stated as expected output. I should mention, that this is not same as mylist structure as expected output is missing tuple level. I don't no whether is important or not.

Some words of unsolicited wisdom: you have to chew your food.

This means that if you ask for help and get one you should pay attention to that and try to modify ideas provided to suit your needs. No one will solve your problems but yourself. There are people out there who are willing to help you but not do your work for you.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(Jul-25-2018, 01:50 PM)metalray Wrote: I tried the following:

mylist = [[('','03/25/93','','','','','','','','','','','','','','','')],
        [('', '', '', 'March 1976', '', '', '', '', '', '', '', '', '', '', '', '', '')]]
mylist2 = [_ for _ in mylist if '' not in _]

#or

mylist2 = list(filter(lambda t: '' not in t, mylist))
        
No luck. Just doesnt work.

You must ask yourself - what I want to accomplish?

What I would have done:
- goal: to get rid empty strings in datastructure (in tuples inside list of lists)

Steps:
- how to get rid empty strings in sequence/iterable
- how to wrap it so that it works on my datastructure

How to get rid of empty strings in iterable?

As suggested by PEP-8 Style Guide for Python Code >>> Programing recommendations: For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

Empowered by this knowledge we can have (list) comprehension:

[date for date in mylist if date]
if date part of code checks whether string is true (non-empty).

First step accomplished, second to go.

Lets try to apply this comprehension on sequence which is inside list of lists (matrix). If order to do so we nest comprehension:

[[date for date in row if date] for row in mylist]
This is not it, but we are getting closer. We are accessing list inside list but there is tuple inside that list. Fortunately for us it appears that there is only one tuple so we can take advantage of using index:

[[date for date in row[0] if date] for row in mylist]
Step two accomplished, goal achieved.

Comprehension is very powerful tool but at times it hard to comprehend Smile

This simple example may help you crasp syntax:

[letter for word in sentence for letter in word]
In human readable language: for every word in sentence for every letter in word return letter.

Don't let 'letter for word' at the beginning to fool you. It is actually:

for word in sentence:
    for letter in word:
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
Wow, Thanks perfringo!
That is something to chew on :D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 424 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Can i clean this code ? BSDevo 8 939 Oct-28-2023, 05:50 PM
Last Post: BSDevo
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 470 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Checking if a string contains all or any elements of a list k1llcod3 1 1,094 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 1,955 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  Clean Up Script rotw121 2 1,006 May-25-2022, 03:24 PM
Last Post: rotw121
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,113 May-17-2022, 11:38 AM
Last Post: Larz60+
  Replace elements of array with elements from another array based on a third array Cola_Reb 6 1,831 May-13-2022, 06:06 PM
Last Post: deanhystad
  How to clean UART string Joni_Engr 4 2,476 Dec-03-2021, 05:58 PM
Last Post: deanhystad
  Why am I getting list elements < 0 ? Mark17 8 3,117 Aug-26-2021, 09:31 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020