Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
which is a better test?
#1
which is a better test where the variable named word is always a string with a length greater than zero?

case 1
    if word[0] in ('w','x','y','z'):
        ...
or
case 2
    if word[0] in 'wxyz':
        ...
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
In case 1 you have a list of 4 strings with 1 character each.
In case 2 you have 1 string that is 4 characters long.
what are you testing for?
Reply
#3
Case 2 is what i would have used or startswith() with a tuple which can make it more readable.
(Feb-04-2018, 03:44 AM)mcmxl22 Wrote: what are you testing for?
Both test for the same,here some code to make it clearer.
>>> word = 'war'
>>> if word[0] in ('w','x','y','z'):
...     print(f'word <{word}> has a first letter in sequence <wxyz>') 
...     
word <war> has a first letter in sequence <wxyz>

>>> if word[0] in 'wxyz':
...     print(f'word <{word}> has a first letter in sequence <wxyz>') 
...     
word <war> has a first letter in sequence <wxyz>

>>> if word.startswith(('w','x','y','z')):
...     print(f'word <{word}> has a first letter in sequence <wxyz>') 
...     
word <war> has a first letter in sequence <wxyz>
Reply
#4
(Feb-04-2018, 03:44 AM)mcmxl22 Wrote: In case 1 you have a list of 4 strings with 1 character each.
In case 2 you have 1 string that is 4 characters long.
what are you testing for?
to see if the first character in the string referenced by the variable named word is 'w' or 'x' or 'y' or 'z'.

i didn't know of .startswith() or even know to look for it. is it faster?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
But if you have string assigned to variable what is the reason to split it to list? As you noticed strings are iterable so they behave like lists. Second case is proper one, but should be used with startswith() method as it was mentioned.
Reply
#6
it seems every time i think there should be an included (not third party) method, module, function, or class (resource) for something i want to do or check on the possibility of using, and look for it, i don't find it. and, it seems every time a resource would not be there (or, more accurately, don't think it would be there), it turns out there is one there, or at least in a third party resource, and one or more of the members here knows about it. my curiosity, at the moment, is: how many such resources are there?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
for start 128743 packages listed on PyPi as of this moment + many many more, not [yet] listed on PyPI
on a more serious tone - we don't know how many existing resources you don't know about :-)
Reply
#8
well i know about twisted, but i don't know everything it can do as that seems to be a lot.

it would be nice to have a grep-able text file listing all 128743 PyPi packages, one per line, with details on the same line describing what all it can do (if quite a lot, it's ok to list a package on more than one line). the name of the package followed by a space should begin each line.

is it the case that any package that anyone does not know about is a package that none of you know about or is there some way you know how to find out if something you need to do has a package for it?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
(Feb-07-2018, 02:35 AM)Skaperen Wrote: is there some way you know how to find out if something you need to do has a package for it
You cannot be serious asking how to find information on Internet in 21 century...
Reply
#10
i did look for a web page that describes the PyPi packages but found none. all i can find is one that only describes today's package updates. their index has been hard to work with and for most of my efforts, a big failure (right now it is showing me nothing, not even any categories). and when it is working, there usually isn't even a category i want. if they have a database of the data on that first page, for all packages, and can provide a dump in JSON or CSV, i could design (in Python, of course) a program to make it into a nice page (in HTML, of course). but i don't see a place to get that.

i'd think a 21st century internet could do better than this.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,140 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,128 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,564 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn

Forum Jump:

User Panel Messages

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