Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83242 > unrolled thread
| Started by | Dariusz Mysior <mysior.darek@gmail.com> |
|---|---|
| First post | 2015-01-05 06:27 -0800 |
| Last post | 2015-01-06 12:12 +1100 |
| Articles | 8 — 6 participants |
Back to article view | Back to comp.lang.python
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
| From | Dariusz Mysior <mysior.darek@gmail.com> |
|---|---|
| Date | 2015-01-05 06:27 -0800 |
| Subject | problem with for and if |
| Message-ID | <a71e126d-4b27-42a3-8103-3381b62794d2@googlegroups.com> |
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")
[toc] | [next] | [standalone]
| From | Shiyao Ma <i@introo.me> |
|---|---|
| Date | 2015-01-05 22:38 +0800 |
| Message-ID | <mailman.17396.1420468696.18130.python-list@python.org> |
| In reply to | #83242 |
On Jan 05 at 06:27 -0800, 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
def gen():
count = 0
for a in zmienna:
# we assume szukana is a single letter
if szukana == a:
count+=1
else:
continue
return count
More preferably, you should repetitively use "str.find"
Or just use `max(0,len(zmienna.split(szukana))-1)`
--
Shiyao Ma
http://introo.me
[toc] | [prev] | [next] | [standalone]
| From | YBM <ybmess@nooos.fr.invalid> |
|---|---|
| Date | 2015-01-05 15:39 +0100 |
| Message-ID | <54aaa23e$0$2112$426a74cc@news.free.fr> |
| In reply to | #83242 |
Le 05/01/2015 15:27, Dariusz Mysior a écrit :
> 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")
In the for loop a is iterating through "traktorzysta" ("t", then "r",
then "a", ...): 12 steps
moreover the expression szukana in zmienna is always true (as t is
a letter of "traktorzysta"), so count += 1 is executed twelve times.
def gen():
count=int(0)
for a in zmienna:
if a == szukana:
count+=1
else:
continue
return count
will work (even if quite ugly, why int(0)? why defining a function
without parameters? etc.)
[toc] | [prev] | [next] | [standalone]
| From | Chris Warrick <kwpolska@gmail.com> |
|---|---|
| Date | 2015-01-05 15:41 +0100 |
| Message-ID | <mailman.17397.1420468894.18130.python-list@python.org> |
| In reply to | #83242 |
On Mon, Jan 5, 2015 at 3:27 PM, Dariusz Mysior <mysior.darek@gmail.com> wrote:
> I want search count of szukana in zmienna but code below counting all 12 letters from "traktorzysta" word
First off, I recommend that you do not write code in Polish — use
English so that everyone can understand your code.
Your error is here:
> for a in zmienna:
> if szukana in zmienna:
You shouldn’t be looking in `zmienna`, you should be checking `a` —
the letter of the word used in the current iteration. Moreover, you
should use == for the comparison — compare the letter in the current
iteration to 't'. The correct line WOULD be
if a == szukana:
BUT there is actually a better solution. You see, Python is a
language with “batteries included” — that is, many things are already
available for you to use without need to re-implement them. Just use
str.count, which will do it for you:
zmienna = 'traktorzysta'
szukana = 't'
print("Literka '",szukana,"' w słowie ",zmienna,
"wystąpiła ",zmienna.count(szukana)," razy")
--
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16
[toc] | [prev] | [next] | [standalone]
| From | Dariusz Mysior <mysior.darek@gmail.com> |
|---|---|
| Date | 2015-01-05 06:47 -0800 |
| Message-ID | <e647b330-be3e-4d8b-9e6e-32d17b1c3cf9@googlegroups.com> |
| In reply to | #83242 |
W dniu poniedziałek, 5 stycznia 2015 15:28:07 UTC+1 użytkownik Dariusz Mysior napisał:
> 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")
Now I understand it!
Thank You!!!
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-01-05 14:48 +0000 |
| Message-ID | <mailman.17398.1420469525.18130.python-list@python.org> |
| In reply to | #83242 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Shiyao Ma <i@introo.me> |
|---|---|
| Date | 2015-01-05 22:57 +0800 |
| Message-ID | <mailman.17399.1420469867.18130.python-list@python.org> |
| In reply to | #83242 |
On Jan 05 at 22:38 +0800, Shiyao Ma wrote: > More preferably, you should repetitively use "str.find" > > Or just use `max(0,len(zmienna.split(szukana))-1)` Forgot there was a `str.count`, ;). -- Shiyao Ma http://introo.me
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2015-01-06 12:12 +1100 |
| Message-ID | <54ab3675$0$12986$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #83242 |
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 I think what you want is: zmienna.count(szukana) -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web