What does "ranking a list" mean? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
For a homework assignment I am given this task:
Read a list of numbers and generate a new list which squares the members of the original list.
generate a third list of ints that ranks the second list of ints.
What does it mean to "rank the list"? What is this asking me to do?
I am NOT asking for a solution to the programming assignment, please do not provide one.

To create a list of integers between 0 and N, a solution is to use the range(N) function.
Then you can use the append() method which adds an item to the end of the list while getting the square of each element.
Finally use the sort() method which sorts the list ascending by default. You can also make a function to decide the sorting criteria(s)
def printSortedList():
l = list()
for i in range(1,21):
l.append(i**2)
l.sort()
print(l)
printSortedList()

Ranking means to number the items in a list from lowest to highest, or vice-versa.
So, for example, “ranking” the list [639, 810, 150, 162, 461, 853, 648] produces the list [4, 6, 1, 2, 3, 7, 5], where 1 corresponds to the lowest value (150), 2 to the next-lowest value (162).
Or maybe you're supposed to rank them the other way, giving [4, 2, 7, 6, 5, 1, 3].

Related

Random Question Sequence From Array Python [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 2 years ago.
Improve this question
I have 5 arrays with 6 questions in each one.
I need the script to pick 2 questions from each array and create an input() function. The part I can't seem to think of is how to make an output for a correct answer for the questions. I understand how a specified input would work but what about randomized.
I think you're looking for something like this:
randomNumber1=***some generated number (0 thru 6)
randomNumber2=***some generated number (0 thru 6)
array1=['what is the meaning of life', 'how far away is the sun',...]
array2=['what did is your favorite color', 'how many pennies are in 1 dollar'...]
q1=array1[randomNumber1]
q2=array2[randomNumber2]
input1=input(q1)
input2=input(q2)
#stores answers in a dictionary
answers={q1:input1, q2:input2}
I do not think the random module has the function that you want.
But it is easy to build one if you like. Python is easy.
Does this work?
import random
from typing import Iterable
def get_sub_random_list(sub_length: int, iterable: Iterable) -> list:
iterable_copy = list(iterable)
result = []
for __ in range(sub_length):
length = len(iterable_copy)
if length == 0:
raise ValueError(f"the iterable should longer than {sub_length}")
index = random.choice(range(length))
result.append(iterable_copy[index])
del iterable_copy[index]
return result
example:
>>> get_sub_random_list(1, [1, 2, 3, 4, 5, 6])
[5]
>>> get_sub_random_list(6, [1, 2, 3, 4, 5, 6])
[4, 1, 5, 2, 6, 3]
The complexity is O(n+m): n is the length of iterable, and the m is the the times of the loop.

How to remove double [] from a list? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a list like this:
my_lst = [[1,2,3,4]]
I was wondering how can I remove [] from the list to get the following list:
my_lst = [1,2,3,4]
The double [[]] is because you have a list containing a list. To get out the inner list, just index it:
my_lst = my_lst[0]
or unpack it:
[my_lst] = my_lst
Unpacking has the mild advantage that it will give you an error if the outer list doesn't not contain exactly one value (while my_lst[0] will silently discard any extra values).
There are many other options.
my_lst = my_lst[0]
You have a list with one element, which is itself a list. You want the variable to just be that first element, the list 1,2,3,4.
you have a list of lists.
my_lst = my_lst[0] #get the first list of the list
You can use itertools to achieve that without writing your explicit loop if the length of the outer list could be more than one.
In [47]: import itertools
In [49]: list(itertools.chain.from_iterable([[1, 2, 3], [1, 2]]))
Out[49]: [1, 2, 3, 1, 2]
You can reduce 1 dimension using sum.
my_lst=sum(my_lst,[])
# [1,2,3,4]

find all nested array in single array in python WITHOUT FLATTEN or any inbuilt function [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 3 years ago.
Improve this question
Hi I need a single list of nested lists in python without any inbuilt function. I did a lot of research but not able to find without flatten or ravel function().
for ex:
input = [1,2,[2,3,[4,5]],[6,7],[8,[9]],10]
output = [1,2,2,3,4,5,6,7,8,9,10]
You can use recursion to implement your own flatten function. Here's a nice tutorial explaining the concepts of recursion.
The idea here is that you loop through each element in your list and if you encounter a sublist you recursively call the same method with the current list. If it's a valid element you add it to to your output list.
Example:
def flatten(input_list, output_list=[]):
for i in input_list:
if isinstance(i, list):
flatten(i, output_list)
else:
output_list.append(i)
return output_list
input_list = [1,2,[2,3,[4,5]],[6,7],[8,[9]],10]
print(flatten(input_list))
Outputs:
[1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I found a solution on my own. Below is the solution:
c = []
def test(a):
for val in a:
if type(val) == list:
test(val)
else:
c.append(val)
return c
a=[1,2,[2,3,[4,5]],[6,7],[8,[9]],10]
print test(a)

Python: The Replacements [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 7 years ago.
Improve this question
I'm currently trying to complete Computer Science Circles online but I am stuck on part 14: Methods. Here is the question.
Using index and other list methods, write a function replace(list, X, Y) which replaces all occurrences of X in list with Y. For example, if L = [3, 1, 4, 1, 5, 9] then replace(L, 1, 7) would change the contents of L to [3, 7, 4, 7, 5, 9]. To make this exercise a challenge, you are not allowed to use [].
Note: you don't need to use return.
I would probably be able to do this if we were allowed to use square brackets.
Here is what I have so far.
def replace(L, X, Y):
while X in L:
var = L.index(X)
var = Y
return(L)
I'll give some tips since this is an exercise.
1) You already found out the index where you're supposed to replace one element with another. What other way is there to replace a value in a given index? Check all the methods of list.
2) A list comprehension also allows an elegant solution:
[...???... for value in list]
You'll need to figure out what the expression should be, and how to make the comprehension modify your original list, not just create a new one.

Top n entries of a dictionary in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi guys i have a dictionary that looks like this:
[[['Test', 0,1],['Test2', 0,4],['Test3', 0,5],['Test4', 0,2],['Test5', 0,6],...]]
How can I get the top two (for example)?
Expected result:
[['Test3', 0,5],['Test5',0,6]]
Sort by the third value:
>>> sorted(l[0], key=lambda x: x[2])[-2:]
[['Test3', 0, 5], ['Test5', 0, 6]]
test = [[['Test', 0,1],['Test2', 0,4],['Test3', 0,5],['Test4', 0,2],['Test5', 0,6]]]
result = sorted(test[0], key=lambda x: x[2])[-2:]
print(result)
Output:
[['Test3', 0, 5], ['Test5', 0, 6]]
Something like this?
(I am assuming that you actually mean ['Test', 0, 1] to be a list with 3 elements which is to be sorted by the third element rather than a two element list with the second element being a float, in which case it should be ['Test', 0.1] and you can look at #jonrsharpe's answer. I also assumed that the output order (lowest to highest) is relevant.)
That is not a dictionary, it is a list ([]) containing another list which contains lists. Each sub-sub-list contains three elements:
['Test2', 0, 4]
#^0 ^1 ^2
Note that Python doesn't allow the user of commas as decimal points; I think you want 0.4.
Assuming that, sorting the sub-list and extracting the two highest sub-sub-lists is easy:
from operator import itemgetter
output = sorted(lst[0], key=itemgetter(1), reverse=True)[:2]
If I am wrong about the comma, make the argument to itemgetter 2.

Categories

Resources