Get split value from two zip list - python-3.x

i have this two lists and i need to zip them and then i need to get one of the values
for x in heapq.nlargest(5, zip(list1,list2)):
print("->: ",x)
This prints ->: (9.99, 'SFO-RE.0025')
I need to get the 9.99 in order to run it thru another function evval('9.99')
I was hopping that someone could help me get this value

heapq.nlargest returns a list, so use x[0] to get the first item in x.
https://docs.python.org/3/library/heapq.html
https://realpython.com/lessons/indexing-and-slicing/

Related

how to add value of type INT into list using python?

A loop produces interger values and I want to store them into a list.
Ex: on 1st iteration I'll get 510, 510 should be stored or added into list. on 2nd iteration I'll get 1780, 1780 should be stored in to list as a integer.
on each iteration ill get the values & it should get stored into list.
you can create an empty list like so
list_example = []
In this case, the "name" of the list is called list_example. Since it is an instance of a list, you may use methods (functions) that pertain to things you would like to do with lists. For example, the function append may be of use to you. The following code
list_example.append(510)
"appends" the number 510 to the end of the list.
>>> print(list_example)
[510]
Other list functions are available in docs, and I would spend some time on an introductory python course or book so you can be familiar with basic python.

Sum all rows when at least one row meets the criteria

I have a database as follows:
I am having issues trying to sum all the quantities by groups when the “Included” flag is Y for at least one of the groups; that is: if there’s a Y on at least one row from group 3 , sum all the rows from group 3, regardless if the rest are not included.
The output should be like this:
Thanks!
You're going to need to use an array function to solve this one. To enter and array function, type a function as you normally would then press Ctrl+Shift+Enter instead just Enter. (Apple Keyboards differ, but I don't have Apple.)
I'll explain how I put the function together, then bring it all together in the end.
First, I wanted an array of group numbers that have at least one "Y" in the Included column. That array is created by IF(B2:B14="Y",A2:A14,0). The result would be {0,0,0,0,0,0,3,...etc} in the sample case.
Next, I flag every group row that has an entry in the list of group numbers using the MATCH(A2:A14,IF(B2:B14="Y",A2:A14,0),0) function.
Since that uses an exact match, it will create an error when there is no match. ISERROR(MATCH(A2:A14,IF(B2:B14="Y",A2:A14,0),0)) gives me a true when there is not a match and a false when there is, so I negate it. NOT(ISERROR(MATCH(A2:A14,IF(B2:B14="Y",A2:A14,0),0)))
Another IF converts this to a 1 or a 0.
IF(NOT(ISERROR(MATCH(A2:A14,IF(B2:B14="Y",A2:A14,0),0))),1,0)
Finally, I bring it together with the Quantity using a SUMPRODUCT:
=SUMPRODUCT(C2:C14,IF(NOT(ISERROR(MATCH(A2:A14,IF(B2:B14="Y",A2:A14,0),0))),1,0))
Again, remember to Ctrl+Shift+Enter to make this work.
Tested in Excel 2013 and it works fine.

How do i return the value of how many times a certain integer appears in a list using a loop? How can I find the mode in the list

Write a program how_many.py
which has the following functions in it:
freq(n,l) which will be passed a list of integers l and a single integer n. It will return the frequency of which l appears, that is how many times it appears. So, freq(3,[3,2,2,1,3,4,5,4,3,4,3]) would return 4 since 3 appears 4 times. DO NOT USE COUNT -- loop through the list and do this manually.
min(l) -- calculates the smallest value of the list - again, do this manually using a loop not using a built in function.
mode(l) which returns the mode - the most frequently occurring item in the list - you can assume a single mode in the list, that is there won't be two items that appear the most times.
Yes this is my homework. No I do not want you guys doing it for me. i want to understand or have some type of help to GUIDE me to start this. I am entirely new to python and to the world of code, so it essential that I understand the concept.
Title question
=====================================================
Initialise a counter which counts how many times you find n in your list. Then you must step through each index in your array, and at each step, check that this value is the same as n
If it is, increment n by 1, if it isn't, do nothing. When you reach the end of your list, return the final result.
=====================================================
Min(l)
Initialise a variable which stores the minimum value so far called min as you are checking through the list. At each index of the list, check to see if the value at this index is bigger than min. If it is, update min's value, otherwise do nothing. Return min when you get to the end of the list.
=====================================================
Mode(l)
The mode is the most frequently found number in the array. You must make a map of keys and values (your keys being the distinct numbers in your list, and value being how many times it appears). Once you have looped through your list and found out how many times they each appear, return the largest value in your new map.
======================================================
good luck

How can i use a string to determine the location of an object within a list?

Let's say i have a list of the alphabet
myList=["a","b","c"..."z"]
Now lets say we have a variable within a loop that takes out a random letter from the list. Obviously random is imported.
while True:
ans=myList[random.randint(1,26)]
I want the user to be asked to take a guess at a letter so within the loop i add
guess=input('Take a guess at a letter from the alphabet')
The user will receive a clue on the whereabouts of the answer
print('The letter locates between x and x.')
Question. How can i determine the position of ans in myList so i can give two random values and perhaps assign them to variables, one below ans and one value over ans.
The range would always be random between these two values so ans is not always the median of the two values.
p.s. I would put the script together to give a better view of what it looks like, but unfortunately i find the formatting help very confusing, and highlighting pieces of code and pressing Ctrl+K does not work as simply as i expected.
The position is the output of the random call, right?
You can save that to a variable before calling the myList[]
index = random.randint(1,26)
ans = myList[index]
use
myList.index(ans)
for above code to work you need to have ans in myList or else it will throw an exception.
BTW this question is similar to Finding the index of an item given a list containing it in Python

Assigning and reading multidimensional arrays in Python

I'm stumped.
for a in range(0,500): #500 is a highly variable number but using it for example purposes
b = findall(r'<(.*?)>', d) # d will return a highly number variable number of matches could be anywhere from 45-10000
c.append([b])
print(c[0][1])
This returns the error because everything from 'b' goes into c[0][0]. I can understand this. The question is how do I split 'b' apart so I can put it into c so I can
print(c[0][234])
and get it give me back the 235, err element 234 of the 1, err 0, line?
This is a situation like I said above where the number of times going through 'b' will be variable, at least for right now until I get the entire file prepped I can only that 'b' in the end will be way north of 10,000 and probably closer to 100,000 by the time I have all the data collection finished. The number of elements that are stored can and will be highly variable depending on the file that they come from. They are all coming from a csv file but I'm hoping to not to deal with adding in any 'complexity' by going out and having to deal with the csv module...since I've never used it before and that will probably just lead to more questions.
I have tried something similiar to...different variables naturally so things would be appropriately matched up
d = list(zip(*(e.split(',') for e in b)))
all this has did is split on each and every letter versus on the comma.
Your error is coming from the square brackets you have in c.append([b]). The brackets create an extra list that contains the list b. So rather than a two dimensional data structure, you're ending up with three dimensions. Your indexing fails because c[0][1] is trying to get a second value from the middle list (which only ever has one item in it).
You might get what you want with c[0][0][1] instead. But you probably don't actually want that extra level in your data structure. You can avoid creating it by using: c.append(b)

Resources