I have two list of same length l1=[reboot,revision,disk_space] and l2=[no,12.15,4300] I want to make these two list as key-value map. Output should be like this [reboot:no,revision:12.18,disk_space:4300].. I tried Zip it is not working
1436
Imagine that you have the following list. keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam']  What is the simplest way to produce the following dictionary? a_dict = {'name': 'Monty', 'age': 42, 'food': 'spam'}
410
I needed to create a list of lists in Python, so I typed the following: my_list = [[1] * 4] * 3  The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]    Then I changed one of the innermost values: my_list[0][0] = 5  Now my list looks like this: [[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]]    which is not what I wanted or expected. Can someone please explain what's going on, and how to get around it?
97052
The typical ConfigParser generated file looks like:  [Section] bar=foo [Section 2] bar2= baz   Now, is there a way to index lists like, for instance:  [Section 3] barList={     item1,     item2 }   Related question:
2867
I have 2 large files (3000 columns, 15000 rows) of the following format  file1 (tab-separated):  1/0 0/0 0/0 0/0 1/1 0/0 1/1 0/1 0/0   file2 (tab-separated):  3 5 2 1 7 10 3 4 3   I'd like to combine the values from the first column of each file with a ":" separator, then move on to the second, third, etc. columns.  Desired output (tab-separated):  1/0:3 0/0:5 0/0:2 0/0:1 1/1:7 0/0:10 1/1:3 0/1:4 0/0:3   Efficiency isn't critical, so any language is fine. I apologize if this has been asked before.
35090
I know how to map a single key to a command using the gconf settings. I also know how to map a single key to another key (F1 -> ctrl) using Xmodmap. My question is how can I map a single key such as F1 to a multi-key operation such at ctrl+c. I am basically trying to set up a single key to simulate copy. I can think of two ways to possibly do this:   Map single key to multiple keys so that when the single key is pressed it is essentially pressing ctrl+c. Use gconf settings to execute the copy “command” upon the single key press. I am not sure if there is a “command” to copy text.
34869
How can I iterate, loop, process, or otherwise do something, with each element from a comma separated list of things?  This question       , and the are already lots of very good answers everywhere. However I think it would also be valuable to have a single reference point where all such alternative techniques are explained in detail, with their advantages and disadvantages discussed.  All of plain-TeX, LaTeX, LaTeX3, LuaTeX, Context, etoolbox, pgffor, etc. answers are invited. Please consider explaining, for example:   How does the command/method deal with whitespace around items? How does the command/method deal with empty/missing items? (e.g. a,b,,c) How does the command/method deal with a trailing comma? Can the command/method work of a list stored on a macro? Is each iteration evaluated globally or on a local group? (i.e., if a command is defined during an iteration, does the command survive the loop?) Do items get expanded or somehow mangled before being processed? What about lists where items are key=value or some other kind of pairs? Does the method work for lists separated with something other than a comma? Any other thing I should know before deciding to use this method?
94174
When I am writing code in Python, I often need to remove items from a list or other sequence type based on some criteria. I haven't found a solution that is elegant and efficient, as removing items from a list you are currently iterating through is bad.  For example, you can't do this:  for name in names:     if name[-5:] == 'Smith':         names.remove(name)   I usually end up doing something like this:  toremove = [] for name in names:     if name[-5:] == 'Smith':         toremove.append(name) for name in toremove:     names.remove(name) del toremove   This is innefficient, fairly ugly and possibly buggy (how does it handle multiple 'John Smith' entries?). Does anyone have a more elegant solution, or at least a more efficient one?   How about one that works with dictionaries?
37453
I have a list of tuples like this:  [ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ]   I want to iterate through this keying by the first item, so, for example, I could print something like this:  a 1 2 3 b 1 2 c 1   How would I go about doing this without keeping an item to track whether the first item is the same as I loop around the tuples? This feels rather messy (plus I have to sort the list to start with)...
99253
I like how Java has a Map where you can define the types of each entry in the map, for example &lt;String, Integer&gt;.   What I'm looking for is a type of collection where each element in the collection is a pair of values.  Each value in the pair can have its own type (like the String and Integer example above), which is defined at declaration time.    The collection will maintain its given order and will not treat one of the values as a unique key (as in a map).    Essentially I want to be able to define an ARRAY of type &lt;String,Integer&gt; or any other 2 types.    I realize that I can make a class with nothing but the 2 variables in it, but that seems overly verbose.    I also realize that I could use a 2D array, but because of the different types I need to use, I'd have to make them arrays of OBJECT, and then I'd have to cast all the time.    I only need to store pairs in the collection, so I only need two values per entry.  Does something like this exist without going the class route?  Thanks!
4615
Given a dictionary like so:  my_map = {'a': 1, 'b': 2}   How can one invert this map to get:  inv_map = {1: 'a', 2: 'b'}
99004
This project I'm dealing with will read the size of free memory segments and size of processes from a text file and then will try to allocate a memory partition for each process using the first-fit, best-fit and worst-fit allocation algorithms. Below is the first part of the code I wrote. import sys  filename = sys.argv[1]  with open(filename, &quot;r&quot;) as file:         free_memory = file.readline().strip().split(',')         process_size = list(map(int, file.readline().strip().split(',')))         file = open(&quot;output.txt&quot;,&quot;w&quot;)    #---------------------------# #First-Fit Memory Allocation# #---------------------------#  file.write(f&quot;First-Fit Memory Allocation\n{'-'*60}\n&quot;)  file.write(f&quot;start =&gt; {' '.join(free_memory)}\n&quot;)  memory = list(map(int, free_memory))  memory_str = list(free_memory)  for process in process_size:         data = f&quot;{process} =&gt; &quot;         allocated = False         for index in range(len(memory)):                 if not allocated and memory[index] &gt;= process:                         memory[index] -= process                         memory_str[index] = memory_str[index][:-1*len(str(process))]                         memory_str[index] += f&quot;{process}*&quot;                           if(memory[index] != 0):                                 memory_str[index] += f&quot; {memory[index]}&quot;                         allocated = True                                  data += f&quot;{memory_str[index]} &quot;           if(not allocated):                 data = f&quot;{process} =&gt; not allocated, must wait&quot;                        file.write(data+'\n')
53456
What is the difference between:  some_list1 = [] some_list1.append("something")   and  some_list2 = [] some_list2 += ["something"]
18727
This question is somewhat of a follow up to an answer to a previous question ().  I would like to create a list environment (similar to itemize and enumerate) such that the output of the list environment is a comma-separated list.  For example, the following input:  \begin{commalist}     \item Item A     \item Item B     \item Item C \end{commalist}   Should produce the following output:  Item A, Item B and Item C.   Additionally, it would be nice to be able to specify the use of a comma instead of the final and (so that the output is instead Item A, Item B, Item C..
43386
Is there a more concise way of doing this in Python?:  def toDict(keys, values):   d = dict()   for k,v in zip(keys, values):     d[k] = v    return d
110341
I have a dict where each key references an int value. What's the best way to sort the keys into a list depending on the values?
2168
I need to convert one into 1, two into 2 and so on.  Is there a way to do this with a library or a class or anything?
32072
I am searching for a nice method of merging two or more files line by line using the zipper method (also called late merge). Assuming we have three files, the result should look like this:  line1 file1 line1 file2 line1 file3 line2 file1 line2 file2 line2 file3 ...   EDIT  I wrote a little python script capable of doing this:  #!/usr/bin/python  import sys, itertools  fileList = [] for file in sys.argv[1:]:     f = open(file, "r")     fileList.append(f.read().split("\n"))  for z in itertools.izip_longest(*fileList):     print "\n".join([i for i in z if i is not None])   I still wonder if there is any standard tool or a clever combination of them doing the same thing.