LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how to do * wild cards in python (http://www.linuxquestions.org/questions/programming-9/how-to-do-%2A-wild-cards-in-python-663480/)

donnied 08-17-2008 09:19 PM

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] ?

ophirg 08-18-2008 12:42 AM

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")]


donnied 08-18-2008 05:49 PM

Thank you.
This sounds good, how would I check field [0][2] off all the stuff[1]- stuff[100]?

Hko 08-19-2008 02:41 AM

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]


Skurmedel 08-19-2008 02:42 AM

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?

ophirg 08-19-2008 03:49 PM

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

95se 08-19-2008 09:26 PM

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]



All times are GMT -5. The time now is 06:03 AM.