suppose i get list of tuple as
s = [(2,),(3,)]
now i want to change the value of tuple to
[2,3]
how i can do that
3 1567
suppose i get list of tuple as
s = [(2,),(3,)]
now i want to change the value of tuple to
[2,3]
how i can do that
Use a "list comprehension", like this:
>>> s = [(2,),(3,)]
>>> l = [t[0] for t in s]
>>> l
[2, 3]
>>>
Use a "list comprehension", like this:
>>> s = [(2,),(3,)]
>>> l = [t[0] for t in s]
>>> l
[2, 3]
>>>
If you deconstruct this to a simpler (as in easier to read) form, it would look like this: - tupList = [(2,),(3,)]
-
resultList = []
-
for tup in tupList:
-
resultList.append(tup[0])
-
print resultList
Once you get used to them, list comprehensions are a great tool.
list comprehension is the way to go. just for fun, this is an "unorthordox" way -
import re
-
>>> map(int,re.findall("(\d+)",str(s)))
-
[2, 3]
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Krista Bailie |
last post by:
I'm sure this is so easy that it will hurt for anyone to read it, but I
really need some direction. I'm trying to create a color chart (RGB) that
shows steps between two different colors as...
|
by: Will McGugan |
last post by:
Hi,
Why is that a tuple doesnt have the methods 'count' and 'index'? It
seems they could be present on a immutable object.
I realise its easy enough to convert the tuple to a list and do this,...
|
by: fedor |
last post by:
Hi all, happy new year,
I was trying to pickle a instance of a subclass of a tuple when I ran
into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should
I rewrite my class so I can...
|
by: Robert |
last post by:
I have a series of web applications (configured as separate applications) on
a server.
There is a main application at the root and then several virtual directories
that are independant...
|
by: metiu uitem |
last post by:
Say you have a flat list:
How do you efficiently get
, , ]
I was thinking of something along the lines of:
for (key,number) in list:
print key, number
|
by: Terry |
last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces.
One of the major uses is to be able to split out Read-only access to an
obect. Let me give you a simple (contrived)...
|
by: metaperl.etc |
last post by:
The following program does not work if you uncomment #lis =
+ list(args)
Evidently Python is opting for the nullary constructor list() as
opposed to the other one which takes a sequence....
|
by: pmcgover |
last post by:
I enjoyed Paul Barry's September article in Linux Journal entitled,
"Web Reporting with MySQL, CSS and Perl". It provides a simple,
elegant way to use HTML to display database content without any...
|
by: setori88 |
last post by:
Hi guys
this script i downloaded from here, http://win32com.goermezer.de/index.php?option=com_content&task=view&id=193&Itemid=274
I copied this script and setup and instance of open office...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
| |