I'm really really newbie, almost no knowledge about python.
The problem I have is :
example) -
a = [ 1, 2, 3, 4, 5, 6, 7 ]
-
for n in a:
-
if a[n] < 4:
-
del a[n]
if I code like this, an error occurs, because the number of elements is changed.
so, what should I do to do this withour any error.
Is there any way to access list datas simultaneously?
6 1052
Rather than deleting items that fail the test, try copying the good'uns to a new list.
I'm really really newbie, almost no knowledge about python.
The problem I have is :
example) -
a = [ 1, 2, 3, 4, 5, 6, 7 ]
-
for n in a:
-
if a[n] < 4:
-
del a[n]
if I code like this, an error occurs, because the number of elements is changed.
so, what should I do to do this withour any error.
Is there any way to access list datas simultaneously?
I don't get what you are trying to do. You have a list of indexes (??) that you use to acces that same list? Is this what you want: -
a = range(1, 8) # generates a list like yours
-
new = []
-
for n in a:
-
if n < 4:
-
new.append(n)
-
Or you could use filter(): -
a = range(1, 8)
-
new = filter(lambda x: x < 4, a)
-
Or maybe a list comprehension is clearer: -
a = range(1, 8)
-
new = [n for n in a if n < 4]
-
I'm really really newbie, almost no knowledge about python.
The problem I have is :
example) -
a = [ 1, 2, 3, 4, 5, 6, 7 ]
-
for n in a:
-
if a[n] < 4:
-
del a[n]
if I code like this, an error occurs, because the number of elements is changed.
so, what should I do to do this withour any error.
Is there any way to access list datas simultaneously?
Our new friend, Strider1066, has a good suggestion.
For a quick copy of a simple list, use a "slice": -
>>> for i, item in enumerate(a[:]): # A "slice" from beginning to end
-
... if item < 4:
-
... del a[i]
-
...
-
>>> a
-
[2, 4, 6, 7]
-
>>>
Our new friend, Strider1066, has a good suggestion.
For a quick copy of a simple list, use a "slice": -
>>> for i, item in enumerate(a[:]): # A "slice" from beginning to end
-
... if item < 4:
-
... del a[i]
-
...
-
>>> a
-
[2, 4, 6, 7]
-
>>>
Are you sure that works? I mean, 2 is not greater than 4. It works with a.remove() but I get the same results like yours when I use del. I never actually use del. What does it do?
Are you sure that works? I mean, 2 is not greater than 4. It works with a.remove() but I get the same results like yours when I use del. I never actually use del. What does it do?
OOoooops! Can't rely on the index into a changing list.
Remove works because it doesn't rely on the index of the item. -
>>> a = [ 1, 2, 3, 4, 5, 6, 7 ]
-
>>> for item in a[:]: # A "slice" from beginning to end
-
... if item < 4:
-
... a.remove(item)
-
...
-
>>> a
-
[4, 5, 6, 7]
-
>>> a = [ 1, 2, 3, 4, 5, 6, 7 ]
-
>>> for item in a[:]: # A "slice" from beginning to end
-
... if item < 4 or item == 7:
-
... a.remove(item)
-
...
-
>>> a
-
[4, 5, 6]
-
>>>
Nice catch, my friend. Thank you.
Thank you everyone, it was wiser to copy and replace lists.
I'm going to like here. :) thx
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Raymond Hettinger |
last post by:
Based on your extensive feedback, PEP 322 has been completely revised.
The response was strongly positive, but almost everyone preferred
having a function instead of multiple object methods. The...
|
by: wageslave |
last post by:
Hi folks,
I have a question about using consequtive combo boxes on a form.
I am designing a basic library database for a small community organisation which deals with inner city problems and...
|
by: karokat |
last post by:
Hi, I want to learn how to program and python seems to be the most intuitive language according to various internet sources - but I'm not sure if it's best for newbies... please advise..
Anyway,...
|
by: donchoi |
last post by:
Hi, newbie here, sorry. I have a couple of basic Python questions.
1) How do I open 'datafile' and skip to the ith line for reading? I have a loop counter i for this purpose. The data file is very...
|
by: user923005 |
last post by:
It would be really nice if C could adopt a really nice algorithms
library like C++'s STL + BOOST.
The recent "reverse the words in this sentence" problem posted made me
think about it.
It's...
|
by: Man4ish |
last post by:
I have created Graph object without vertex and edge property.It is working fine.
#include <boost/config.hpp>
#include <iostream>
#include <vector>
#include <string>
#include...
|
by: clouddragon |
last post by:
Hi,
i am in desperate need for any help regarding one of my assignments. I am to write a python program that lists the numbers that are composite from 1 to n(input) and write it to an external...
|
by: idiolect |
last post by:
Hi all - Sorry to plague you with another newbie question from a
lurker. Hopefully, this will be simple.
I have a list full of RGB pixel values read from an image. I want to
test each RGB band...
|
by: jch |
last post by:
Sorry for the newbie question but I'm trying to learn Visual Studio. I've
got VS Express 2008 and I'm using visual basic. I'm trying to learn how to
deploy a program so I've written a very basic...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |