473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Pythonically extract data from a list of tuples (getopt)

So the return value from getopt.getopt() is a list of tuples, e.g.
>>import getopt
opts = getopt.getopt('-a 1 -b 2 -a 3'.split(), 'a:b:')[0]; opts
[('-a', '1'), ('-b', '2'), ('-a', '3')]

what's the idiomatic way of using this result? I can think of several
possibilities.

For options not allowed to occur more than once, you can turn it into
a dictionary:
>>b = dict(opts)['-b']; b
'2'

Otherwise, you can use a list comprehension:
>>a = [arg for opt, arg in opts if opt == '-a']; a
['1', '3']

Maybe the usual idiom is to iterate through the list rather than using
it in situ...
>>a = []
b = None
for opt, arg in opts:
.... if opt == '-a':
.... a.append(arg)
.... elif opt == '-b':
.... b = arg
.... else:
.... raise ValueError("Unrecognized option %s" % opt)

Any of the foregoing constitute The One Way To Do It? Any simpler,
legible shortcuts?

Thanks.
Jun 27 '08 #1
1 2165

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Greg Brunet | last post by:
I'm writing some routines for handling dBASE files. I've got a table (DBF file) object & field object already defined, and after opening the file, I can get the field info like this: >>>...
11
by: Dan Stromberg | last post by:
We will soon have 3 copies, for testing purposes, of what should be about 4.5 terrabytes of data. Rather than cmp'ing twice, to verify data integrity, I was thinking we could speed up the...
0
by: could ildg | last post by:
I want to retrieve all urls in a string. When I use re.fiandall, I get a list of tuples. My code is like below: url=unicode(r"((http|ftp)://)?((((+\.)+){3}+(/+)?)|(\w*((\.\w+)+){2,})(*)*)")...
8
by: Amit Khemka | last post by:
Hello All, say you have some string: ", foobar " Now i want to extract all substrings for which "isinstance(eval(substr), list)" is "True" . now one way is to walk through the whole sample...
17
by: News | last post by:
Hi everyone, My goal is to pull command switches/options from a file and then assign the values to select variables which would eventually be included in a class object. The data file looks...
11
by: Noah | last post by:
I have a list of tuples I want to reverse the order of the elements inside the tuples. I know I could do this long-form: q = y = for i in y: t=list(t)
10
by: rshepard | last post by:
While working with lists of tuples is probably very common, none of my five Python books or a Google search tell me how to refer to specific items in each tuple. I find references to sorting a list...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.