Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83247

Re: problem with for and if

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'counting': 0.09; 'method,': 0.09; 'notes:': 0.09; 'def': 0.12; 'be:': 0.16; 'count,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'adds': 0.24; 'subject:problem': 0.24; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'character': 0.29; 'characters': 0.30; 'statement': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'int,': 0.31; 'subject:with': 0.35; 'received:84': 0.35; 'but': 0.35; 'there': 0.35; 'should': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'letters': 0.60; 'skip:z 20': 0.60; 'total': 0.65; 'fact,': 0.69
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=ZZGTN6lA c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=xSRs65CQCjkA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=kOYNt9H-dgtSfJ2RzLMA:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett:2500
Date Mon, 05 Jan 2015 14:48:53 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
MIME-Version 1.0
To python-list@python.org
Subject Re: problem with for and if
References <a71e126d-4b27-42a3-8103-3381b62794d2@googlegroups.com>
In-Reply-To <a71e126d-4b27-42a3-8103-3381b62794d2@googlegroups.com>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17398.1420469525.18130.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1420469525 news.xs4all.nl 2889 [2001:888:2000:d::a6]:60492
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 3990
X-Received-Body-CRC 3837808183
Xref csiph.com comp.lang.python:83247

Show key headers only | View raw


On 2015-01-05 14:27, Dariusz Mysior wrote:
> I want search count of szukana in zmienna but code below counting all 12 letters from "traktorzysta" word
>
> szukana="t"
> zmienna="traktorzysta"
>
>
> def gen():
>      count=int(0)
>      for a in zmienna:
>          if szukana in zmienna:
>              count+=1
>          else:
>              continue
>      return count
>
>
> print("Literka '",szukana,"' w słowie ",zmienna,
>        "wystąpiła ",gen()," razy")
>
It's asking whether szukana is in zmienna for each character in zmienna.

There are 12 characters in zmienna, so it's asking 12 times.

Because szukana is in zmienna, it adds 1 to count each time, giving a
total of 12.

If szukana wasn't in zmienna, then nothing would be added to count,
giving a total of 0.

The body of the loop should be:

       for a in zmienna:
           if a == szukana:
               count += 1

Other notes:

0 is already an int, so int(0) is just 0!

There's no need for the 'else' part in the 'if' statement

Strings have a .count method, so you can, in fact, simplify it to just:

def gen():
     return zmienna.count(szukana)

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

problem with for and if Dariusz Mysior <mysior.darek@gmail.com> - 2015-01-05 06:27 -0800
  Re: problem with for and if Shiyao Ma <i@introo.me> - 2015-01-05 22:38 +0800
  Re: problem with for and if YBM <ybmess@nooos.fr.invalid> - 2015-01-05 15:39 +0100
  Re: problem with for and if Chris Warrick <kwpolska@gmail.com> - 2015-01-05 15:41 +0100
  Re: problem with for and if Dariusz Mysior <mysior.darek@gmail.com> - 2015-01-05 06:47 -0800
  Re: problem with for and if MRAB <python@mrabarnett.plus.com> - 2015-01-05 14:48 +0000
  Re: problem with for and if Shiyao Ma <i@introo.me> - 2015-01-05 22:57 +0800
  Re: problem with for and if Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-06 12:12 +1100

csiph-web