1.Three lists a, b and c. If a[index] is in b[index] then get the element in list c corresponding to list b[index]. That is if a[0]=b[1],get c[1]:
a = ['ASAP','WTHK']
b = ['ABCD','ASAP','EFGH','HIJK']
c = ['1','2','3','4','5']
I hope this is what you were looking for. You can add the b and the corresponding c value to the dictionary in a loop if the a array contains the b value. After that you can get the c value by a value as key like in the code below.
a = ['ASAP','WTHK']
# b c
dictionary_trans = {'ASAP' : '1'}
dictionary_trans = {'WTHK' : '1337'}
# etc. put all b values existing in a to the dict
# with thier coresponding c values.
key = a[0]
c_value = dictionary_trans.get(key)
print c_value
My python skills are very limited, but I think I would try to solve the problem this way.
This solution could crash if you use an a value which is not contained in the dictionary, so you need to implement some logic to handle missing relations between a and c, like insert dummy entries to the dictionary or so.
Related
I have a nested list as follows:
[[u'56', u'99', u'160'], [u'74', u'113', u'169'], [u'92', u'127', u'177'], [u'110', u'142', u'186'], [u'128', u'156', u'195'], [u'146', u'170', u'203'], [u'165', u'184', u'212'], [u'183', u'198', u'220'], [u'201', u'212', u'229'], [u'219', u'227', u'238'], [u'237', u'241', u'246']]
I want to pass each list as a sequence to the following map function:
map(None, nested_list1, nested_list2 ...]
So that, I get 3 lists, where, the first list contains the first element from all the nested_lists, the second list contains the second element from all the nested_lists.
So far, after many tries, I have come up with the following code (it does not involve map()).
first_list = [each[0] for each in nested_list]
second_list = [each[1] for each in nested_list]
third_list = [each[1] for each in nested_list]
I think this can be made into a one-liner if map() is used. Any ideas?
What you want is zip but you can do it with map, you just have to use *list_of_lists to transpose:
a, b, c = map(None,*l)
If you want list you would have to also map to list:
map(None,*l)
Really you should use zip:
a,b,c = map(list,zip(*l))
Both will give the same output in python 2:
In [19]: a,b,c = map(list,map(None,*l))
In [20]: print a
[u'56', u'74', u'92', u'110', u'128', u'146', u'165', u'183', u'201', u'219', u'237']
In [21]: print b
[u'99', u'113', u'127', u'142', u'156', u'170', u'184', u'198', u'212', u'227', u'241']
In [22]: print c
[u'160', u'169', u'177', u'186', u'195', u'203', u'212', u'220', u'229', u'238', u'246']
In [23]: a,b,c = map(list, zip(*l))
In [24]: print a
[u'56', u'74', u'92', u'110', u'128', u'146', u'165', u'183', u'201', u'219', u'237']
In [25]: print b
[u'99', u'113', u'127', u'142', u'156', u'170', u'184', u'198', u'212', u'227', u'241']
In [26]: print c
[u'160', u'169', u'177', u'186', u'195', u'203', u'212', u'220', u'229', u'238', u'246']
But the map approach won't work using python3.
Consider the following lua table:
foo = {
bar1 = {a = 1, b = 2, c = "hello"},
bar2 = {a = 5, b = 2, c = "bbq"},
bar3 = {a = 76, b = 13, c = "pwnd"}
}
I am trying to iterate this table using the lua C API to retrieve the key names, bar1, bar2 and bar3. I used the lua_next(L, -2) function to iterate as suggested by many, but the problem is that it returns the elements in random order. The order changes on each run.
I use the following code:
for( lua_pushnil(L); lua_next(L, -2) != 0; lua_pop(L, 1) )
{
printf("%s\n", lua_tostring(L, -2));
}
Most of the time, the output is unordered, such as bar2 bar1 bar3. When lucky, it's ordered. Is there an easy way to loop the table keys in an ordered fashion? What would be the equivalent code as I use, but ordered? Thanks!
Edit:
I know I am using a map rather than an array. But in lua script we have ipairs which would work just fine for this case. I'm looking at the C API equivalent. I've found this stackoverflow answer which gives a different answer, but it doesn't work for me, so I wonder if that answer is good or relevant.
No. The order that Lua traverses tables is undefined.
If you know that the keys are of the form barXXX, then you can create these keys as strings dynamically in the order you wish and then get the corresponding value from the table.
Put the names in a C array, and then order them in C.
As others are saying, the order of keys returned by the hash portion of a table is not guaranteed.
Alternatively, use an array-like structure so you can use the array part:
foo = {
{name = 'bar1', a = 1, b = 2, c = "hello"},
{name = 'bar2', a = 5, b = 2, c = "bbq"},
{name = 'bar3', a = 76, b = 13, c = "pwnd"}
}
So you can use ipairs or the equivalent to for i=1,#foo to iterate over the items in order.
Checking Lua's documentation, the main structure that Lua supports is a Hash Table. Given that it's always going to be a hash-table, you'd probably want to reimplement foo as an array. The bar1, bar2, etc can be part of the entry into the array like so:
foo = {}
foo[0] = {name='bar1', ...}
foo[1] = {name='bar2', ...}
...
or as #lhf suggested, just build the bar names inside a for-loop (if you know it's in sequence) and retrieve the values from foo table.
I have 2 immutable case classes A(source, key, value) and B(source, key, value)
I want to add A over B in such a way when 'source' and 'key' doesn't exist, to be added from A to the B and when 'source' and 'key' exist to replace the value from B with the one from A. The same way 'merge_array' function from php works on a multidimensional array.
I tried with 'A.union(B).groupBy(.key)' and then 'groupBy(.source)' and get the 1st value. But then I realized that I can never be sure that first value will always be the value of A.
I'm quite new to scala and I really ran out of ideas how I could do this from a functional immutable point of view.
Anyone has any idea how I could do this?
Thank you
Edit:
case class TranslationValue(source: String, key: String, value: String)
def main(args:Array[String]):Unit = {
println(merge(data1.toSet, data2.toSet))
}
def merge(a: Set[TranslationValue], b: Set[TranslationValue]) = {
a.union(b).groupBy(_.key).flatMap{ case (s, v) =>
v.groupBy(_.source).flatMap{case (s1, v1) => {
for (res <- 0 to 0) yield v1.head
}
}
}
}
Example
data1 has this data
Set(
TranslationValue(messages,No,No),
TranslationValue(messages,OrdRef,Order Reference),
TranslationValue(messages,OrdId,Order Id)
)
data2 has this data
Set(
TranslationValue(messages,No,No),
TranslationValue(messages,OrdRef,OrderRef)
TranslationValue(messages,Name,Name)
)
putting data1 over data2 I want to obtain
List(
TranslationValue(messages,No,No),
TranslationValue(messages,OrdRef,Order Reference),
TranslationValue(messages,OrdId,Order Id)
TranslationValue(messages,Name,Name)
)
I know that what I do can be done better, but like I said, I'm learning :)
you can group in one go:
def merge(a: Seq[TranslationValue], b: Seq[TranslationValue]) = {
a.union(b).groupBy(t=>(t.key,t.source)).map(c=>c._2.head)
}
i think you could also override the equals method for TranslationValue so that two translation values are equal when source and key are the same(the hashcode method has also to be overridden). Then a.union(b) would be enough.
edit:
It seems Set doesnt guarantee order of items(Scala: Can I rely on the order of items in a Set?), but a seq should.
I'm new to OCaml and i'm trying to understand the concept of mutable record field.
I'd like to create an array of records and that record contains a boolean mutable field. So i did something like:
type t = {i: int; mutable b: bool};;
I want to be able to change the value of the 'b' field of the record, so i put it mutable
let m = Array.make 10 ({i = 5; b = false});;
Here i try to set the b field of the record located at the first index of my array :
(Array.get m 0).b <- true;;
The problem is want i do it, it will set 'b' field of all the records of the array, and this is not what i want.
Does the mutable fields of a same record share the same memory emplacement? How can i do to change the value of the 'b' field of a specific record?
As the documentation states, Array.make creates an array whose elements are all physically equal. That's not a problem if these elements are immutable, but as you saw, you have to take that into account if they are mutable.
What you can do is use Array.init, to create a different object for every cell of your array:
let m = Array.init 10 (fun _ -> {i = 5; b = false});;
I have converted grid1 and grid2 into arrays and using following function which iterates through table and should return corresponding value form table when grid1 and grid2 values are matched. But somehow the final output contain only 4 integer values which isn't correct. Any suggestion what is possibly wrong here?
def grid(grid1,grid2):
table = {(10,1):61,(10,2):75,(10,3):83,(10,4):87,
(11,1):54,(11,2):70,(11,3):80,(11,4):85,
(12,1):61,(12,2):75,(12,3):83,(12,4):87,
(13,1):77,(13,2):85,(13,3):90,(13,4):92,}
grid3 = np.zeros(grid1.shape, dtype = np.int)
for k,v in table.iteritems():
grid3[[grid1 == k[0]] and [grid2 == k[1]]] = v
return grid3
I think what's happening is that the assignment to the variables "k" and "v" not done using "deepcopy". This means the assignment is just to the variables and not their values. For example, when the value of "k" changes on subsequent iterations, all previous "gridx" assignments now reflect the new/current status of "k".