Mapping two arrays containing strings to the same integer values - python

I have 2 arrays like:
['16.37.235.200','17.37.235.200','16.37.235.200', '18.37.235.200']
['17.37.235.200','17.37.235.200','16.37.235.200', '17.37.235.200']
And I want to map (injective) every IP address to an integer value.
Like for that instance above, eg.:
[0,1,0,3]
[1,1,0,1]
Is their an existing function (of NumPy or anything else) for that?

Ok i found this solution for seperate mapping of the lists
Python Map List of Strings to Integer List
Works like i want for seperated mapping of the 2 lists.

Related

How to manipulate string items in a 3d list in python? (without numpy)

I have a 3d list 6 items long and 6 items wide, which is a list of lists of a list of strings.
lst = [ [['A'],['A'],['B'],['B'],['A'],['A']],
[['B'],['B'],['A'],['A'],['B'],['B']],
[['A'],['A'],['B'],['B'],['A'],['A']],
[['B'],['B'],['A'],['A'],['B'],['B']],
[['A'],['A'],['B'],['B'],['A'],['A']],
[['B'],['B'],['A'],['A'],['B'],['B']],
]
I want to move the strings into other locations on the list, but I know I'm not using the right code:
lst.insert([1][0][0], 'A')
gives me a TypeError: 'int' object is not subscriptable
I know how to add items by doing this:
lst2 = lst[0][0]
lst2.append('A')
(adds another 'A' string to the first item)
I want to perform various actions on the lowest list like:
add/remove strings to that list,
check how many string items are in that list
move 'A' or 'B' strings to different locations so that they have multiple strings.
Check to see what the first string is in the list is
I am very new to programming and I am just starting to understand how to use 2d lists.
How do I accomplish this without any additional modules or libraries?
First of all, let me clarify this line:
lst.insert([1][0][0], 'A')
The insert method expects an int argument for the index. If you want to insert an element in a multidimensional list it should be done as:
lst[1][0].insert(0, 'A')
After all, it is a list of list (of lists). Only if you look at an inner index (defined by 2 coordinates), will you get a simple list (of strings in this case). You can then insert a string element to this simple list, by calling the insert() method.
check how many string items are in that list
count = 0
for d2 in lst: #d2 is a 2d list (an element of a 3d list)
for d1 in d2: # d1 - 1 dimensional
count += len(d1)
Here, I have gone through each of the lowermost-level (simple) lists using a nested loop, counted how many elements are there in each and added them up.
move 'A' or 'B' strings to different locations so that they have multiple strings.
Say I want to move the element from [3][2][0] to [1][2][1]. I would insert it in the new position and then delete from the old.
element = lst[3][2][0] # the task can be accomplished without using another variable, but this is for better understanding
lst[1][2].insert(1, element) # inserting
lst[3][2].pop(0) # deleting
The element could even be moved to a position like [1][2]. So there would be one string along with the other 'sub-lists'
Check to see what the first string is in the list is
Do you mean lst[0][0][0].

How to sort a list of tuples by the length of the tuple

I have a list, which has a bunch of tuples of different sizes inside. Each Tuple contains a certain number of objects. I want to sort the list so the tuples with 1 object are all before tuples with 2 objects etc. I know tuples have a built in length but I dont know how to use it with the sort method in python. Thank you!
Use this:
print(sorted(list_of_tuples,key=len))
Or:
list_of_tuples.sort(key=len)
print(list_of_tuples)
Both reproduce the expected result.

ValueError: too many values to unpack in a list

In python I have a list like below
in_list =[u'test_1,testing_1', u'test_2,testing_2', u'test_3,testing_3']
I want to print the values in this list in a loop
for test, testing in input:
print test, testing
I get this error:
ValueError: too many values to unpack
What is the correct method?
You have a list of three values on the right side; you have only two variables on the left. Doing this assignment of a sequence (list, in your case) to a series of variables is called "unpacking". You must have a 1:1 correspondence between values and variables for this to work.
I think what you're trying to do is to iterate through comma-separated value pairs. Try something like the code below. Iterate through the three strings in your input list (use a different variable name: input is a built-in function). For each string, split it at the comma. This gives you a list of two values ... and those you can unpack.
for pair in input_list: # "input" is a built-in function; use a different name
test, testing = pair.split(',')
# continue with your coding

how to create a list containing of 100 number of strings whose names are in series

a list of string objects is like
nodes=["#A_CN1","#A_CN2","#A_CN3","#A_CN4","#A_CN5","#A_CN6","#A_CN7","#A_CN8","#A_CN9","#A_CN10"]
Here in the above list there are 10 elements but i need to use around 100 elements and the element is like #A_CN100
Is there any way to represent it shortly rather than writing 100 times in python ?
If suppose there is a list of 100 elements where each element itself is a list like, node1 , node2.. all are some lists
nodes=[node1,node2,node3,node4,node5,node6....node100]
if I express this as
nodes=[node{0}.format(i) for i in range(1,101)]
But,this throws an error! How to rectify this?
A one liner with list comprehensions
nodes = ["#A_CN{0}".format(i) for i in range(1,101)]
There is also a suggestion in the comments that a generator version be demonstrated. It would look like this:
nodes = ("#A_CN{0}".format(i) for i in range(1,101))
But more commonly this is passed to list
nodes = list("#A_CN{0}".format(i) for i in range(1,101))
So we end up with the same result as the list comprehension. However the second form is useful if you want to generate about a million items.
You omitted quotes (or apostrophes). Instead of
nodes=[node{0}.format(i) for i in range(1,101)]
use
nodes=["node{0}".format(i) for i in range(1,101)]

Remove None from tuple

previous answers on how to remove None from a list dont help me!
I am creating a list of tuples with:
list(zip(*[iter(pointList)] *3))
So what i have is
[(object1,object2,object3),(object4,object5,object6),(object7,None,None)]
or
[(object1,object2,object3),(object4,object5,object6),(object7,object8,None)]
And i want to remove the None in the tuples (which only can occure at the last entry of the list!). So my desired output would be:
[(object1,object2,object3),(object4,object5,object6),(object7)]
or
[(object1,object2,object3),(object4,object5,object6),(object7,object8)]
What i thought would help me is:
filter(None,myList)
Tuples are immutable so once you construct a tuple you cannot alter its length or set its elements. Your only option is thus to construct new tuples as a post processing step, or don't generate these tuples in the first place.
Post processing
Simply use a generator with the tuple(..) constructor in a list comprehension statement:
[tuple(xi for xi in x if xi is not None) for x in data]
Alter the "packing" algorithm
With packing I mean converting a list of m×n items into n "slices" of m elements (which is what your first code fragment does).
If - like the variable name seems to suggest - pointList is a list. You can save yourself the trouble of using zip, and work with:
[tuple(pointList[i:i+3]) for i in range(0,len(pointList),3)]
directly. This will probably be a bit more efficient as well since here we never generate tuples with Nones in the first place (given postList does not contain Nones of course).

Categories