[issue41899] Poor example for Element.remove()

2020-10-01 Thread Stefan Behnel


Stefan Behnel  added the comment:

Closing as duplicate of issue 41891. Let's keep the discussion there.

--
nosy: +scoder
resolution:  -> duplicate
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
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-10-01 Thread Stefan Behnel


Change by Stefan Behnel :


--
Removed message: https://bugs.python.org/msg377740

___
Python tracker 

___
___
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-10-01 Thread Stefan Behnel


Stefan Behnel  added the comment:

Closing as duplicate of issue 41892. Let's keep the discussion there.

--

___
Python tracker 

___
___
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 

___
___
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 Eric V. Smith


Eric V. Smith  added the comment:

As you've seen, the example is correct. I made the same mistake earlier today.

For others: see also #41891 for a suggestion to improve the documentation.

As was pointed out in that issue, it's generally true in Python that you should 
not mutate a sequence while iterating over it.

--
nosy: +eric.smith

___
Python tracker 

___
___
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 

___
___
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 

___
___
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 

___
___
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 

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