This python loop needs a fix - python

So, I have this code:
path_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9',
'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9',
'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']
def grabber():
a = 0
max_path = 39
path = path_list[int(a)]
while a <= max_path:
print(path)
a = a+1
And when I run grabber(), It returns the same item, always.
So, running grabber() always return 0, which is the first element of the path_list
So, how can I solve it? I really need this loop to change the item in the list.

You can use a for loop, it's much simpler:
path_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9',
'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9',
'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']
def grabber():
for path in path_list:
print(path)
or alternately:
def grabber():
for i in range(len(path_list)):
print(path_list[i])

Move the path = path_list[int(a)] line inside the loop.

Related

Split data in list based on condition

I have following list :
data = ['A1', 'C3', 'B2', 'A2', 'D3', 'C2', 'A3', 'D2', 'C1', 'B1', 'D1', 'B3']
I want to split the list such that
split1 = ['A1', 'C3', 'B2', 'A2', 'C2', 'A3', 'C1', 'B1', 'B3']
split2 = ['D3', 'D2', 'D1']
Constraint is that no item with same prefix(A, B, etc.) can wind up in separate list. The data can be split in any ratio like 50-50, 80-20.
Here you go:
import numpy as np
data = np.array(['A1', 'C3', 'B2', 'A2', 'D3', 'C2', 'A3', 'D2', 'C1', 'B1', 'D1', 'B3'])
# define some condition
condition = ['B', 'D']
boolean_selection = [np.any([ c in d for c in condition]) for d in data]
split1 = data[boolean_selection]
split2 = data[np.logical_not(boolean_selection)]

Python saving results to variables

I am trying to loop through an array and I am checking where I have connection. (from an other function comes back a 0 or 1)
The bold arrays should be my results. I want to save them to a global variable, myResults, but if I print it at the end, then I will get back a much longer array...
I think the global variable is overwritten, why does it happen?
Thank you :)
[[['A2', 'A1', 'B1', 'B2', 'B3'], 'A3'], 26]
[[['A2', 'A1', 'B1', 'B2', 'B3', 'B4', 'A4'], 'A3'], 32]
[[[['A2', 'A1', 'B1', 'B2', 'B3', 'B4', 'A4', 'B4', 'C4', 'C3', 'C2', 'C1', 'D1', 'D2', 'D3', 'D4', 'D3', 'D4', 'D3', 'D4', 'D3', 'D4', 'D3', 'D4', 'D3', 'D4', 'D3', 'D4', 'C4', 'D4', 'C4', 'D4', 'C4', 'D4', 'C4', 'D4'], 'A3'], 26], [[['A2', 'A1', 'B1', 'B2', 'B3', 'B4', 'A4', 'B4', 'C4', 'C3', 'C2', 'C1', 'D1', 'D2', 'D3', 'D4', 'D3', 'D4', 'D3', 'D4', 'D3', 'D4', 'D3', 'D4', 'D3', 'D4', 'D3', 'D4', 'C4', 'D4', 'C4', 'D4', 'C4', 'D4', 'C4', 'D4'], 'A3'], 32]]
def callbackFunction(start, end, time):
myResult = []
def logToVariable(path, goal, timeFor):
finalPath = path
finalPath.append(goal)
print(finalPath)
add_result = [finalPath, timeFor]
print(add_result)
myResult.append(add_result)
def innerLoop(start, end, time):
correctList = [item for item in myList if item not in start]
for i in correctList:
first = start[-1]
time_diff = directPoints(first, i, time)
if time_diff[1] == 1:
if i==end:
#new_result = []
new_result = [[start, i], time_diff[0]]
#logToVariable(start, i, time_diff[0])
# print(start, i)
myResult.append(new_result)
print(new_result)
#break
#return result
else:
start.append(i)
innerLoop(start, end, time_diff[0])
else:
continue
innerLoop(start, end, time)
print(myResult)
return myResult

haw can I find the smallest list among some lists generated by my program?

I wrote a program that generates some lists, something like
['a0', 'a1', 'a2', 'a3', 'a3', 'a4', 'C', 'b4', 'b3', 'b2', 'b2', 'b3', 'b4', 'b5', 'b5', 'b4', 'D', 'c4']
['a0', 'a1', 'a2', 'a3', 'a3', 'a4', 'C', 'b4', 'b3', 'b2', 'b2', 'b3', 'b4', 'D', 'c4', 'c4', 'D', 'b4', 'b5']
['a0', 'a1', 'a2', 'a3', 'a3', 'a4', 'C', 'b4', 'b5', 'b5', 'b4', 'b3', 'b2', 'b2', 'b3', 'b4', 'D', 'c4']
['a0', 'a1', 'a2', 'a3', 'a3', 'a4', 'C', 'b4', 'b5', 'b5', 'b4', 'D', 'c4', 'c4', 'D', 'b4', 'b3', 'b2']
['a0', 'a1', 'a2', 'a3', 'a3', 'a4', 'C', 'b4', 'D', 'c4', 'c4', 'D', 'b4', 'b3', 'b2', 'b2', 'b3', 'b4', 'b5']
['a0', 'a1', 'a2', 'a3', 'a3', 'a4', 'C', 'b4', 'D', 'c4', 'c4', 'D', 'b4', 'b5', 'b5', 'b4', 'b3', 'b2']
and I want to find the shortest list, the list that has the minimum number of elements
thanks,
You can use the min function:
min(data, key = len)
If you want to handle cases where there are multiple elements having the shortest length, you can sort the list in ascending order by length:
sorted(data, key = len)
You can sort it by list length then get the first element but this won't take into account lists that all have the same length.
smallest_list = sorted(list_of_list, key=len)[0]
Another would be get the length of the smallest list then use that as a filter
len_smallest_list = min(len(x) for x in list_of_list)
smallest_list = [list for list in list_of_list if len(list) == len_smallest_list]

Mixing lists in python

from itertools import product
x_coord = ['a','b','c','d','e']
y_coord = ['1', '2', '3', '4', '5']
board = []
index = 0
for item in itertools.product(x_coord, y_coord):
board += item
for elements in board:
board[index] = board[index] + board[index +1]
board.remove(board[index +1])
index += 1
print board
Hello. Let me explain what I want to do with that:
I have two lists(x_coord and y_coord) and I want to mix them like that:
board = ['a1', 'a2', ..., 'e1', 'e2', ...]
But I get the IndexError: list index out of range error instead of that.
How should I proceed?
OBS.:If there's any type of error in my english, please tell me. I'm learning english as well as code.
You can try like that,
>>> x_coord = ['a','b','c','d','e']
>>> y_coord = ['1', '2', '3', '4', '5']
>>> [item + item2 for item2 in y_coord for item in x_coord]
['a1', 'b1', 'c1', 'd1', 'e1', 'a2', 'b2', 'c2', 'd2', 'e2', 'a3', 'b3', 'c3', 'd3', 'e3', 'a4', 'b4', 'c4', 'd4', 'e4', 'a5', 'b5', 'c5', 'd5', 'e5']
Sorted results:
>>> sorted([item + item2 for item2 in y_coord for item in x_coord])
['a1', 'a2', 'a3', 'a4', 'a5', 'b1', 'b2', 'b3', 'b4', 'b5', 'c1', 'c2', 'c3', 'c4', 'c5', 'd1', 'd2', 'd3', 'd4', 'd5', 'e1', 'e2', 'e3', 'e4', 'e5']
combined = map(lambda x: ''.join(x), product(x_coord, y_coord))
coord = map(lambda x,y: x+y, x_coord, y_coord)
print(coord)
t = [a + b for a,b in itertools.product(x_coord,y_coord)]
print t % prints what you want
Normally itertools.product(x_coord,y_coord) will print the following:
[('a', '1'), ('a', '2'), ('a', '3'), ('a', '4'), ('a', '5'), ('b', '1'), ('b', '2'), ('b', '3'), ('b', '4'), ('b', '5'), ('c', '1'), ('c', '2'), ('c', '3'), ('c', '4'), ('c', '5'), ('d', '1'), ('d', '2'), ('d', '3'), ('d', '4'), ('d', '5'), ('e', '1'), ('e', '2'), ('e', '3'), ('e', '4'), ('e', '5')]
As you can see it's already in order because itertools.product will multiply a by each index in y_coord before moving x_coord to b, etc. etc.
By using list comprehension we can combine the two indices using a+b for each pair in the output, resulting in this:
['a1', 'a2', 'a3', 'a4', 'a5', 'b1', 'b2', 'b3', 'b4', 'b5', 'c1', 'c2', 'c3', 'c4', 'c5', 'd1', 'd2', 'd3', 'd4', 'd5', 'e1', 'e2', 'e3', 'e4', 'e5']
x_coord = ['a','b','c','d','e']
y_coord = ['1', '2', '3', '4', '5']
a=[]
for i in range(len(x_coord)):
for j in range(len(y_coord)):
a.append(x_coord[i].join(" "+ y_coord[j]))
b=[]
for item in a:
b.append(item.replace(" ",""))
print b

List of Lists to List of Dictionaries

How can I convert a list of lists into a list of dictionaries?
More specifiicaly: How do I go from this:
[['a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1', 'i1'], ['a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2', 'i2'], ['a3', 'b3', 'c3', 'd3', 'e3', 'f3', 'g3', 'h3', 'i3'], ['a4', 'b4', 'c4', 'd4', 'e4', 'f4', 'g4', 'h4', 'i4'], ['a5', 'b5', 'c5', 'd5', 'e5', 'f5', 'g5', 'h5', 'i5'], ['a6', 'b6', 'c6', 'd6', 'e6', 'f6', 'g6', 'h6', 'i6'], ['a7', 'b7', 'c7', 'd7', 'e7', 'f7', 'g7', 'h7', 'i7'], ['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8', 'i8'], ['a9', 'b9', 'c9', 'd9', 'e9', 'f9', 'g9', 'h9', 'i9']]
to this:
[{'a1': None, 'b1': None, 'c1': None, 'd1': None, 'e1': None, 'f1': None, 'g1': None, 'h1': None, 'i1': None}, #etc
In [20]: l = [['a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1', 'i1'], ['a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2', 'i2'], ['a3', 'b3', 'c3', 'd3', 'e3', 'f3', 'g3', 'h3', 'i3'], ['a4', 'b4', 'c4', 'd4', 'e4', 'f4', 'g4', 'h4', 'i4'], ['a5', 'b5', 'c5', 'd5', 'e5', 'f5', 'g5', 'h5', 'i5'], ['a6', 'b6', 'c6', 'd6', 'e6', 'f6', 'g6', 'h6', 'i6'], ['a7', 'b7', 'c7', 'd7', 'e7', 'f7', 'g7', 'h7', 'i7'], ['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8', 'i8'], ['a9', 'b9', 'c9', 'd9', 'e9', 'f9', 'g9', 'h9', 'i9']]
In [21]: map(dict.fromkeys, l)
Out[21]:
[{'a1': None,
'b1': None,
'c1': None,
'd1': None,
'e1': None,
'f1': None,
'g1': None,
'h1': None,
'i1': None},
{'a2': None,
'b2': None,
'c2': None,
'd2': None,
...
This will work with any iterable of iterables, not just a list of lists (provided, of course, that the second-level elements are hashable).
In Python 2, the above code returns a list.
In Python 3, it returns an iterable. If you require a list, you could use list(map(dict.fromkeys, l)).
Try this:
l = [['a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1', 'i1'], ['a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2', 'i2'], ['a3', 'b3', 'c3', 'd3', 'e3', 'f3', 'g3', 'h3', 'i3'], ['a4', 'b4', 'c4', 'd4', 'e4', 'f4', 'g4', 'h4', 'i4'], ['a5', 'b5', 'c5', 'd5', 'e5', 'f5', 'g5', 'h5', 'i5'], ['a6', 'b6', 'c6', 'd6', 'e6', 'f6', 'g6', 'h6', 'i6'], ['a7', 'b7', 'c7', 'd7', 'e7', 'f7', 'g7', 'h7', 'i7'], ['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8', 'i8'], ['a9', 'b9', 'c9', 'd9', 'e9', 'f9', 'g9', 'h9', 'i9']]
res = []
for line in l:
res.append(dict((k, None) for k in line))
OR:
res = [dict((k, None) for k in line) for line in l]

Categories

Resources