LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-17-2008, 09:19 PM   #1
donnied
Member
 
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Rep: Reputation: 30
how to do * wild cards in python


If I import a bunch of lists from 'data' say:
stuff1=[("a", "b", "c'), ("a", "b", "c')]
...
stuff200=[("a", "b", "c'), ("a", "b", "c')]

how can I:

print data.stuff*[0] ?
 
Old 08-18-2008, 12:42 AM   #2
ophirg
Member
 
Registered: Jan 2008
Location: Israel
Distribution: Kubuntu 12.10
Posts: 127

Rep: Reputation: 19
if the stuff variables are global, try

Code:
for (k,v) in locals().items():
  if k.startswith('stuff'): print v[0]
but you should really use a list or a hash map instead of separate variables.
something like:
Code:
stuff = {}
stuff[1] = [("a", "b", "c"), ("a", "b", "c")]
...
stuff[100] = [("a", "b", "c"), ("a", "b", "c")]

Last edited by ophirg; 08-18-2008 at 12:43 AM.
 
Old 08-18-2008, 05:49 PM   #3
donnied
Member
 
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Original Poster
Rep: Reputation: 30
Thank you.
This sounds good, how would I check field [0][2] off all the stuff[1]- stuff[100]?
 
Old 08-19-2008, 02:41 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Code:
#!/usr/bin/env python

stuff = {}
stuff[0] = [("a", "b", "c"), ("x", "y", "z")]
stuff[1] = [("d", "e", "f"), ("x", "y", "z")]
stuff[2] = [("g", "h", "i"), ("x", "y", "z")]
stuff[3] = [("j", "k", "l"), ("x", "y", "z")]
# ...
# stuff[100] = [("a", "b", "c"), ("x", "y", "z")]

for i in range(4):
    print stuff[i][0][2]
Still not quite nice python code. Better rewrite the thing to use list instead of dict, but may be possible in your case. I depends where the data comes from.

Code:
#!/usr/bin/env python

stuff =[]
stuff.append([("a", "b", "c"), ("x", "y", "z")])
stuff.append([("d", "e", "f"), ("x", "y", "z")])
stuff.append([("g", "h", "i"), ("x", "y", "z")])
stuff.append([("j", "k", "l"), ("x", "y", "z")])
# ..

for item in stuff:
    print item[0][2]
 
Old 08-19-2008, 02:42 AM   #5
Skurmedel
LQ Newbie
 
Registered: Aug 2008
Distribution: OpenSuSE/Debian
Posts: 5

Rep: Reputation: 0
Not sure exactly what you mean. Do you want to compare for example somelist[0][2] with all the other values present in the list?
 
Old 08-19-2008, 03:49 PM   #6
ophirg
Member
 
Registered: Jan 2008
Location: Israel
Distribution: Kubuntu 12.10
Posts: 127

Rep: Reputation: 19
Quote:
how would I check field [0][2] off all the stuff[1]- stuff[100]?
if you use a dictionary like in the code i posted and store a tuple in each item like you indicated, use:
Code:
stuff[0][2]
to access the 3rd value in the stuff[0].
remember - the index of the first value in a tuple/array is zero.
read more in http://docs.python.org/tut/tut.html and http://docs.python.org/tut/node7.html
 
Old 08-19-2008, 09:26 PM   #7
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Not sure what you mean by "check" all the entries at [0][2], but if you want a list of all the entries at [0][2] for checking, then you could use list comprehension:

Code:
# for a dict
checkList = [v[0][2] for k,v in stuff.iteritems()]
# for an array
checkList = [v[0][2] for v in stuff]

Last edited by 95se; 08-19-2008 at 09:27 PM.
 
  


Reply

Tags
list, python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Swaret gone wild! jp_ Slackware 3 01-02-2006 06:36 AM
ndiswrapper gone wild! konowdude Linux - Wireless Networking 2 12-28-2005 01:27 PM
Wild Cards using the find command -- grrr hpladd Linux - Newbie 9 07-14-2005 05:18 PM
More Players Supporting Wild-Cards? chime Linux - General 0 12-01-2004 10:21 AM
mldonkey gone wild crashmeister Linux - Software 0 11-15-2002 09:49 AM


All times are GMT -5. The time now is 08:14 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration