Sunday 15 August 2010

Python: Iterate through a list that refers to other lists -


This is not a list of lists, it does not know properly to ask.

I have a list of variables and each is a list, I am trying to get the variable from the list.

  gt; & Gt; Print sorted list ['Group 0', 'Group 1', 'Group 2', 'Group 3' '' Group 4 '' 'Group 5' '' Group 6 '' 'Group 7' '' Group 8 ' 'Group 9', 'Group 10', 'Group 11', 'Group 12']> gt; & Gt; & Gt; Print group 0 ['abc']> gt; & Gt; & Gt; For the group job: For the listed group  
  My loop fails to do me: For job in the group: Print Job Group 0 .....  

The number of group's lists vary, so I do not have any good way to get them through their list.

This sort is sorted by me:

< Pre> group_re = re.compile ('(group) (\ d +)', again I) totalVars = vars (). Keys () for myList = (S) if group_re.match (s) sorted list = sorted (myList, key = lambda x: (len (x), x))

By your example, you have the variable defined in the local namespace of the interpreter and you refer to names in other lists. Want to You can do that local () function, such as:

  for the sorted group: for the local people () [group]: print Job  

If these variables are defined at the module level, you can use globals () [group] instead.

From your updated information, variables are in the dependence_group namespace, but since you did the dependency_groups import * , they are also in your module's global And should be referred to as globals () [group] . You can also catch them with a separate function from other modules:

  Import dependency groups for the sorted group list: getattr (dependency_group, group) for jobs: print job Some give priority to this last method, because its more obvious where the groups came from. 


No comments:

Post a Comment