0

I'm attempting to search and replace using information from 2 lists, this is whilst caching any replacements that have been done so the same corresponding values can be given.

For example, I have the following -

names = ["Mark","Steve","Mark","Chrome","192.168.0.1","Mark","Chrome","192.168.0.1","192.168.0.2"] 

type = ["user","user","user","process","address","user","process","adress","address"]

And I'm hoping to get the following output -

{
"Mark":"user1",
"Steve":"user2",
"Chrome":"process1",
"192.168.0.1":"adress1",
"192.168.0.2":"adress2"
}

So trying to use the type in the the 2nd list to determine the item in the first list's corresponding value.

Hope this makes sense, is this possible? Any help would be appreciated.

md1305
  • 21
  • 1

3 Answers3

0

I would recommend you use a dictionary personally.

names = {
    "Mark": "user",
    "Steve": "user2",
    "Chrome": "process1",
    "192.168.0.1": "address1",
    "192.168.0.2": "address2"
}

print(names["Mark"])

By using this dictionary you can precisely tap into the name you'd like to information of or anything else you want. It is also a little more readable

joeca
  • 46
  • 9
  • Also if you wanted to print all of the names like the output you wanted just use print(names) and it will give a similar output – joeca Oct 07 '22 at 10:03
  • Hi, thanks for your answer. This would be good, however I'm going to be working with duplicates. As seen in the first list, there will potentially be more than one occurrence of a certain value to begin with, hence the need for caching :/ – md1305 Oct 07 '22 at 10:26
0

To form a dictionary from said values you can iterate the range and access values with the same index:

output = {names[i]: types[i] for i in range(len(names))}

Also refrain from using variable name type because it's already taken by a builtin Python syntax.

LTJ
  • 1,197
  • 3
  • 16
0

Looks like you're also trying to store / retrieve the count of the types (i.e. "user1", "user2, "address1", etc.). Hence, we need another data structure to keep count of the types already registered in our "hashmap" (dictionary in python). In the below solution, we use the type_cache.

The code should work as is.

from collections import defaultdict

names = ["Mark", "Steve", "Mark", "Chrome", "192.168.0.1", "Mark", "Chrome", "192.168.0.1", "192.168.0.2"]
types = ["user", "user", "user", "process", "address", "user", "process", "address", "address"]
expected = {
    "Mark": "user1",
    "Steve": "user2",
    "Chrome": "process1",
    "192.168.0.1": "address1",
    "192.168.0.2": "address2"
}


def process_names_and_types(names, types):
    result = {}
    type_cache = defaultdict(int)

    for name, type_ in zip(names, types):
        if name not in result:
            type_cache[type_] += 1
            result[name] = f"{type_}{type_cache[type_]}"
    return result


if __name__ == "__main__":
    actual = process_names_and_types(names, types)
    assert actual == expected, f"Expected: {expected}, Actual: {actual}"