Oct-15-2019, 12:01 PM
(This post was last modified: Oct-15-2019, 12:18 PM by newbieAuggie2019.)
(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 for your time and explanations!
I modified my code according to yours (after installing 'inflect'):
import inflect def commaCode(list1): p = inflect.engine() print(p.join(list1)) # print list spam1 = ['apples', 'bananas', 'tofu', 'cats'] commaCode(spam1) eggs1 = ['bird', 'dolphin', 'whale', 'parrot', 'gorilla', 'dog'] commaCode(eggs1) numbers1 = [1, 2, 3, 4, 5] commaCode(numbers1)and when I ran it:
Output:apples, bananas, tofu, and cats
bird, dolphin, whale, parrot, gorilla, and dog
Error:Traceback (most recent call last):
File "C:/Users/User1/AppData/Local/Programs/Python/Python37/atbs_04_commaCode_04.py", line 12, in <module>
commaCode(numbers1)
File "C:/Users/User1/AppData/Local/Programs/Python/Python37/atbs_04_commaCode_04.py", line 5, in commaCode
print(p.join(list1)) # print list
File "C:\Users\User1\AppData\Local\Programs\Python\Python37\lib\site-packages\inflect.py", line 3789, in join
if "," in "".join(words):
TypeError: sequence item 0: expected str instance, int found
(Oct-15-2019, 11:09 AM)buran Wrote: Mine is longer because it handles more cases, e.g. it can handle list with numerical values, range objects, generators, single element containers, empty containers, etc. If I just replicate your code (and inherent problems) it will be a one liner. The only case your code would handle is the range/list with numerical values
def print_nicely(iterable): print(f"'{', '.join(iterable[:-1])} and {iterable[-1]}'")or if you want to handle also range/list with numerical values
def print_nicely(iterable): print(f"'{', '.join(str(item) for item in iterable[:-1])} and {str(iterable[-1])}'")
I guess that's why I get that
Error:TypeError: sequence item 0: expected str instance, int found
(Oct-15-2019, 11:09 AM)buran Wrote: for example try with your code to print generator expression or empty list or single element listNot sure what's a generator expression.
for empty list will get IndexError, for generator expression - TypeError, single element list will look ugly', and bird.'
(Oct-15-2019, 11:09 AM)buran Wrote: if you are not familiar with map() you can use list comprehension instead (I've seen that Guido is in favour of comprehension compared to map)I'm really ashamed of myself for my still lack of knowledge. Not sure either of what's a list comprehension, but I think I can get it with your example.
iterable = [str(item) for item in iterable]
(Oct-15-2019, 10:37 AM)buran Wrote: I found it
https://github.com/jazzband/inflect
Very interesting! Thanks also for that!!!
Quote:[ ... ] convert numbers to words
[ ... ] # ADD CORRECT "a" OR "an" FOR A GIVEN WORD:
print("Did you want ", p.a(thing), " or ", p.an(idea))
# CONVERT NUMERALS INTO ORDINALS (i.e. 1->1st, 2->2nd, 3->3rd, etc.)
print("It was", p.ordinal(position), " from the left\n")
# CONVERT NUMERALS TO WORDS (i.e. 1->"one", 101->"one hundred and one", etc.)
# RETURNS A SINGLE STRING...
words = p.number_to_words(1234) # "one thousand, two hundred and thirty-four"
words = p.number_to_words(p.ordinal(1234)) # "one thousand, two hundred and thirty-fourth"
[ ... ] # JOIN WORDS INTO A LIST:
mylist = join(("apple", "banana", "carrot"))
# "apple, banana, and carrot"
mylist = join(("apple", "banana"))
# "apple and banana"
mylist = join(("apple", "banana", "carrot"), final_sep="")
# "apple, banana and carrot"
https://pypi.org/project/inflect/
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
"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