Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Back again on f-strings
#6
(Oct-15-2019, 10:00 AM)buran Wrote:
def print_nicely(iterable):
    iterable =  list(map(str, iterable))
    if len(iterable) > 1:
        print(f"'{', '.join(iterable[:-1])} and {iterable[-1]}'")
    elif len(iterable) ==  1:
        print(f"'{iterable[0]}'")
    else:
        raise ValueError('Expecting non-empty iterable')

print_nicely(['apples', 'bananas', 'tofu', 'cats']) # print list
print_nicely(['bird']) # print single element list
print_nicely(range(4)) # print range object
print_nicely([])
Output:
'apples, bananas, tofu and cats' 'bird' '0, 1, 2 and 3' Traceback (most recent call last): File "***", line 13, in <module> print_nicely([]) File "***", line 8, in print_nicely raise ValueError('Expecting non-empty iterable') ValueError: Expecting non-empty iterable
by the way i may have seen a package on PyPI that does it but cannot find it at the moment

Thanks a lot!

Although I like your creative way of thinking, in this case, you have transformed the 4 lines I wanted to simplify, using f-strings:

def commaCode(list1):
    print("'", end='')
    print(*list1[:-1], sep=', ', end=",")
    print(' and', list1[-1], end=".'\n\n")
into actually 8 lines of code (maybe 6 if we eliminate the last 2 lines):

def print_nicely(iterable):
    iterable =  list(map(str, iterable))
    if len(iterable) > 1:
        print(f"'{', '.join(iterable[:-1])} and {iterable[-1]}'")
    elif len(iterable) ==  1:
        print(f"'{iterable[0]}'")
    else:
        raise ValueError('Expecting non-empty iterable')
I think that as I don't know about 'map()', I prefer Perfringo's one liner in this case, but I have not sorted out the problem with the numerical list yet.

Thanks and all the best,

(Oct-15-2019, 10:37 AM)buran Wrote: I found it
https://github.com/jazzband/inflect

import inflect
p = inflect.engine()
print(p.join(['apples', 'bananas', 'tofu', 'cats'])) # print list
print(p.join(['bird'])) # print single element list
print(p.join(list(map(str, range(4))))) # print range object
print(p.join([]))
Output:
apples, bananas, tofu, and cats bird 0, 1, 2, and 3

Thanks a lot!

That looks to me much clearer than the other one.

Thanks and all the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Messages In This Thread
Back again on f-strings - by newbieAuggie2019 - Oct-15-2019, 09:40 AM
RE: Back again on f-strings - by perfringo - Oct-15-2019, 09:50 AM
RE: Back again on f-strings - by newbieAuggie2019 - Oct-15-2019, 10:29 AM
RE: Back again on f-strings - by buran - Oct-15-2019, 10:00 AM
RE: Back again on f-strings - by newbieAuggie2019 - Oct-15-2019, 10:48 AM
RE: Back again on f-strings - by buran - Oct-15-2019, 10:37 AM
RE: Back again on f-strings - by buran - Oct-15-2019, 11:09 AM
RE: Back again on f-strings - by newbieAuggie2019 - Oct-15-2019, 12:01 PM
RE: Back again on f-strings - by perfringo - Oct-15-2019, 11:52 AM
RE: Back again on f-strings - by newbieAuggie2019 - Oct-15-2019, 12:27 PM
RE: Back again on f-strings - by newbieAuggie2019 - Oct-16-2019, 11:24 PM
RE: Back again on f-strings - by buran - Oct-15-2019, 12:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 858 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  i'm back to f-strings, again Skaperen 4 1,145 Sep-21-2022, 08:13 PM
Last Post: Gribouillis
  Splitting strings in list of strings jesse68 3 1,848 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,570 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,290 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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