May-10-2018, 05:44 PM
When creating an explicit list, list = [ 'a', 'b', 'c', ... 'z' ], is there a way to "self quote" like @list = qw( a b c ... z) in Perl?
Syntactic sugar for explicit lists?
|
May-10-2018, 05:44 PM
When creating an explicit list, list = [ 'a', 'b', 'c', ... 'z' ], is there a way to "self quote" like @list = qw( a b c ... z) in Perl?
May-10-2018, 06:09 PM
since strings always has to be inside of quotes i don't think that this is possible in python. Though, what comes close to that what you want to do is when you have a string some_string = "a b c d e f g h i j k l m n o p q r s t u v w x y z" and you then use the split operator
some_string = "a b c ... z" list = some_string.strip()then list == ["a", "b", ..., "z"] But maybe someone else nows a different way :)
May-10-2018, 06:19 PM
(This post was last modified: May-10-2018, 06:20 PM by Antipaladin.)
Thanks! I think I should probably stop trying to make Python act like Perl and embrace the new paradigm :)
(May-10-2018, 06:09 PM)ThiefOfTime Wrote: since strings always has to be inside of quotes i don't think that this is possible in python. Though, what comes close to that what you want to do is when you have a string some_string = "a b c d e f g h i j k l m n o p q r s t u v w x y z" and you then use the split operator Please, never - even in examples - shadow builtin "list" with a custom variable (May-10-2018, 05:44 PM)Antipaladin Wrote: When creating an explicit list, list = [ 'a', 'b', 'c', ... 'z' ], is there a way to "self quote" like @list = qw( a b c ... z) in Perl? There are str and repr functions in python - for a "flat" list of scalars, their output is similar
Test everything in a Python shell (iPython, Azure Notebook, etc.)
May-10-2018, 08:03 PM
You have it.
In [1]: from string import ascii_lowercase In [2]: print(ascii_lowercase) abcdefghijklmnopqrstuvwxyz In [3]: letters = list(ascii_lowercase) In [4]: print(letters) ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
May-11-2018, 06:07 AM
(May-10-2018, 07:10 PM)volcano63 Wrote:I normally would not do it. I just made it that way to fit it to his question. I also would never name a variable "and", "is", "path", etc.. Though, thanks for your tip.(May-10-2018, 06:09 PM)ThiefOfTime Wrote: since strings always has to be inside of quotes i don't think that this is possible in python. Though, what comes close to that what you want to do is when you have a string some_string = "a b c d e f g h i j k l m n o p q r s t u v w x y z" and you then use the split operator
May-11-2018, 06:22 AM
(May-11-2018, 06:07 AM)ThiefOfTime Wrote: I normally would not do it. I just made it that way to fit it to his question. I also would never name a variable "and", "is", "path", etc.. Though, thanks for your tip. When I feel too lazy to invent names - usually in forums or in prototyping - I just write list_, dict_, etc.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Split dict of lists into smaller dicts of lists. | pcs3rd | 3 | 3,328 |
Sep-19-2020, 09:12 AM Last Post: ibreeden |
|
Being explicit about the items inside a tuple argument | rudihammad | 3 | 3,246 |
Dec-04-2019, 08:10 AM Last Post: perfringo |
|
sort lists of lists with multiple criteria: similar values need to be treated equal | stillsen | 2 | 5,099 |
Mar-20-2019, 08:01 PM Last Post: stillsen |
|
convert non-string with explicit base | jacklee26 | 5 | 20,812 |
Nov-06-2018, 06:50 AM Last Post: jacklee26 |
|
Newb: Simple Explicit Formula | Duplicitous | 1 | 3,842 |
May-05-2017, 07:03 PM Last Post: buran |