How for loop works in python? - python

I have a lists of lists in variable lists something like this:
[7, 6, 1, 8, 3]
[1, 7, 2, 4, 2]
[5, 6, 4, 2, 3]
[0, 3, 3, 1, 6]
[3, 5, 2, 14, 3]
[3, 11, 9, 1, 1]
[1, 10, 2, 3, 1]
When I write lists[1] I get vertically:
6
7
6
3
5
11
10
but when I loop it:
for i in list:
print(i)
I get this horizontally.
7
6
1
8
3
etc...
So, how it works? How can I modify loop to go and give me all vertically?

Short answer:
for l in lists:
print l[1]

Lists of lists
list_of_lists = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]
for list in list_of_lists:
for x in list:
print x

Here is how you would print out the list of lists columns.
lists = [[7, 6, 1, 8, 3],
[1, 7, 2, 4, 2],
[5, 6, 4, 2, 3],
[0, 3, 3, 1, 6],
[3, 5, 2, 14, 3],
[3, 11, 9, 1, 1],
[1, 10, 2, 3, 1]]
for i in range(0, len(lists[1])):
for j in range(0, len(lists)):
print lists[j][i],
print "\n"

Related

How do I roll 4 dice 6 different times and have the output of all 6 iterations?

So what I'm trying to do is be able to roll 6 sets of 4 individual 6 sided dice. The current code that I currently have is:
import random
stat_rolls = []
for i in range(6):
for j in range(4):
num = random.randint(1, 6)
stat_rolls.append(num)
print(stat_rolls)
Currently the output is as follows:
[5, 6, 5, 2, 5, 6, 2, 2, 4, 6, 3, 1, 3, 5, 5, 3, 5, 5, 6, 4, 3, 4, 1, 4]
What I actually want the output to look like is the following:
[5, 6, 5, 2]
[5, 6, 2, 2]
[4, 6, 3, 1]
[3, 5, 5, 3]
[5, 5, 6, 4]
[3, 4, 1, 4]
How would this be possible? Would it be through a dict function? I'm still learning Python so any kind of help would be much appreciated!!
One really basic whay of achieving what you want is to create a buffer array that gets filled with a cycle of trows and then appending it to an upper array to get the desired format:
import random
stat_rolls = []
buffer=[]
for i in range(6):
buffer=[]
for j in range(4):
num = random.randint(1, 6)
buffer.append(num)
stat_rolls.append(buffer)
print(stat_rolls)
this outputs:
[[4, 4, 6, 1], [1, 4, 2, 3], [3, 5, 3, 3], [3, 2, 5, 2], [6, 3, 5, 4], [3, 2, 5, 1]]
you can then change your print format to get to different print layouts,for example,if you print it out as:
for i in stat_rolls:
print(i)
you get:
[6, 4, 1, 4]
[3, 5, 2, 1]
[5, 1, 5, 6]
[6, 4, 1, 3]
[4, 6, 6, 2]
[5, 2, 3, 3]

Efficiently structure data into series by ID

I am looking at a very large structured dataset that I would like to make unstructured. Here is the example…
x1 x2 x3 day id
1 5 9 2 A
9 7 9 3 B
3 1 4 1 A
2 6 5 1 B
3 5 8 2 B
3 2 3 2 C
The rows above are presented in a random order. Another way to think of this example is as follows…
x = [[1, 5, 9, 2, “A”],
[9, 7, 9, 3, “B”],
[3, 1, 4, 1, “A”],
[2, 6, 5, 1, “B”],
[3, 5, 8, 2, “B”],
[3, 2, 3, 2, “C”]]
Once processed, the desired output is…
[[[3, 1, 4, 1], [1, 5, 9, 2]],
[[2, 6, 5, 1], [3, 5, 8, 2], [9, 7, 9, 3]],
[[3, 2, 3, 2]]],
[[1, A], [1,B], [2,C]]
The first list has the x variables, and the second list has the start date with each identifier.
I have an idea of how to achieve this, but it is in O(n^3). Is there a more efficient method, maybe in O(nlogn)?
Edit: Although mentioned in my previous post, I have made it clearer that the rows are presented in random order. I have also removed redundant column in the code example.
Try:
x = [
[3, 1, 4, 1, 1, "A"],
[1, 5, 9, 2, 2, "A"],
[2, 6, 5, 1, 1, "B"],
[3, 5, 8, 2, 2, "B"],
[9, 7, 9, 3, 3, "B"],
[3, 2, 3, 2, 2, "C"],
]
out = {}
for row in x:
out.setdefault(row[-1], []).append(row[:-1])
print(list(out.values()) + [[[v[0][-1], k] for k, v in out.items()]])
Prints:
[
[[3, 1, 4, 1, 1], [1, 5, 9, 2, 2]],
[[2, 6, 5, 1, 1], [3, 5, 8, 2, 2], [9, 7, 9, 3, 3]],
[[3, 2, 3, 2, 2]],
[[1, "A"], [1, "B"], [2, "C"]],
]

Python : nested list comprehension

I have a nested list issue that I cannot solve.
Input
li=
[1, 2, 3, 4]
[5, 6, 7, 8]
[9,10,11,12]
newList = [[0 if kk==6 or kk==7 else kk for kk in x] for x in li]
output
[1, 2, 3, 4]
[5, 0, 0, 8]
[9,10,11,12]
I want to print this output with list slicing and index
and another output
[1,2,3,4,8,12,11,10,9,5]
like this 1
Slice numpy arrays
a=[[1, 2, 3, 4],
[5, 0, 0, 8],
[9,10,11,12]]
b=np.array(a)
list(b[0:1][0]) + list(b[1:,-1]) + list(b[-1:][0])[-2::-1] + list(b[1:,0])[-2::-1]
Output
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5]

Replacing items in a list in all possible permutations

Let's say I have a list or an array of the type:
mylist = [1, 2, 3, 4]
And I want to replace an item in this list. Normally I would use something like the following:
mylist[2] = 7
That works well. However, can someone explain how to create all possible permutations of mylist when replacing one or more items in mylist. For example, I want to create the following:
[7, 2, 3, 4]
[7, 7, 3, 4]
[7, 2, 7, 4]
[7, 7, 7, 4]
[7, 2, 3, 7]
...(and so on)
I know I can use itertools to generate all possible permutations, but how can I specify that I want to substitute an item in all possible locations in the list before generating the permutations? Here is how I tried to use itertools:
list(itertools.permutations([1,2,3,4,7], 4))
This doesn't work because it doesn't substitute 7 more than one time per permutation, and it also generates permutations that do not include the number 7.
Use itertools.combinations to find the indices to replace:
replace = 7
mylist = [1, 2, 3, 4]
for i in range(1, len(mylist) + 1):
for selected in itertools.combinations(range(len(mylist)), i):
res = mylist[:]
for n in selected:
res[n] = replace
print(res)
Output:
[7, 2, 3, 4]
[1, 7, 3, 4]
[1, 2, 7, 4]
[1, 2, 3, 7]
[7, 7, 3, 4]
[7, 2, 7, 4]
[7, 2, 3, 7]
[1, 7, 7, 4]
[1, 7, 3, 7]
[1, 2, 7, 7]
[7, 7, 7, 4]
[7, 7, 3, 7]
[7, 2, 7, 7]
[1, 7, 7, 7]
[7, 7, 7, 7]
You can create a function and just pass the list and value to that function and you get what you want :
import itertools
def replaced_it(list_1,value):
final_list = []
len_=len(list_1)
track_index = [k for k, j in enumerate(list_1)]
for i in range(len(track_index) + 1):
empty_list = [value]
replaced_one = list_1[:]
for ia in itertools.permutations(track_index, r=i):
if ia:
for i, j in zip(ia, empty_list * len(ia)):
replaced_one[i] = j
if replaced_one not in final_list:
final_list.append(replaced_one)
replaced_one = list_1[:]
return final_list
print(replaced_it([1,2,3,4],7))
output:
[[7, 2, 3, 4], [1, 7, 3, 4], [1, 2, 7, 4], [1, 2, 3, 7], [7, 7, 3, 4], [7, 2, 7, 4], [7, 2, 3, 7], [1, 7, 7, 4], [1, 7, 3, 7], [1, 2, 7, 7], [7, 7, 7, 4], [7, 7, 3, 7], [7, 2, 7, 7], [1, 7, 7, 7], [7, 7, 7, 7]]

Create a list of list with flexible input (Python) [duplicate]

This question already has answers here:
How can I make a for-loop pyramid more concise in Python? [duplicate]
(4 answers)
Closed 5 years ago.
I currently have a function that creates a list of lists like below using 3 nested for-loops.
[[1,1,1] , [1,1,2] , .... , [3,3,3]]
However, the problem is I can't use this function if someone wants the list of list to be something like
[[1,1,1,1,1,1,1] , ..... , [9,9,9,9,9,9,9]]
which has more numbers (from 1 - 9) and more elements (7 of 1's instead of 4).
Here's my current code:
def listofList():
temp = []
for i in range(1,4):
for j in range(1,4):
for k in range(1,4):
temp.append([i,j,k])
return temp
Can someone provide me with a better solution? I want my function listofList() to be flexible where it could receive an input for both the size of the list of list and the elements inside the list.
Try the following:
def listofList(subLen, totalLen):
final = [[item for i in range(subLen)] for item in range(1, totalLen+1)]
return final
>>> listofList(9, 9)
[[1, 1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6, 6, 6, 6], [7, 7, 7, 7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9, 9, 9, 9]]
>>> listofList(9, 2)
[[1, 1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2, 2]]
>>> listofList(2, 9)
[[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9]]
>>>

Categories

Resources