iterations destroy reversed() results
Hi, reversed() results are fine until iterated over, after which the results are no longer available. This was discovered after using something like this: rev = reversed( sorted( list ) ) sr = sum( 1 for _ in rev ) # rev is now destroyed So reversed() results can only be iterated once unlike sorted(), etc... Script to illustrate the issue: /tmp/rev: orig = [ 'x', 'a', 'y', 'b', 'z', 'c' ] co = sum( 1 for _ in orig ) print( 'orig', orig, co ) # reversing rev = reversed(orig) print( 'before iteration:', [ x for x in rev ] ) # list comprehension was an iteration over 'rev' print( 'after iteration:', [ x for x in rev ] ) # how this was discovered... orig = [ 'x', 'a', 'y', 'b', 'z', 'c' ] rev = reversed(orig) cr = sum( 1 for _ in rev ) print( 'after sum():', [ x for x in rev ] ) which produces: $ python /tmp/rev orig ['x', 'a', 'y', 'b', 'z', 'c'] 6 before iteration: ['c', 'z', 'b', 'y', 'a', 'x'] after iteration: [] after sum(): [] Regards, Pierre -- https://mail.python.org/mailman/listinfo/python-list
Re: Newline (NuBe Question)
On Wed, 15 Nov 2023 16:51:09 - Grizzy Adams via Python-list wrote: I don't give solutions; just a nudge... you appear not to fully grok "list"; your list is ONE list with no delineation between students. You want a "list of lists"... >['Example High', 'Mary', 89.6, 'Pass', 'Example High', 'Matthew', 76.5, >'Fail', 'Example High', 'Marie', 80.4, 'Fail', 'Example High', 'Manuel', 79.6, >'Fail', 'Example High', 'Malala', 98.9, 'Pass'] Like this: students = [ ['Example High', 'Mary', 89.6, 'Pass'], ['Example High','Matthew', 76.5, 'Fail'], ['Example High', 'Marie', 80.4, 'Fail'], ['Example High', 'Manuel', 79.6, 'Fail'], ['Example High', 'Malala', 98.9, 'Pass'] ] This may help get you headed in the right direction: for s in students: print( s ) Hint: look forward to learning about f-strings... HTH, Pierre -- https://mail.python.org/mailman/listinfo/python-list
Re: How to Add ANSI Color to User Response
On Thu, 11 Apr 2024 04:50:49 +1000 WordWeaver Evangelist via Python-list wrote: >Hello List, > >I have a simple question. I use the following textPrompt in some of my Jython >modules: > '\n[1;33mYour choice is? (A B C D E): ', maxChars=1, autoAccept=False, > forceUppercase=True) >Is there a way to add an ANSI color code to the end where the conditions are, >so that the color of the user’s input is of a color of my choosing, instead of >just white? >Thank you very much in advance. >Kind regards, >Bill Kochman Over the years, I've tried different mechanisms for applying colors until I got my hands on f-stings; then I created a tiny module with all the colors (cR, cG, etc) which made my life so much simpler (attached). The module includes background colors (bX); but I very rarely use those. Then, I just use the module like this: # place the module in a directory where your script is # e.g., $ mkdir mymods (rename as desired) from mymods.colors import * # or just include the contents inline # this simply switches from one color to the next print( f"{cR}red, {cB}blue, {cG}green {cO}are colors." ) # color just the response ans = input( f"Answer?: {cG}" ) # turn off color on next line print( f"{cO}You entered: {cY}{ans}{cO}" ) # # to turn off each color (white commas), change the above to: print( f"{cR}red{cO}, {cB}blue{cO}, {cG}green {cO}are colors." ) On Windows, you'll need to add this *before* using the colors: import os if os.name == 'nt': # Only if we are running on Windows from ctypes import windll w = windll.kernel32 # enable ANSI VT100 colors on Windows. w.SetConsoleMode(w.GetStdHandle(-11), 7) HTH, Pierre -- https://mail.python.org/mailman/listinfo/python-list
Re: How to Add ANSI Color to User Response
On Thu, 11 Apr 2024 05:00:32 +0200 Gisle Vanem via Python-list wrote: >Pierre Fortin wrote: > >> Over the years, I've tried different mechanisms for applying colors until >> I got my hands on f-stings; then I created a tiny module with all the >> colors (cR, cG, etc) which made my life so much simpler (attached). > >Attachments are stripped off in this list. >It would be nice to see this tiny module of yours. >An URL or attach as inline text please. #!/bin/python # -*- mode: python; -*- # Copyright: #2024-Present, Pierre Fortin # License: #GPLv3 or any later version: https://www.gnu.org/licenses/gpl-3.0.en.html # Created: #2023-11-10 Initial script # Updated: # Usage: f"{cR}red text {cG}green text{cO}; colors off" #or: print( cY, "yellow text", cO ) # VT100 type terminal colors ESC = "\u001b"; # Foreground Colors _black = f"{ESC}[30m"; _red = f"{ESC}[31m"; _green = f"{ESC}[32m"; _yellow = f"{ESC}[33m" _blue = f"{ESC}[34m"; _magenta = f"{ESC}[35m"; _cyan = f"{ESC}[36m"; _white = f"{ESC}[37m" # Background Colors _black_ = f"{ESC}[40m"; _red_ = f"{ESC}[41m"; _green_ = f"{ESC}[42m"; _yellow_ = f"{ESC}[43m" _blue_ = f"{ESC}[44m"; _magenta_ = f"{ESC}[45m"; _cyan_ = f"{ESC}[46m"; _white_ = f"{ESC}[47m" _off = f"{ESC}[0m" ANSIEraseLine = '\033[2K\033[1G' EL = ANSIEraseLine # short alias # Color abbreviations (shortcuts for f-sting use) cK=_black; cR=_red; cG=_green; cY=_yellow; cB=_blue; cM=_magenta; cC=_cyan; cW=_white; cO=_off # background colors; use {cO} to turn off any color bK=_black_; bR=_red_; bG=_green_; bY=_yellow_; bB=_blue_; bM=_magenta_; bC=_cyan_; bW=_white_ -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a better way? [combining f-string, thousands separator, right align]
On Sun, 25 Aug 2024 15:12:20 GMT Gilmeh Serda via Python-list wrote: >Subject explains it, or ask. > >This is a bloody mess: > s = "123456789" # arrives as str f"{f'{int(s):,}': >20}" >' 123,456,789' > Oops.. forgot comma f"{int(s):>20,}" -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a better way? [combining f-string, thousands separator, right align]
On Sun, 25 Aug 2024 15:12:20 GMT Gilmeh Serda via Python-list wrote: >Subject explains it, or ask. > >This is a bloody mess: > s = "123456789" # arrives as str f"{f'{int(s):,}': >20}" >' 123,456,789' > f"{s:>20}" -- https://mail.python.org/mailman/listinfo/python-list