Print key of list w/o knowing its position - list

I have a list that contains many keys:
mylist = {"a", "b", "c", "1", "2", "3", ...}
and I want to print the key for example that has value "x", without knowing it's exact position in the list. That mean I have to run the whole list and till "x" is found and print it. How could I do this? Seems easy question but it confuses me a bit... Thanks a lot

for key, value in pairs(mylist) do
if value == "x" then print(key) end
You can also create another mapping, eg.
mapping_list = {}
for key, value im pairs(mylist) do
mapping_list[value] = key
(assuming that list elements are unique) then, you'd be able to
print(mapping_list["x"])

Related

Map Reduce Removing Duplicates

I have been given a large text file and want to find the number of different words that start with each letter. I am trying to understand input and output values for map and reduce functions.
I understand a simpler problem which does not need to deal with duplicate words: determine the frequency with which each letter of the alphabet starts a word in the text using map reduce.
Map input: <0, “everyday i am city in tomorrow easy over school i iterate tomorrow city community”>
Map output: [<e,1>,<i,1>,<a,1>,<c,1>,<i,1>,<t,1>,<e,1>,<o,1>,<s,1>,<i,1>,<i,1>,<t,1>,<c,1>,<c,1>]
Reduce input: <a,[1]>,<c,[1,1,1]>,<e,[1,1]>,<i,[1,1,1,1]>,<o,[1]>,<s,[1]>,<t,[1,1]>
Reduce output: [<a,1>,<c,3>,<e,2>,<i,4>,<o,1>,<s,1>,<t,2>]
For the above problem the words 'i' 'city' and 'tomorrow' appear more than once so my final output should be:
Reduce output: [<a,1>,<c,2>,<e,2>,<i,3>,<o,1>,<s,1>,<t,1>]
I am unsure of how I would ensure duplicate words are remove in the above problem (would it be done in a pre processing phase or could it be implemented on either map or reduce functions). If I could get help understanding the map and reduce outputs of the new problem I would appreciate it.
You can do it in two map-reduce passes:
find all the distinct word by using word as a map output and in reduce outputting each word once
you already solved - find frequency of each initial letter on each unique word.
Alternatively, since there are not many unique words you can cache them in mapper and output each one (or its first letter) only once and the reduce will be identical to your simpler problem. Actually, no, that won't work because same words can appear in different mappers. But you can still cache the words in mapper in the first solution and output each word only once per mapper - a little bit less traffic between map and reduce.
Maybe something like this would help,
let str = "everyday i am city in tomorrow easy over school i iterate tomorrow city community"
let duplicatesRemoved = Set(str.split(separator: " "))
Output:
["city", "community", "tomorrow", "easy", "everyday", "over", "in", "iterate", "i", "am", "school"]
And maybe you don't need those map statements and can achieve something like this,
Code
var varCount = [Character: Int]()
for subStr in duplicatesRemoved {
if let firstChar = subStr.first {
varCount[firstChar] = (varCount[firstChar] ?? 0) + 1
}
}
Output
["i": 3, "t": 1, "e": 2, "c": 2, "s": 1, "a": 1, "o": 1]

How can I find how many times a string in a list is a substring of another string that is in another list?

Basically I have to lists of strings
list_one = ["a", "b", "c"]
list_two = ["arrr", "aaa", "acacb"]
expected output would be 9, since "a", "b" and "c" appear 9 times.
Sorry for my English!
Firstly please tell us what language are you using, and what have you tried as a solution?
Now since you called it a list, I am assuming it to be python
counter = []
for s in list_one:
temp = list(map(lambda x: x.count(s), list_two))
counter.append(sum(temp))
output = sum(counter)
If it is not python, try to understand the logic and apply it in the language you are using

Looping through a list (with sublists) and assign the matching IDs to the same key and all of the corresponding values from that sublist?

I'm quite new in python coding and I can´t solve the following problem:
I have a list with trackingpoints for different animals(ID,date,time,lat,lon) given in strings:
aList = [[id,date,time,lat,lon],
[id2,date,time,lat,lon],
[...]]
The txt file is very big and the IDs(a unique animal) is occuring multiple times:
i.e:
aList = [['25','20-05-13','15:16:17','34.89932','24.09421'],
['24','20-05-13','15:16:18','35.89932','23.09421],
['25','20-05-13','15:18:15','34.89932','24.13421'],
[...]]
What I'm trying to do is order the ID's in dictionaries so each unique ID will be the key and all the dates, times, latitudes and longitudes will be the values. Then I would like to write each individual ID to a new txt file so all the values for a specific ID are in one txt file. The output should look like this:
{'25':['20-05-13','15:16:17','34.89932','24.09421'],
['20-05-13','15:18:15','34.89932','24.13421'],
[...],
'24':['20-05-13','15:16:18','35.89932','23.09421'],
[...]
}
I have tried the following (and a lot of other solutions which didn't work):
items = {}
for line in aList:
key,value = lines[0],lines[1:]
items[key] = value
Which results in a key with the last value in the list forthat particular key :
{'25':['20-05-13','15:18:15','34.89932','24.13421'],
'24':['20-05-13','15:16:18','35.89932','23.09421']}
How can I loop through my list and assign the same IDs to the same key and all the corresponding values?
Is there any simple solution to this? Other "easier to implement" solutions are welcome!
I hope it makes sense :)
Try adding all the lists that match to the same ID as list of lists:
aList = [['25','20-05-13','15:16:17','34.89932','24.09421'],
['24','20-05-13','15:16:18','35.89932','23.09421'],
['25','20-05-13','15:18:15','34.89932','24.13421'],
]
items = {}
for line in aList:
key,value = line[0],line[1:]
if key in items:
items[key].append(value)
else:
items[key] = [value]
print items
OUTPUT:
{'24': [['20-05-13', '15:16:18', '35.89932', '23.09421']], '25': [['20-05-13', '15:16:17', '34.89932', '24.09421'], ['20-05-13', '15:18:15', '34.89932', '24.13421']]}

Modify dictionary in Python

I have the following dictionary:
DATA = {"records": [{"key1": "AAA", "key2": "BBB", "key3": "CCC",
"key4": "AAA"}]}
I want to change "records" with for example "XX" so I will have the following:
DATA = {"XX": [{"key1": "AAA", "key2": "BBB", "key3": "CCC", "key4":
"AAA"}]}
How can I do this? Thank you.
You can't change a string dictionary key directly in the way you have presented. The reason for this is that the key itself- which again, is a string, or str object- is immutable. This would not necessarily be true for other types of keys, such as for a user-defined object.
Your only option is to add the new key to the existing dictionary, or create a new dictionary with the key you want.
You can assign a new dictionary to the same name, DATA, and add the entry you want to the new dictionary (the old one will eventually be garbage collected):
DATA = {'XX': DATA['records']}
IMPORTANT: Note that any other references to the old DATA dictionary will NOT be updated! For this reason, the new dictionary approach is probably not what you want.
Alternatively, if it is acceptable to keep the records key in your dictionary, you can simply add the XX member to the dictionary and point it to the same value:
DATA['XX'] = DATA['records']
Lastly, if you both want to keep the original dictionary AND remove the records key from it, you will have to do that in two steps:
DATA['XX'] = DATA['records']
del DATA['records']
OR:
DATA['XX'] = DATA.pop('records')
Note that the last suggestion still occurs in two steps even though it is one line of code.

How to cross ref's from dictionaries & lists

I am new to the whole python thing. I have a question and will try to keep it short.
I am trying to write a program where I have a group of items as below.
product_lookup = {"C1557E" : "FM51", "C1557E" : "JBC4343" "C1565ECA/2" : "FM349",
"C1568E" : "FM133", "C1578E" : "FM154"}
Now I have a enquiry (list of values) as below that I want to cross referance with the dictionary
enquiry_lookup = ["FM51", "FM133", "FM154", "GRE4534"]
Then I" want this displayed as
result ["FM51" : "C1557E", "FM133" : "C1568E", "FM154" : "C1578E", "GRE4534" : "NOT AVAILABLE"]
Firstly, is it possible?
I am stick... HELP PLEASE :)
Only just started and after working on a few things I am getting the feeling it might not be possible??
Please point me in the right direction.
Thanks a stack
G
This might give you a clue:
>>> dict = {"a" : "b", "c" : "d"}
>>>
>>> for k,v in dict.iteritems():
... print k
... print v
...
a
b
c
d
By using iteritems(), we iterate over all key,value pairs in our dictionary. So you can inspect the value to see if it is the value you are looking for, if so you can place the key and the value in your result dictionary.
But, if you are interested in a set of items, and those items are the values instead of the keys of your dictionary, are you sure your dictionary is not backwards? By which I mean, could your dictionary instead be
product_lookup = {"FM51" : "C1557E", etc}
then when you have
enquiry_lookup = ["FM51", etc
you can just see if product_lookup["FM51"] exists? This could be more efficient/easier to write.