[issue41892] use both "for in" and "ElementTree.remove" has a index bug

2020-10-01 Thread WoodyWoo


WoodyWoo  added the comment:

Could I say the mutable sequence containing not the object but the pointer like 
C++.
So they can changed in def functions.

--

___
Python tracker 
<https://bugs.python.org/issue41892>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41899] Poor example for Element.remove()

2020-09-30 Thread WoodyWoo


WoodyWoo  added the comment:

Could I say the mutable sequence containing not the object but the pointer like 
C++.
So they can changed in def functions.

--

___
Python tracker 
<https://bugs.python.org/issue41899>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41892] use both "for in" and "ElementTree.remove" has a index bug

2020-09-30 Thread WoodyWoo


WoodyWoo  added the comment:

@eric.smith @scoder @serhiy.storchaka Thank U all.
I get what to do,and still think the "for in" structure should rebuilding.
All three methods:

import xml.etree.ElementTree as ET 
xmlstr=\
r'''


2
2008
141100




5
2011
59900



69
2011
13600




69
2011
13600



'''
print(xmlstr)

#orginal code
root = ET.fromstring(xmlstr)
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
print("___orginal___")
for country in root.findall('country'):
print (country.get("name"))
print("^^^orginal\n")

#wrong code in my mind
root = ET.fromstring(xmlstr)
for country in root:
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
print("___bad___")
for country in root.findall('country'):
print (country.get("name"))
print("^^^bad\n")

#my code
root = ET.fromstring(xmlstr)
index=0
count=len(root.findall("./*"))
while index 50:
root.remove(root[index])
index=index+0
count=count-1  # avoid index err
continue
index=index+1
print("___new___")
for country in root.findall('country'):
print (country.get("name"))
print("^^^new\n")

--

___
Python tracker 
<https://bugs.python.org/issue41892>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41899] Poor example for Element.remove()

2020-09-30 Thread WoodyWoo


WoodyWoo  added the comment:

The docs should specially tell that when need root.remove(child) must works 
with "for child  in root.findall()".

And my code with "while if continue" could make root.insert(index,newchild) 
easily.

--

___
Python tracker 
<https://bugs.python.org/issue41899>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41899] Poor example for Element.remove()

2020-09-30 Thread WoodyWoo

WoodyWoo  added the comment:

My fault.
"for country in root.findall('country')“ is not working as same as "for country 
in root"
all 3 method below:

import xml.etree.ElementTree as ET 
xmlstr=\
r'''


2
2008
141100




5
2011
59900



69
2011
13600




69
2011
13600



'''
print(xmlstr)

#orginal code
root = ET.fromstring(xmlstr)
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
print("___orginal___")
for country in root.findall('country'):
print (country.get("name"))
print("^^^orginal\n")

#wrong code in my mind
root = ET.fromstring(xmlstr)
for country in root:
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
print("___bad___")
for country in root.findall('country'):
print (country.get("name"))
print("^^^bad\n")

#my code
root = ET.fromstring(xmlstr)
index=0
count=len(root.findall("./*"))
while index 50:
root.remove(root[index])
index=index+0
count=count-1  # avoid index err
continue
index=index+1
print("___new___")
for country in root.findall('country'):
print (country.get("name"))
print("^^^new\n")

--

___
Python tracker 
<https://bugs.python.org/issue41899>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41899] Poor example for Element.remove()

2020-09-30 Thread WoodyWoo


WoodyWoo  added the comment:

#new code to avoid an err
index=0
count=len(root.findall("./*"))
while index 50:
root.remove(root[index])
index=index+0
count=count-1  # avoid index err
continue
index=index+1

--

___
Python tracker 
<https://bugs.python.org/issue41899>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41899] Poor example for Element.remove()

2020-09-30 Thread WoodyWoo

New submission from WoodyWoo :

We can remove elements using Element.remove(). Let’s say we want to remove all 
countries with a rank higher than 50:

>>>
>>> for country in root.findall('country'):
... rank = int(country.find('rank').text)
... if rank > 50:
... root.remove(country)
...
>>> tree.write('output.xml')

When the original xml has over 2 country with  rank>50,and they are one by one 
neighborly siblings element,the upper code will delete the 1st 3rd 5th and more 
odd No. country.
A proper example should be:
index=0
while index < len(root.findall("./*")):
rank = int (root[index].find("rank").text)
if rank>50:
root.remove(root[index])
index=index+0
continue
index=index+1


I think "for each in list" should not work by index,but should work by pointer 
like thing,could be a list of pointers.
A finial solution should be like this --- when the "for each in list" was 
acting,the pointers list would be fixed,and you need not to worry about the 
"list" changing.

--
assignee: docs@python
components: Documentation
messages: 377726
nosy: WoodyWoo, docs@python
priority: normal
severity: normal
status: open
title: Poor example for Element.remove()
type: behavior
versions: Python 3.8

___
Python tracker 
<https://bugs.python.org/issue41899>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41892] use both "for in" and "ElementTree.remove" has a index bug

2020-09-30 Thread WoodyWoo


WoodyWoo  added the comment:

I think "for each in list" should not work by index,but should work by pointer 
like thing,could be a list of pointers.
When the "for each in list" was acting,the pointers list would be fixed,and you 
need not to worry about the "list" changing.

--

___
Python tracker 
<https://bugs.python.org/issue41892>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41892] use both "for in" and "ElementTree.remove" has a index bug

2020-09-30 Thread WoodyWoo


WoodyWoo  added the comment:

Only that makes "for each in list" literally.

--

___
Python tracker 
<https://bugs.python.org/issue41892>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41892] use both "for in" and "ElementTree.remove" has a index bug

2020-09-30 Thread WoodyWoo


WoodyWoo  added the comment:

I'm green hand in Coding.
But I think the problem caused by "for each in list".
Anything changed in "list" should not act at once,but should act when "for end" 
or break.

--

___
Python tracker 
<https://bugs.python.org/issue41892>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41892] use both "for in" and "ElementTree.remove" has a index bug

2020-09-30 Thread WoodyWoo


Change by WoodyWoo :


--
title: use both "for" and "ElementTree.remove" has a index bug -> use both "for 
in" and "ElementTree.remove" has a index bug

___
Python tracker 
<https://bugs.python.org/issue41892>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41892] use both "for" and "ElementTree.remove" has a index bug

2020-09-30 Thread WoodyWoo


New submission from WoodyWoo :

#Just run it in Python v3.8.6 of win7 32bit 
import xml.etree.ElementTree as ET 
xmlstr='''



'''
etroot = ET.fromstring(xmlstr)
for ELEMchild in etroot:
if ELEMchild.get("no") == "1" :
etroot.remove(ELEMchild)   #so far so good
print (ELEMchild.tag)
#It should be :  "b /n c" or "a /n b /n c",I can live with it both.
#But it is :  "a /n c".
#The index of ELEMchild should not +1 when there was a remove method worked 
on one of the before ELEMs.
#I was forced to use while and if to control the index +1/+0.
#BTW,ELEM has no method returning index in siblings, which is buging me too.

--
components: XML
messages: 377698
nosy: WoodyWoo
priority: normal
severity: normal
status: open
title: use both "for" and "ElementTree.remove" has a index bug
type: behavior
versions: Python 3.8

___
Python tracker 
<https://bugs.python.org/issue41892>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com