Making array of arrays [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Basically, im trying to make array of arrays in python and i don't know how really. It needs to be in "for" loop and each time it goes trough loop, it needs to add array of some numbers to the array of arrays but of course on index+1
EDIT:
this is what i already tried, but it gives me error:
for x in range(5):
poljeRazina[x][x]=1

In Python, arrays are called Lists. This could be helpful when googling.
Also, you can declare them like this (depth-based):
x = [[]]
Then...
for big in range(10): #outer loop
for small in range(10): #inner loop
x[big].append(small*big) #add new number
print('%03d' % x[big][small], end=" ") #we print the number padded to 3 digits
x.append([]) #add another internal list
print() #move down to next line
Equals...

List comprehension might help
[list(range(i)) for i in range(5)]

Related

if you know exact number of int you want in your list, how do you put it in one line [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
If I know that I need to put for example 5 numbers in a list, how can i put it in with just one line of input?
Sorry for beginner question, but i am new to this and I wasn't able to find an answer. Thanks in advance.
To me it is unclear what you mean exactly, but I think you mean this:
myList = [int1,int2,int3,...]
Integers can be added into the list directly this way. I hope this helped!
Note that this goes for any type of value, not only for integers
In order to read user input of multiple numbers, you can do the following:
myList = [int(i) for i in input().split()]
If on Python2, you should use this instead:
myList = [int(i) for i in raw_input().split()]
You can use Pythons built range() function. Which will return a range of numbers, on which you would then apply the Python list() function to.
For instance if you want a list between 1 and 100 you can do.
list1 = list(range(1,101))
This which would return all integers between 1 and 100.
In your case say you wanted 5 numbers between 0-5
five_numbers = list(range(1,6))
This would return [1,2,3,4,5]

Python Logical Error in Loop while reading Dictionary [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am new to python and OOPS.I am expecting my module add_book to increment if book is already present in dictionary. Please help me .Not sure why for loop is not working as expected.
https://github.com/amitsuneja/Bookstore/commit/4aefb378171ac326aacb35f355051bc0b057d3be
You should not append to the list while you are still iterating it. Also, your code will append the new item for each item already in the list that has a different name. Instead, you should use a for/else loop. Here, the else case will only be triggered if you do not break from the loop.
for recordlist in self.mybooksinventory:
if self.name == recordlist['name']:
recordlist['quantity'] += 1
break # break from the loop
else: # for/else, not if/else !
self.mybooksinventory.append({'name':self.name,'stuclass':self.stuclass,'subject':self.subject,'quantity':1})

Read 2d array and list result in Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have following 2D array
name_list = [['Orange', '5'],['Mango','6'],['Banana','3']]
I want to get each fruit name alone with its count and print it using a python code. So how do I read above array to extract the data (inside for loop)
I need print out as
Name:Orange<br/>
Count:5<br/>
Name:Mango<br/>
Count:6<br/>
Name:Banana<br/>
Count:3<br/>
You can unpack your list like this:
for name, amount in name_list:
print("Name:{}".format(name))
print("Count:{}".format(amount))
Try this:
name_list = [['Orange', '5'],['Mango','6'],['Banana','3']]
for item in name_list:
print("Name: {}".format(item[0]))
print("Count: {}".format(item[1]))

For Loop in Python isn't working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
inputs = []
iterations = int(input())
for x in inputs:
currentInput = input()
inputs.append(currentInput)
print(x)
That code isn't working. It is supposed to make more "currentInput" variables based on "iterations". Thank you soooo, much, as this has been bugging me.
Your code isn't working because it's not right.
Your for loop is going through each element in the list inputs. But inputs is an empty list; you haven't added anything to it so the for loop won't work. You must have meant
for x in range(iterations):
currentInput=input()
inputs.append(currentInput)
print(currentInput)

Why won't this answer to project euler number 4 work properly? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
numbers = []
palindromes = []
for i in range (100,999):
for j in range(100,999):
number = i * j
test = str(number)
if test == test[::-1]:
palindromes.append(number)
print(palindromes[-1])
This code gives back palindromes. However it gives back 580085, which is far from 906609 which is correct. Is there something in the code that makes it quit early?
Your list does contain all the palindromes in that range, but not necessarily in a sorted order; the biggest one is somewhere in the middle. Use max to retrieve it.
print(max(palindromes))

Categories

Resources