Hi !
I'm a new python programmer and I'm searching some tools for working
with tuples's list.
For example I have a list like that :
[('+',4,3),('+', 3,9),'('+',5,6) ,('x',10),('x', 3)]
and I would like to get a list with just the tuple with index 0 '+' or
things like that. The question is : is it possible without doing a
loop ?
Thx 2 1809
steph bagnis wrote: I'm a new python programmer and I'm searching some tools for working with tuples's list. For example I have a list like that : [('+',4,3),('+', 3,9),'('+',5,6) ,('x',10),('x', 3)] and I would like to get a list with just the tuple with index 0 '+' or things like that. The question is : is it possible without doing a loop ?
I guess it depends on what you consider a loop to be:
For example, a list comprehension should do the trick:
newlist = [x[0] for x in oldlist]
(This is equivalent to a for loop that goes through each item
in the old list, appending the 0-th element of each tuple to a new
list as it goes.)
-Peter
"Peter Hansen" <pe***@engcorp. com> wrote in message
news:TJ******** ************@po wergate.ca... steph bagnis wrote:
I'm a new python programmer and I'm searching some tools for working with tuples's list. For example I have a list like that : [('+',4,3),('+', 3,9),'('+',5,6) ,('x',10),('x', 3)] and I would like to get a list with just the tuple with index 0 '+' or things like that. The question is : is it possible without doing a loop ?
I guess it depends on what you consider a loop to be:
For example, a list comprehension should do the trick:
newlist = [x[0] for x in oldlist]
(This is equivalent to a for loop that goes through each item in the old list, appending the 0-th element of each tuple to a new list as it goes.)
If I understood the OP correctly, he wants to select those tuples that have
a '+' as the first item. So maybe he wants:
newlist = [x for x in oldlist if x[0] == '+']
--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Fuzzyman |
last post by:
Pythons internal 'pointers' system is certainly causing me a few
headaches..... When I want to copy the contents of a variable I find
it impossible to know whether I've copied the contents *or* just
created a new pointer to the original value....
For example I wanted to initialize a list of empty lists....
a=, , , , ]
I thought there has to be a *really* easy way of doing it - after a
|
by: Yomanium Yoth Taripoät II |
last post by:
HI,
1) what are the differences between list and tuple?
2) how to concatenate tuple and list? no method, no opérator?
3) im looking the fucking manual, and cant add value in my tuple, when it
already created :/
how to do it?
thx.
|
by: GrelEns |
last post by:
hello,
i wonder if this possible to subclass a list or a tuple and add more
attributes ? also does someone have a link to how well define is own
iterable object ?
what i was expecting was something like :
>>> t = Test('anAttributeValue', )
>>> t.anAttribute
|
by: Ishwar Rattan |
last post by:
I am a little confused about a list and a tuple.
Both can have dissimilar data-type elements, can be returned
by functions. The only difference that I see is that list is
mutable and tuple is not (of course list have .append() etc.)
What is a possible scenario where one is preferred over the other?
-ishwar
|
by: flyaflya |
last post by:
a = "(1,2,3)"
I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',',
'2', ',', '3', ')') not (1,2,3)
| |
by: Xah Lee |
last post by:
suppose i'm going to have a data structure like this:
,
,
,
,
....
]
should i use (width,height) or ?
|
by: fdu.xiaojf |
last post by:
Hi all,
I can use list comprehension to create list quickly. So I expected that I
can created tuple quickly with the same syntax. But I found that the
same syntax will get a generator, not a tuple. Here is my example:
In : a = (i for i in range(10))
In : b =
|
by: beginner |
last post by:
Hi,
I am wondering how do I 'flatten' a list or a tuple? For example, I'd
like to transform or ] to .
Another question is how do I pass a tuple or list of all the
aurgements of a function to the function. For example, I have all the
arguments of a function in a tuple a=(1,2,3). Then I want to pass each
item in the tuple to a function f so that I make a function call
f(1,2,3). In perl it is a given, but in python, I haven't figured out
|
by: Davy |
last post by:
Hi all,
I am curious about whether there is function to fransform pure List to
pure Tuple and pure Tuple to pure List?
For example,
I have list L = ,]
something list2tuple() will have T=list2tuple(L)=((1,2,3),(4,5,6))
|
by: Hatem Nassrat |
last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote:
Well I have looked into this and it seems that using the list
comprehension is faster, which is reasonable since generators require
iteration and stop iteration and what not.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |