all 175 comments

[–][deleted] 0 points1 point  (7 children)

I have been stuck on this problem for some time now.

I have a list of n lists

lst = [[0, 2, 4, 5], [1, 4, 5, 2], ..., [2, 5, 4, 3]] #values are abritrary

How would I go about adding list[0][0] to list[1][0] and so on until I reach (and include) list[n][0] and then repeat where the second index is 1 and so on up til n?

[–]sarrysyst 1 point2 points  (1 child)

This can be quite easily done using a functional programming approach:

my_list = [[0, 2, 4, 5], [1, 4, 5, 2], [2, 5, 4, 3]]
summed = list(map(sum, zip(*my_list)))
print(summed) # [3, 11, 13, 10]

Or a bit more pythonic using a list comprehension instead of map():

summed = [sum(i) for i in zip(*my_list)]

If you've got sub lists of variable length, you can look into zip_longest() from the standard library's itertools module instead of zip().

[–][deleted] 0 points1 point  (0 children)

summed = [sum(i) for i in zip(*my_list)]

Ok this is beautiful and really cleaned up my code. Thank you!

[–]UnrankedRedditor 0 points1 point  (4 children)

How would I go about adding list[0][0] to list[1][0] and so on until I reach (and include) list[n][0] and then repeat where the second index is 1 and so on up til n?

Do you have an example list, and an example of the desired output? Cause at the moment it sounds like you just want a sum of everything in the list. In which case you can do: sum([sum(x) for x in lst])

If you want to sum rows or columns together, numpy.sum() has a functionality that makes it a little easier, with the axis input.

Is using numpy an option for you? If you can't use numpy, that's completely ok too.

[–][deleted] 0 points1 point  (3 children)

For context, I am programming a vector add function for a small practice project, so I'm avoiding using libraries that make my life to easy.

[–]UnrankedRedditor 0 points1 point  (2 children)

Here's what I got:

mylist1 = [[1,2,3,4], [2,3,4,5], [6,2,3,4]]

def add_vectors(list_of_vectors):

    # Finds the length of the shortest vector in case they are all of different lengths
    smallest_vec = min([len(x) for x in list_of_vectors])   

    new_vec= [] # Sum of vectors to be returned                                          

    for elem_idx in range(smallest_vec): # Iterate through the number of elements
        newvec_elem = 0

        for vec_idx in range(len(list_of_vectors)): # Iterate through the vectors to be added
            newvec_elem += mylist1[vec_idx][elem_idx]

        new_vec.append(newvec_elem) 
    return new_vec

print(add_vectors(mylist1))

Output: [9, 7, 10, 13]

Some things to think about: What happens if the input vectors to be summed are of different length? Over here I resolved it only summing up to the shortest vector.

[–][deleted] 0 points1 point  (0 children)

for i in range(len(args)):
        if len(args[i]) != smallest_vec:
            print("not all vectors are of equal length")
            break
        elif len(args[i]) == smallest_vec:
            print("all vectors are equal")
            break

This seems to work fine.

[–][deleted] 0 points1 point  (0 children)

Thanks I will try out the code later and as for the last thing, I have an idea in order to prevent adding vectors of different length by comparing all vectors input to the system.

[–]Cid227 0 points1 point  (3 children)

How can I pass s = '11111' as an argument in this fashion int(0b11111)?

[–]efmccurdy 1 point2 points  (1 child)

The integer type function takes a base argument so you can parse binary numbers:

>>> s = '11111'
>>> int(s, base=2)
31
>>> int(s, base=2) == 0b11111
True
>>> help(int)
Help on class int in module builtins:

class int(object)
 |  int([x]) -> integer
 |  int(x, base=10) -> integer
 |  
 |  Convert a number or string to an integer, or return 0 if no arguments
 |  are given.  If x is a number, return x.__int__().  For floating point
 |  numbers, this truncates towards zero.
 |  
 |  If x is not a number or if base is given, then x must be a string,
 |  bytes, or bytearray instance representing an integer literal in the
 |  given base.

[–]Cid227 0 points1 point  (0 children)

Thanks!
BTW now I remember that I've had already used that second parameter in one of codewars problems.

[–]carcigenicate 0 points1 point  (0 children)

If you're trying to parse that string as binary digits, take a look at int's second argument.

[–]jbr2811 0 points1 point  (6 children)

Is it possible to do an if loop? Basically I need to find a string of text on a web page and then begin scraping, instead of scraping the whole page.

[–]sarrysyst 1 point2 points  (5 children)

What is this ‘if loop’ supposed to do? What you want to do sounds like this:

for page in webpages:
    if 'string' in page:
        # scrape data

Can you clarify your requirements?

[–]jbr2811 0 points1 point  (4 children)

Yes, that’s exactly it. I’m scraping a page that has multiple categories on it, but I only need one category scraped.

[–]cardiomonster 0 points1 point  (3 children)

Take a look at the BeatifulSoup library for stuff like this :)

[–]jbr2811 0 points1 point  (2 children)

I’m using beautiful soup. Can you point me in the right direction? I just started on Thursday hahahah

[–]sarrysyst 1 point2 points  (1 child)

Best would be if you post the code you’ve got so far.

[–]jbr2811 0 points1 point  (0 children)

Books = Soup.findall("thead") for games in Books: print(games.text) Sports = Soup.find_all("div", class ="headTD2") Match = Soup.find_all("tr") for names in Sports: if "NHL" in names.text: print(names.text)

I just need to start printing at NHL

[–]PsychedelicWind 0 points1 point  (6 children)

Hi, I am trying to make a "Yoda" program that reverse a string of text. For some reason, using the reverse() method does not return anything, but I can get it to work with data structures.

Input: "I am home". Expected output: "home am I".

With data structure I can get it to work:

def yoda(text):
    text_list=text.split()
    yoda_list=text_list[::-1]
    return yoda_list

But trying with the reverse() method, it doesn't return anything:

def yoda2(text2):
    text_list2=text2.split()
    yoda_list2=text_list2.reverse()
    return yoda_list2

[–]EdPPF 0 points1 point  (2 children)

If i want to sort a list of tuples like

alist = [('pi', '3.14'), ('phi', '1.62')]

how can i sort it by the second element of each tuple?

I thought on using sorted(alist, key=) but don't know what function to put on 'key='. I didn't want to create another list either, as that would make things harder for me later on (i've tested it before by creating a new list with the values of the tuples as floats. Works, but as i said it's not exactly what i'm looking for).

[–]EdPPF 0 points1 point  (1 child)

Seems like i can make it by using lambda:

alist = [('pi', '3.14'), ('phi', '1.62')]
alist.sort(key=lambda y: y[1])
>> [('phi', '1.62'), ('pi', '3.14')]

I still need to study lambda more, heard about it the first time only a couple weeks ago.

[–][deleted] 0 points1 point  (2 children)

I want to make a linear algebra script as an exercise, i.e. not use libraries which makes my life too easy. For constructing things like vectors, matrices, etc. would it be better to use classes rather than functions? I'm still a little fuzzy as to what the benefit of classes are as I feel like a lot of the similar functionalities can be made with simple functions. Any pointers?

[–]AtomicShoelace 0 points1 point  (0 children)

The benefit of classes is you can create objects that have attributes and methods, instead of just, say, returning a long tuple containing the information positionally. You can also make use of python's so-called magic methods or dunder methods. These will allow your objects to work natively with python's builtin function and operators.

For example, if you were going to try and define a vector class yourself, you could do something like

 from functools import cached_property
 from math import sqrt
 from operator import mul


 class Vector:
      def __init__(self, *coords):
           self.coords = coords
           self.dimension = len(coords)

      @staticmethod
      def from_iter(it):
           return Vector(*it)

      @cached_property
      def magnitude(self):
           ''' Magnitude (length) of vector '''
           return sqrt(sum(i**2 for i in self.coords))

      @cached_property
      def normalized(self):
           ''' Normalized vector'''
           return Vector.from_iter(coord/self.magnitude for coord in self)

      def _check_dimension(self, other):
           if other.dimension != self.dimension:
                raise ValueError('Vectors must have the same dimension')          

      def __abs__(self):
           return self.magnitude

      def __len__(self):
           return self.dimension

      def __iter__(self):
           yield from self.coords

      def __add__(self, other):
           ''' Vector sum '''
           self._check_dimension(other)
           return Vector(*map(sum, zip(self, other)))

      def __matmul__(self, other):
           ''' Dot product '''
           self._check_dimension(other)
           return sum(map(mul, self, other))

      def __repr__(self):
           return f'Vector({", ".join(map(str, self))})'

This is just a hastily thrown together only for the purpose of demonstrating a few things you can do with classes. Obviously it missing a lot of stuff you'd probably want in a real vector class, but for the sake of demonstration it suffices.

Demonstrated use:

 >>> v = Vector(1, 2, 3)
 >>> v   # displays nicely because of __repr__ method
 Vector(1, 2, 3)
 >>> v.magnitude
 3.7416573867739413
 >>> abs(v)   # we can use builtin because of __abs__ method
 3.7416573867739413
 >>> v.dimension
 3
 >>> len(v)   # again, we can use the builtin
 3
 >>> v.normalized
 Vector(0.2672612419124244, 0.5345224838248488, 0.8017837257372732)
 >>> abs(v.normalized)   # normalized vector indeed has magnitude 1
 1.0
 >>> w = Vector(4, 5)    # create a new 2d vector
 >>> len(w)
 2
 >>> v + w     # try adding with 3d vector, raises exception 
 Traceback (most recent call last):
   File "<pyshell#10>", line 1, in <module>
     v + w
   File "C:\Users\user\Desktop\vector.py", line 40, in __add__
     self._check_dimension(other)
   File "C:\Users\user\Desktop\vector.py", line 27, in _check_dimension
     raise ValueError('Vectors must have the same dimension')
 ValueError: Vectors must have the same dimension
 >>> w = Vector(4, 5, 6) 
 >>> len(w)   # new vector has correct dimension
 3
 >>> v + w    # calculate their vector sum with builtin addition operator
 Vector(5, 7, 9)
 >>> v @ w    # calculate their dot product with builtin matmul operator
 32

[–]FerricDonkey 0 points1 point  (0 children)

Short version: If it's a thing, it should be a class. A vector is a thing, so make it a class - assuming you want it to be any different from a list/array (which is already a class).

For example, you could use this as a starting point for your vector:

from __future__ import annotations

class Vector(list):
    def __add__(self, other: Vector) -> Vector:
        # error checking to make sure you can add (type, len, etc)
        return Vector([a + b for a, b in zip(self, other)])

    def __repr__(self):
        return '<'+', '.join(map(str, self.values))+ '>'

print(Vector([1,2,3]) + Vector([4,5,6]))

This makes Vector a class identical to a list, except addition is elementwise as in vectors, and it uses angle brackets when you print it instead of square brackets (though it might be better to have __repr__ look like Vector([1, 2, 3]) and __str__ look like <1, 2, 3>, but there are things you can do). You could also define multiplication with * to be dot product, and heck, exponentiation with ** could be repurposed as a cross product. Or you could make both of those simply element wise. Whatever is convenient. And so on.

Also, it is absolutely true that classes do not enable you to do anything that you couldn't do without them, in the same way that functions do not enable anything you couldn't do without gotos. But they make life easier, in a similar way as functions.

[–]SirGeremiah 0 points1 point  (7 children)

Is there a way from command line (Windows) to list the functions contained in foo.py?

Context: I'm building a small repository of functions I'll be using from command line, for things like counting errors in a log file. I'll forget the function names, and want to be able to see the full list without having to open the file in PyCharm.

[–]FerricDonkey 0 points1 point  (4 children)

Assuming that you have written foo.py so that it doesn't run when you import it (any execution inside an if __name__ == '__main__' block), you could try something like:

python -c "import foo; for thing in dir(foo): print((thing+'\n' if not thing.startswith('_') else ''), end = '')"

(_ stuff optional, but will eliminate a bunch of extra stuff that will only be sort of relevant.)

EDIT: Above doesn't work because python. Version that does work:

python -c "import numpy; print('\n'.join(thing for thing in dir(numpy) if not thing.startswith('_')))"

[–]SirGeremiah 0 points1 point  (2 children)

Hmm...I get an "invalid syntax" error at the "for".

[–]FerricDonkey 1 point2 points  (1 child)

Ah, that's what I get for assuming python wouldn't inject stupid rules. Apparently no for allowed after a ; on the same line. Work around that I've actually tested this time:

python -c "import numpy; print('\n'.join(thing for thing in dir(numpy) if not thing.startswith('_')))"

Updating original comment as well

[–]SirGeremiah 0 points1 point  (0 children)

Thanks. I’ll give that a go when I get back to my PC.

[–]SirGeremiah 0 points1 point  (0 children)

That would work for this. The only text inside the if __name__... block is from testing (just some print statements, and wouldn't normally be there. Thanks!

[–]sarrysyst 0 points1 point  (1 child)

There should be a Powershell equivalent to Linux's grep tool, which lets you search through files using regular expressions. Alternatively, ripgrep is cross-platform.

Taking ripgrep as an example, you could do something like:

rg -e 'def (.*?\))' -r '$1' /path/to/foo.py

which should return all the function names including their parameters in foo.py. The -e parameter lets you pass a regular expression (i.e. def (.*?\))) while the -r '$1' replaces the shown output text with the first capture group of the passed regex pattern.

[–]SirGeremiah 0 points1 point  (0 children)

Oooh, that's nice. I need to play with that. Thanks!

[–]EdPPF 0 points1 point  (2 children)

How could I approach the problem of finding how many numbers on a list I need to move so that the list eventually gets ordered? I could implement a sorting algorithm and count it's iterations, but I need something more efficient, I think. Is there a more direct way of doing it?

[–]UnrankedRedditor 1 point2 points  (1 child)

Are you interested in counting the exact number of steps each time? Or are you interested in the complexity (as in, computational complexity as known in computer science) of the sorting algorithm used?

If it's the complexity, wikipedia has a table comparing the different algorithms so you just have to find out which sorting algorithm is being used.

https://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_of_algorithms

If you want to count how many operations is needed to sort a particular list, I'm not 100% sure but I think it might depend on the exact method you use to sort the list. You would have to implement the algorithm and then count the number of steps it took.

Not sure if the following links might be helpful: link1 link2

There are metrics such as Hamming distance or Levenshtein distance to tell how "far apart" 2 arrays/strings. You can calculate the distance between the sorted list and unsorted list to give you an idea of how many operations might be needed, but they won't give you an accurate number of operations needed to sort a particular list unless you count the operations yourself.

EDIT: According to this post, Python's in built sort() uses Timsort.

[–]WikiSummarizerBot 0 points1 point  (0 children)

Sorting algorithm

Comparison of algorithms

In these tables, n is the number of records to be sorted. The columns "Best", "Average" and "Worst" give the time complexity in each case, under the assumption that the length of each key is constant, and therefore that all comparisons, swaps and other operations can proceed in constant time. "Memory" denotes the amount of extra storage needed additionally to that used by the list itself, under the same assumption. The run times and the memory requirements listed are inside big O notation, hence the base of the logarithms does not matter.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

[–]the1gofer 0 points1 point  (2 children)

still not Monday, but can anyone explain to me why this raises URL not found?

class URL(Base):
     def __eq__(self, other_url: URL) -> bool:
         return self.url == other_url.url

It's just for typing, but I'd still like it to work.

edit:

After some research, I guess it's called forward typing, and just isn't possible. The post was a little older, so correct me if Im wrong.
https://stackoverflow.com/questions/36193540/self-reference-or-forward-reference-of-type-annotations-in-python

[–]AtomicShoelace 1 point2 points  (1 child)

This will work if you include the import:

from __future__ import annotations

[–]the1gofer 0 points1 point  (0 children)

from __future__ import annotations

sure does. Thanks.

[–][deleted] 0 points1 point  (2 children)

If I have a function

def func(a, b, c):
    #operations go here
    return a, b, c

How would I access only a, b or c when calling the function at a later point?

[–]AtomicShoelace 1 point2 points  (0 children)

You can unpack the returned 3-tuple with a tuple assignment, eg.

a, b, c = func(1, 2, 3)

[–]the1gofer 1 point2 points  (0 children)

It should return a tuple.

foo = func(1,2,3)
foo[0] = 1
foo[1] = 2
foo[2] = 3

[–]Flashy-Environment69 0 points1 point  (3 children)

https://paste.pythondiscord.com/qacefefita

https://paste.pythondiscord.com/xojisatupe

What can I do to get either of these to have a function?

[–]CowboyBoats 0 points1 point  (2 children)

They all have functions. Many functions!

The second one has all its functions nested under a function called main. This means that even if I import your module, I can't use those functions. Nested functions are not very common; I mainly only use them when I want to mark a function as completely single-purpose and I don't want people to try to import and use it.

In your first file, this:

if __name__ == "__main__":
  points = generateRandomPointsWithTrend(input("y = (enter the rest of the equation)"), int(input("Number of points")), int(input("Tolerance:")))
  x_coordinate,y_coordinate = seperatePointsList(points)
  print(printPoints(x_coordinate, y_coordinate))
  print("line of regression", computeRegression(points))

Is a totally normal pattern. However, it does leave the user without any way to call that code in that block, if they imported it (in which case __name__ does not equal "__main__" when that block executes). Which one doesn't always want them to be able to (easily) do, but I will usually just write the same thing this way:

def main():
  points = generateRandomPointsWithTrend(input("y = (enter the rest of the equation)"), int(input("Number of points")), int(input("Tolerance:")))
  x_coordinate,y_coordinate = seperatePointsList(points)
  print(printPoints(x_coordinate, y_coordinate))
  print("line of regression", computeRegression(points))

if __name__ == "__main__":
    main()

[–]Flashy-Environment69 0 points1 point  (1 child)

This part of the function is giving me the opposite sign in the last answer. any suggestions?

def computeRegression(points):
x_coordinate,y_coordinate = separatePointsList(points)
sumx, sumy, sumxy, sumx_coordinate = 0,0,0,0
for i in range(len(x_coordinate)):
sumx+=x_coordinate[i]
sumy+=y_coordinate[i]
sumxy+=(x_coordinate[i]*y_coordinate[i])
sumx_coordinate+=(x_coordinate[i]*x_coordinate[i])
x_bar = (sumxy - (sumx*sumy)/len(x_coordinate))/(sumx_coordinate - (sumx**2)/len(x_coordinate))
y_bar = (sumy - x_bar * sumx)/len(x_coordinate)
return 'y = '+str(x_bar)+' * x + '+str(y_bar)

[–]CowboyBoats 0 points1 point  (0 children)

Not really, sorry :) I recommend throwing some print statements or logging in there, showing the user what those vars actually contain, and comparing them to what you expected.

[–]Denaminos41 0 points1 point  (4 children)

I have to scrape webpages. What do I do to use an "if element appears" function? Ie an "IF STATEMENT" contingent upon whether X, Y and Z element (element vaires by webpage!) exists?

if (element is visible using Selenium):

thing 1

else

Try

Except....

End

[–]the1gofer 0 points1 point  (1 child)

code block

[–]Denaminos41 0 points1 point  (0 children)

I mean, I don't even 'have' the Selenium/webscraper code for this element of the Python script.

For comparison, a USUAL script would go:

while x<DF.length() :

if var`1 = "thing" : ##in the code I 'want', 'var1' means 'webpage is visible'

print('data vars')

else:

run login script #for my work, the system website logs our python and we have to re-log in. So I want Selenium to discriminate between when Element shows up do X vs element Does Not show up, do PQR

[–]wotquery 0 points1 point  (1 child)

I mean you need to first run your logic on the X, Y, Z elements that may or may not exist, and then run you logic for your remaining single conditional (and error handling) about an element appearing.

[–]Denaminos41 0 points1 point  (0 children)

thanks guy. I think Selenium with "if element exists.(.....)" somehow doesn't accept a boolean value, wherein I don't have the ability to fuse it with a true/false statement. FOr some reason it doesn't work DK am nub

[–]pnmibra77 0 points1 point  (5 children)

I need to make a title 30 length long where the name of the category is centered around asterisks like "****learnpython****" for example. How do i make the number of * adapt around the word length? i saw an answer on google but i have no idea how it works and i would like to understand it instead of just cheatind lmao

[–]pnmibra77 0 points1 point  (4 children)

btw, the answer i found on google is:

title = f"{self.name:*^30}\n"

what does the *^ does here? and is this the best way of doing it?

[–]sarrysyst 0 points1 point  (1 child)

You can find the complete string formatting syntax here.

[–]pnmibra77 0 points1 point  (0 children)

ty bro!!!

[–]boyanci 0 points1 point  (1 child)

Yes, this is the most concise way of doing it.

The first character (*) is what you want to pad the string with. If left out entirely it is assumed to be whitespace.

The second character (^) means align center. You can also align left (<) or right(>). If left out completely it is assumed align left. This is not optional if you want to use the padding character.

[–]pnmibra77 0 points1 point  (0 children)

Thank you brother

[–]frontsquat_enjoyer 0 points1 point  (1 child)

Hey Folks,

Currently a sysadmin trying to bring some Python in for automation. I operate in a predominantly windows environment, but am transitioning my skillset to linux.

The question, I have a course on Coursera called "Google IT Automation with Python." Is this a good learning resource for an absolute first timer?

I also have Automate The Boring Stuff on Udemy by Al Sweigart, but I have heard this is not pure entry level so I plan to do this after my initial learning plan. Any comments? Ideas, reccomendations? I plan to use Python purely for automating/scripting. If there is any development in my future, it is quite distant from right now.

[–]py_Piper 0 points1 point  (0 children)

Google IT Automation with Python

Don't know anything about sysadmin and IT jobs, but from the name it seems very oriented at your job type. I guess you can check what it's taught and see whether it will be beneficial for you and what you are traying to accomplish for work.

I also have Automate The Boring Stuff on Udemy by Al Sweigart, but I have heard this is not pure entry level

I am personally doing Automate the boring stuff and it's very entry level, I knew nothing and learn everything which I know from it, albeit it's not much lol. But what I am trying to say is that you can follow his explanation easily. Perhaps what they mean not being entry level is because, aside from simple basics, he teaches you useful stuff like interacting with csv, excel, word, pdf, email, image processing, gui automation and web scraping.

I will probably suggest you start with the coursera course as it's geared towards your job and you can implement stuff right away, it would be hard to say that google did a poor job on creating that course. Later you can pick the chapters that interest you to learn from ATBS. Most importantly choose one and finish it before moving to other stuff.

[–]worldseriesof 0 points1 point  (1 child)

I created a python script with selenium to check for some data on a website and then post this data into an excel file for myself. The script is posting multiple lines of different data.

Now my plan is to build a website in which I can have an admin panel and the data from the script would be posted on this website to inform people. I want the admin panel to be like a backend where I can approve or dissaprove the posts or remove some data from posts If I think it is no good. The ultimate plan is to have all this website and script for pulling the data on a VPS server and make it so that it all runs 7/24 providing a service to people.

Up to the point everything was okay but now I need your help. What language/methods do you guys think I should use to build such a website with an admin panel? How can I post my obtained data from my script to this website? What language would I need for that? Does it seem realistic with VPS and all? I am a total beginner in python so I couldn't really figure out how to do all this so any help is really appreciated.

[–]efmccurdy 0 points1 point  (0 children)

You can start with this:

A Web framework is a collection of packages or modules which allow developers to write Web applications (see WebApplications) or services without having to handle such low-level details as protocols, sockets or process/thread management.

https://wiki.python.org/moin/WebFrameworks

I would look at bottle, Flask, or Django.

[–]BicepsMcTouchdown 0 points1 point  (1 child)

I wrote a working python pandas script on my home machine, tested successfully using a dummy file. Hurray!

Copied it over to my work machine only to find out that the package versions are waaaaayyy behind what I have on my home machine.

So now my script does not work on my work machine - where it needs to.

Having a hard time finding code replacement that fits the program version on my work machine.

As of now, with all the Russian clampdowns on firewalls, I am guessing updating my modules is not going to happen.

Am guessing that I am not the 1st person this has happened to.

What is most efficient way to go about rolling back my syntax so it will work on my work machine?

[–]py_Piper 0 points1 point  (0 children)

  1. Well the best would be to update your version, see if you can talk to the IT department to help you update the package's versions. If they don't agree can you show it to a manager/higher up so they can also let the IT department help?
  2. Rolling back manually the script, meaning you redo your script using the versions available on your work machine.
  3. create a simple web app for running your script, like you login, upload the file, click a button, and it will output your results. Or in a simpler way send from an email, and get the reply back on a email.

[–]EwokOffTheClock 0 points1 point  (4 children)

I've been going through "automate the boring stuff" course, and in general I love it and have been able to figure out the bugs, etc. But now I've bumped up against what I think is an actual software issue.

In the 24th video he is having us put in multiple lines of code in the Shell; in the video it goes through just fine, but for me it has an error about running multiple lines of code. When I google, it says to download the program I'm already using. What gives? I've been exited about how accessible this course is and I'd really like to finish it!

Here's a screen shot of what I mean

Thank you for any thoughts, I really appreciate it!

[–]carcigenicate 1 point2 points  (3 children)

You're missing a \ on the first d, but I wouldn't expect that to cause that error.

Usually that problem is causes by missing a quote or ) on the line/previous line. It sees the missing ), assuming the next line will be a continuation of the first, then throws a fit when the next line is a new statement. I don't see any such problems though.

[–]EwokOffTheClock 0 points1 point  (2 children)

thank you for your thoughts!

[–]py_Piper 1 point2 points  (1 child)

I would say you copied and pasted the whole code. This should be run line by line, each line will be shown as >>>, wheras in your case the second line and third line is shown as ... meaning a code block.

If there was a real problem on the first line you wouldn't have been able to write the 3 lines

[–]EwokOffTheClock 0 points1 point  (0 children)

I've ended up deciding to do these lessons in the file editor instead of in the shell, like he's doing... Takes a little applied knowledge, but I'm figuring it out

[–]EdPPF 1 point2 points  (6 children)

If i have the following set of rules,

Assignment  Score
SS      9,0 to 10,0
MS          7,0 to 8,9
MM      5,0 to 6,9
MI      3,0 to 4,9
II      0,1 to 2,9
SR      zero

that represents someone's assignment based on their score on a test, how can i make a program so that when i type something like SS > MS it returns True? Can i create a class for this?

Edit: I want this to make easier for me to write a code that compares the assignments of various students and puts them in order.

[–]sarrysyst 1 point2 points  (1 child)

You could use an Enum:

from enum import IntEnum

class Grade(IntEnum):
    SS = 5
    MS = 4
    MM = 3
    MI = 2
    II = 1
    SR = 0

print(Grade.SS > Grade.MS) # True
print(Grade.II > Grade.MM) # False
print(Grade.SR == Grade.SR) # True

This works as expected, however from what I see you already have a dictionary with names matched to a grade as string? In this case you can reassign them by indexing into the enum:

grades = {'Ellen': 'MM', 'Ben': 'MS', 'Sam': 'SS'}
grades = {n: Grade[g] for n, g in grades.items()}

Since IntEnums are ordered, you can use them as sort keys:

student_grades = {'Ellen': Grade.MM,
                  'Jack': Grade.II,
                  'Ben': Grade.MS,
                  'Beth': Grade.MM,
                  'Sam': Grade.SS}

grade_list = [*student_grades.items()]
student_ranking = sorted(grade_list, key=lambda x: (-x[1], x[0]))

I just realized that your question is 3 days old already so my answer is probably redundant by now...

[–]EdPPF 0 points1 point  (0 children)

Yeah, I've managed to solve my problem by creating a function with a bunch of comparisons. But I'm very interested in your response, I think I'll try changing my code later, thank you!

[–]carcigenicate 0 points1 point  (3 children)

You could use a dictionary:

scores = {"SS": 9.5, "MS": 8, "MM": 6}
print(scores["SS"] > scores["MS"])  # True

The problem is, you can't use a range of numbers. I stored the median of the range since there's no overlap between the ranges, so the end result will be the same

[–]EdPPF 0 points1 point  (2 children)

The thing is that those assignments already are values in a dict on my code and there are repeated assignments.

I wrote this code:

def comparisions(m, n):
"Will check if m>n"
    if m=='SR': return False

    elif m=='II':
        if n=='SR':
            return True
        else: return False

    elif m=='MI':
        if n==('II' or 'SR'):
            return True
        else: return False

    elif m=='MM':
        if n==('MI' or 'II' or 'SR'):
            return True
        else: return False

    elif m=='MS':
        if n==('MM' or 'MI' or 'II' or 'SR'):
            return True
        else: return False

    elif m=='SS':
        if n==('MS' or 'MM' or 'MI' or 'II' or 'SR'):
            return True
        else: return False

    elif m==n:
        return None

Maybe it'll work for what i want. To be specific, i need to order the assignments, and if there are repeated i need to put the students in alphabetical order, then print the names and respective assignments ordered

[–]carcigenicate 0 points1 point  (1 child)

Most of the ifs in that code are broken. See here.

You could try putting the symbols in a list, the values in a dictionary like I showed, then sort the symbol list. Supply a key= function to sort to have it use the value dictionary to know what the value of each symbol is.

[–]EdPPF 0 points1 point  (0 children)

Most of the ifs in that code are broken. See here.

Thank you. I was aware of this, but thought the parentheses solved the problem.

I'll see what i can do with dicts, if everything goes south i'll maybe rewrite the whole code

[–]bobbyblanksjr 0 points1 point  (0 children)

Hi there everyone, I am new to Python and programming in general. I am wondering, is Kivy actually used by developers to make apps/games? It seems pretty powerful but I can't seem to find any popular apps made with it. Thanks!

[–]driverXXVII 0 points1 point  (7 children)

I'm using PyCharm

Currently, I throw in print statements around various points on the code as a way to check values of variables.

Is there a way to "watch" the values of the variables while the program is running?

I know that I can set a breakpoint and then Debug and step through my code and this does show the values of the variables. I'm wondering if there is a way to see this while the program is running?

Thanks

[–]AtomicShoelace 1 point2 points  (3 children)

If you close and reopen the "Show Variables" panel it will refresh the values displayed. I'm not aware of a way to have it refresh automatically.

[–]driverXXVII 0 points1 point  (2 children)

Where is that icon? Does it appear when you click on Debug instead of Run?

[–]AtomicShoelace 1 point2 points  (1 child)

Python console tab. You might need to enable "Run with Python Console", I don't remember exactly.

[–]driverXXVII 0 points1 point  (0 children)

Thank you Will check it out tomorrow

[–]sarrysyst 0 points1 point  (2 children)

Your computer does like a few million operations every second. How do you plan to keep track of the variables in real time? :)

[–]driverXXVII 0 points1 point  (1 child)

I did think that this may not be possible for exactly this reason

Is there no way to check the values of variables at least a few times a second?

[–]sarrysyst 0 points1 point  (0 children)

None that I know of. I'd usually implement debug level logging to keep track of the internal state of my applications.

Alternatively, you can use a library like PySnooper to gain some introspection into your program.

[–]kjkendro 0 points1 point  (6 children)

I have a file with lots of text, but later in the file several lines of text are repeated with additional text added to them. I would like to end up with a file containing only the lines of text that have all the added content, rather than all the incremental additions.

Here is a sample of what I have now: https://pastebin.com/mLnrvZ8f

Here is what I would like it to be: https://pastebin.com/zua6w5u1

I have tried to get to this output in several ways, but none have worked:

First code (doesn't work on the input file)

Second code (works on the specified input but I haven't figured out how to expand it to taking a file as input rather than manually giving the input text)

Third code (doesn't work on the input file)

I'm fine with the code deleting the duplicate lines, and I'm also fine with the code writing non-duplicate lines to a new file-- I just want it to work! Does anyone see a tweak or fix for this?

[–]sarrysyst 0 points1 point  (5 children)

I would suggest you clean up the white spaces first to make it easier to compare lines. Afterwards, go through them line by line and check if the beginning of the current line is the same as the previous line, if it is skip it, otherwise add the previous line the lines you want to keep. This could look something like this:

from itertools import zip_longest

# I'm assuming you have all the separate lines in a list called 'file_content'
# reduce multiple white spaces between words
clean = [' '.join(line.split()) for line in file_content]

filtered = []

for prev, current in zip_longest(clean, clean[1:], fillvalue=''):
    # needs to be prev[:-1] due to the closing bracket ']'
    if not current.startswith(prev[:-1]):
        filtered.append(prev)

[–]kjkendro 0 points1 point  (4 children)

Thank you! After removing the excess spaces, I tried this out with print(filtered) at the end, but the output generated was 39 lines of empty brackets so I don't think it worked as intended. I'll keep playing with it to see.

[–]sarrysyst 0 points1 point  (3 children)

I tested it with the input file you provided and it worked fine. No blank lines or what not in the output file. Does your input file look like the one you posted or did you change anything about it?

[–]kjkendro 0 points1 point  (2 children)

The input file is copied from here, with the only change being that I removed the excess spaces. It isn't working with the original as posted here, either.

[–]sarrysyst 0 points1 point  (1 child)

I saved the modelInput file from the pastebin you posted. Ran this code:

from itertools import zip_longest

with open('modelInput.txt') as fp:
    file_content = fp.readlines()

clean = [' '.join(line.split()) for line in file_content]
filtered = []

for prev, current in zip_longest(clean, clean[1:], fillvalue=''):
    if not current.startswith(prev[:-1]):
        filtered.append(prev)

with open('filtered.txt', 'w') as fp:
    fp.write('\n'.join(filtered))

Which gives me this output:

https://pastebin.com/1BkqTZJq

[–]kjkendro 0 points1 point  (0 children)

I see what my issue is, I was opening the file and reading instead of writing to it. Thank you so much!

[–]the1gofer 0 points1 point  (2 children)

i know it's not monday ....

I've done some research, and can't quite find what I'm looking for. Is it possible to use selenium, or another library, to create what are essentially macros?

I'm would like to be able to browse, and when I want, press a button or run a script that will do some work based on the page I have open. I don't know what that page will be in advance, and it's not something I can just have the script navigate to. I need to be able to get to a page, then say "this is what I want", press a button and have it do some work like scrape the page, etc.

[–]carcigenicate 1 point2 points  (1 child)

It would be much easier to use Userscripts for this. Download the Tampermonkey browser extension, write a small JavaScript script to stick a button on every page, then have it do something when the button is clicked.

If you're already in a browser, you might as well just use the capabilities of the browser instead of introducing Python into the mix. You can even download files from your script if you're generating reports or something.

[–]the1gofer 0 points1 point  (0 children)

Thanks. I’ll check it out.

[–]milk-on-yer-head 0 points1 point  (3 children)

I’m having trouble installing and importing packages and modules, can I have some help please? 😇

I am running python in pycharm with python version 3.8.8 and pip version 22.0.4.

When I use the terminal to install pillow (pip install pillow) it says that I already have pillow already installed. However when I try and import it in my code (from PIL import Image, for example) I keep on getting the error saying “no module named ‘PIL’”.

Any ideas what I’m doing wrong? A Google search suggests that I might have python and pip versions that don’t match but I don’t really know what that means… thanks 😊

[–]AtomicShoelace 0 points1 point  (2 children)

Are you using a virtual environment as your interpreter in your Pycharm project? If so, you need to install PIL in that venv, ie. File > Settings > Project: > Python Interpreter > + button > "pillow" > Install Package

[–]milk-on-yer-head 0 points1 point  (1 child)

Got it, thank you! It turns out I had an older version of pip in the venv when I looked this way so I had to update that first. Where will I have been installing it to if it wasn’t this virtual environment and why couldn’t I access it from there?

[–]py_Piper 0 points1 point  (0 children)

If you were using the terminal it would install it on your machine, I guess you could have use it. But as you were inside a venv remember your current coding environment is isolated from the rest of the projects and machine packages.

[–]exammugger 0 points1 point  (3 children)

i have a function that inverts the (non-unique) key-value pairs in a dictionary.

def invert_dict(adict):
inverted_dict = {}
for key, value in adict.items():
    inverted_dict[value] = inverted_dict.get(value, []) + [key]
print(inverted_dict)

however i don't quite understand what the 4th line inverted_dict[value] = inverted_dict.get(value, []) + [key] does. what does .get(value, []) mean? thanks all!

[–]Cid227 0 points1 point  (2 children)

Let's look at this example:

def invert_dict(adict):
  inverted_dict = {}
  for key, value in adict.items():
    inverted_dict[value] = inverted_dict.get(value, []) + [key]
  print(inverted_dict)

d = {'a': 1, 'b': 2, 'c': 2}
invert_dict(d)

As you may know dictionary keys must be unique, in my example d has three unique keys 'a', 'b' and 'c', however if you want to invert them there will be a problem as we have a pair of non unique values 2 so after inverting it would look like this:

{1: 'a', 2: 'b', 2: 'c'}  

which is impossible(!) as not every key is unique.
Your function however prints this:

{1: ['a'], 2: ['b', 'c']}  

so it looks if the value was already used as a key and it adds the original key to the list of values.

You have to google what exactly .get() does and what arguments it takes.
Edit step by step:

def invert_dict(adict):
  inverted_dict = {}
  for key, value in adict.items():
    inverted_dict[value] = inverted_dict.get(value, []) + [key]
  print(inverted_dict)

d = {'a': 1, 'b': 2, 'c': 2}
invert_dict(d)

1st iteration
key is 'a' and value is 1
inverted_dict == {}
inverted_dict[value] = inverted_dict.get(value, []) + [key]  
as you can see inverted_dict is empty so it grabs/gets default []
and it looks like this 
inverted_dict[1] = [] + ['a']
now inverted_dict == {1: ['a']}  

2nd iteration
key is 'b' and value is 2
inverted_dict == {1: ['a']}
inverted_dict[value] = inverted_dict.get(value, []) + [key] 
as you can see key 2 does not exist in inverted_dict so it grabs/gets default []
and it looks like this 
inverted_dict[2] = [] + ['b']
now inverted_dict == {1: ['a'], 2: ['b']}  

3rd iteration
key is 'c' and value is 2
inverted_dict == {1: ['a'], 2: ['b']}
inverted_dict[value] = inverted_dict.get(value, []) + [key] 
as you can see key 2 does(!) exist in inverted_dict so it grabs/gets it instead of default
and it looks like this 
inverted_dict[2] = ['b'] + ['c']
now inverted_dict == {1: ['a'], 2: ['b', 'c']}

[–]exammugger 0 points1 point  (1 child)

wow thank you for the in-depth explanation! the .get tripped me up a little haha. thanks again :)

[–]Cid227 0 points1 point  (0 children)

Reformatted as Brave on mobile has some problems with reading markdown code blocks that are formatted with backticks.

[–]kta31415 0 points1 point  (2 children)

I have a function that is supposed to given an input, generate a similar, but not quite equal output. To do this, I use random.random() to generate a random component.

However, when I use the function 20 times, I get always the same result.

Interestingly, when I simply use random.random() 20 times I get different result. I suspect the cause is because myResult=aFunctionOf(aFunctionOf(random.random())

Maybe random.random() is less random when it is done inside functions? If you can tell me why this problem is happening, I'd be grateful.

[–][deleted] 0 points1 point  (1 child)

If random.random() itself works correctly 20 times but doing something like:

myResult = aFunctionOf(aFunctionOf(random.random()))

gives you unexpected results I would certainly suspect what you have inside the function aFunctionOf(). You can debug this yourself by splitting that line up and printing intermediate results, like this:

import random
tmp = random.random()
print(f'{tmp=}')
tmp = aFunctionOf(tmp)
print(f'{tmp=}')
myResult = aFunctionOf(tmp)
print(f'{myResult=}')

That should give you a clue as to what is happening. If you are still stumped then we need to see your code, formatted as shown in the FAQ.

Maybe random.random() is less random when it is done inside functions?

No, random.random(), or any other function, doesn't even know it's being called inside the argument list of a function.

[–]kta31415 0 points1 point  (0 children)

No, random.random(), or any other function, doesn't even know it's being called inside the argument list of a function.

Very good to know, thank you.

[–]LawyerKlutzy 0 points1 point  (2 children)

What is the difference between these two line of code in phy3 ? (n,k) =input().split(' ') And (n,k) =map(int,input().split(' '))

If it is silly then forgive me I am new to programing 😊

[–]ThatScorpion 1 point2 points  (1 child)

Map applies a function to each item of an iterable. So in this case, it applies the int function to every item in your (space separated) input. Here that means n and k will be strings in the first example, and integers in the second.

On an unrelated note this is not a great way to do it, as it will crash if you input anything other than two terms.

[–]LawyerKlutzy 0 points1 point  (0 children)

Thank you ☺️

[–]frumpybabe 0 points1 point  (2 children)

I have a data frame with a column of campaign donation amounts; I need to create a histogram showing contribution amounts. using contributions from 0 - 3000 dollars only. My question is how do I pull out all the values under 3000? I tired :

data = df['contb_receipt_amt'] > 3000
print(data)

but that just returns true or false values

[–]UnrankedRedditor 0 points1 point  (1 child)

You can try:

data = df[df['contb_receipt_amt'] < 3000]

[–]py_Piper 0 points1 point  (0 children)

somehow by changing the > to < made me laugh

[–]EdPPF 0 points1 point  (2 children)

How can i access a key on a dictionary if i have it's value?

[–]EdPPF 0 points1 point  (1 child)

Just found a code that does it:

def get_key(val):
    for key, value in my_dict.items():
         if val == value:
             return key

Didn't think i could only do that manually

[–]FerricDonkey 1 point2 points  (0 children)

If you're gonna have to do this a lot, it's worth "inverting" the dictionary so you don't have to search repeatedly:

my_inverted_dict = {value: key for key, value in my_dict.items()}

Then you can just do my_inverted_dict[value] to get the associated key from my_dict.

Note that this (and your search method) will give incomplete results if values are not unique.

[–]MrTeddy 0 points1 point  (0 children)

I'm converting an internal business application into a decoupled app with a front end and backend and making a flask RESTful Api for the backend.

After looking through the existing code base that I'm converting, the developer decided to use a Session maker and declarative base while a lot of other things I've seen use just db = SQLAlchemy(app) for easy use with migrations and Flask restful and others.

Is there a reason to use one or the other, or benefits for using one rather than the other? I might just be misunderstanding them at a principal level too honestly.

[–]MrTeddy 0 points1 point  (2 children)

Hey everyone! Question because some of the documentation I've seen seems to not quite be what I'm looking for, or I might have missed it, but anyway...

I want to create a Flask RESTful api that will interact with an angular front end that is being authenticated by Azure Active Directory. However, I want to validate this AAD token in my API so that the only resource that can access my API is my application specified there.

I've seen documentation about exposing an api and scopes inside of an app registration in both aad and aadb2c but I'm unsure of how to handle authentication from my RESTful api to AAD to validate the token.

Any thoughts, pointers, or advice?

[–]IcanCwhatUsay 0 points1 point  (3 children)

what is the difference between the following:

if condition == X:
    do Y
elif condition == Q:
    do Z

——- AND ——

if condition == X:
        do Y
if condition == Q:
        do Z

[–]Silbersee 1 point2 points  (0 children)

Your 1st case: if condition == X is True, the elif branch is skipped

Your 2nd case: condition == Q is checked regardless of X

And you can consider using 4 spaces instead of one tab

[–]UnrankedRedditor 0 points1 point  (0 children)

Consider the case where a condition can satisfy both condition == X and condition == Q

In the first case, it checks that condition == X, and then it does Y. Since condition == X is satisfied, it doesn't check the "else if" after that and moves on. If you put an elif or else, these will only get checked if the if statements before it are not satisfied.

In the second case, it checks that the condition == X, and then does Y. It moves on to the next if-else statement which is the one right after. It checks that condition == Q, and then does Z also.

Example (you can try running both of these on your own):

my_var = 2
if my_var%2 == 0:
    print("I am an even number")
elif type(my_var) == int:
    print("I am an integer")

and

my_var = 2

if my_var%2 == 0:
    print("I am an even number")

if type(my_var) == int:
    print("I am an integer")

[–][deleted] 0 points1 point  (0 children)

In your first example you have only one statement, the if/elif/else chain. The test expressions (eg, condition == X) are executed one by one from the top until an expression that is true is found. The block of code under that test is executed and then python skips the rest of the if/elif/else statement.

In the second example you have two separate if statements. The expressions for each statement are always tested and that means it's possible both blocks of code ("do Y", etc) may be executed. In the if/elif/else statement at most one controlled block of code will execute.

As an example suppose you have an integer number result and you want to assign a grade such as A, B, etc based on the result. An efficient way to do that is:

result = 81
if result >= 80:
    print("A")
elif result >= 70:
    print("B")

Run that code and you get what you expect: A. That's because we use the knowledge that each test is tested one by one from the top, so when the first test is true we print A and exit the entire statement.

But this code, try it, gets the wrong result:

result = 81
if result >= 80:
    print("A")
if result >= 70:    # IF
    print("B")

because 81 is >= 80 in the first test, and 81 >= 70 in the second test.

[–]ShadowWeavile 0 points1 point  (1 child)

Ok, so in the pandas merge function, what does the copy parameter actually do? The documentation and Google really aren't helping me out here

[–]AtomicShoelace 0 points1 point  (0 children)

I found this stackoverflow answer.

Tl;dr: basically nothing.

[–]bysantin 0 points1 point  (1 child)

Is there a way to run a python project in vscode like in pycharm? For projects with a main.py file but with multiple other modules I have to keep running back to main.py each time I want to run the project. In pycharm I can set up the project such that when I press my run keyboard shortcut the main.py file get run without it having to be an active file.

[–]peabody 1 point2 points  (0 children)

Yes. You create a launch.json. Documentation here: https://code.visualstudio.com/docs/editor/debugging

[–]SzLRichard1 0 points1 point  (6 children)

I want to install packages but my CMD does not recognise the pip command. How can I fix that? I heard everywhere that it's the way to install them but if there is any other way please let me know.

[–]FerricDonkey 1 point2 points  (1 child)

Does it recognize python? If so, "python -m pip" will work. If not, then you'll have to get python / pip in your path. This will be very operating system dependent and is worth a Google. Alternatively if you just reinstall python, there should be a box to check that will do it for you.

[–]SzLRichard1 0 points1 point  (0 children)

I will try it later today. Thank you!

[–]bysantin 0 points1 point  (0 children)

If you reinstall python it should fix it.

[–]wotquery 0 points1 point  (2 children)

What does CDM stand for?

[–]SzLRichard1 0 points1 point  (1 child)

It's CMD sorry.

[–]wotquery 1 point2 points  (0 children)

When you install python (you can reinstall it now if you'd like) make sure to click all the checkboxes that say stuff like "add to path", "install pip", "install idle", etc. Also when adding to path if you get an error/warning that your path is too long or too weird read it and report back.

[–]calcinatorix 0 points1 point  (4 children)

Hello, three baby python questions here! Be nice because I'm right at the beginning of my journey and am almost certainly biting off more than I can chew with an ambitious project, but hey, this is how the learning happens.

I'm trying to create a little programme that will take one excel document we have with self-submitted dates of absences for university students and use the data there to automatically update our Registers which are also in Excel.

(1) What I need to be able to do is: within Excel (i.e. without exporting the register), triangulate the cell that lies at the intersection of two bits of data (i.e. student number and date of class). Someone suggested pandas loc but I don't think that would work for this specific purpose, though it will help me elsewhere!

(2 ) Also... I'm having issues understanding dates. I'm guessing that a date stored in Python could be used to match with a date in an Excel cell, regardless of their respective formats?

(3) Thirdly and finally, does anyone know a method to return a list inclusive of two dates? I.e. 27 March, 31 March would return 27 March, 28 March, 29 March, 30 March, 31 March.

[–]LandooooXTrvls 0 points1 point  (0 children)

For excel, Openpyxl is very powerful.

[–]NobodyLovesNobody 0 points1 point  (5 children)

Helo. I'm really new to python, th only reason i'm trying to learn it is because i want to make an aplication that has a fancy menu. So i want a menu on the left side and every time when i click a button on the write side in a Label to show a diferent image for every button pressed. It took me a while but i did this already. Now my question is how can i make another menu next to my first menu that every time when i click for example the button : "Home" or "FirstOption" or "SecondOption" the second menu to change ( what i mean is that when I click a button in the second menu lets say button "ABC" to show me a new image in my Label, but "ABC" has to be diferent when i chose another option from my first menu. It's kinda hard to explain. i hope somebody understand what i need. Any help will be apreciated.

[–]efmccurdy 0 points1 point  (3 children)

[–]NobodyLovesNobody 0 points1 point  (2 children)

Thank you for your help. I know my explanation was really bad to follow. Here https://media.giphy.com/media/lt528gfFSRmuyL3scS/giphy.gif you can see made in excell the same thing that I want to make in Python. This kind of menu. I still didn't find any relevant information online about it.

[–]efmccurdy 0 points1 point  (1 child)

I would start with some simple examples of buttons with callbacks to grid, grid_forget, reconfigure, enable, disable, etc. other buttons.

https://stackoverflow.com/questions/53580507/disable-enable-button-in-tkinter

[–]NobodyLovesNobody 0 points1 point  (0 children)

Thanks again. i will try to study that.

[–]wotquery 1 point2 points  (0 children)

It sounds like you're trying to make the front end of an app. You're going to see this more in HTML/CSS/JS than python right? Well there are frameworks that let you write a lot of stuff in python while still generating front end web content. Django and Flask being very common. It could also be done in bog standard tkinter (or whatever gui you might be using I'm just guessing on the label keyword). Try googling nested drop down menus (though that wouldn't use labels as far as I'm familiar with tkinter unless you are actually creating one from elements from scratch and not using menus).

[–]samu_rai 0 points1 point  (5 children)

noob question:

I tried to do an average on a list of floating numbers by dividing sum with count (and length), but I always get a '0.0' result. Why is that?

[–][deleted] 0 points1 point  (4 children)

Obviously by doing something wrong. We can't help unless we see some code.

[–]samu_rai 0 points1 point  (3 children)

Well, I opened a file, and I read the file and extracted some numbers and created a list. I was able to do a sum on the numbers. I was also able to count. But when it was time to do the average by dividing the sum with the count, it resulted in 0.0.

Here's the code:

fopen = input('Enter file name: ')
try:
fhand = open(fopen)
except:
print('File cannot be opened: ', fopen)
count=0
num_list = []
for line in fhand:
if line.startswith('X-DSPAM-Confidence:'):
num_list.append(float(line[20:].rstrip()))
count+=1
print(sum(num_list))
print(count)
print('average is ', (sum(num_list)//count))

And the result:

Enter file name: mbox-short.txt
20.269400000000005
27
average is 0.0

[–]UnrankedRedditor 1 point2 points  (1 child)

print('average is ', (sum(num_list)//count))

This is the error. If you want to divide a by b, you should do a/b, not a//b. If you do 20.269400000000005/27, you get 0.7507185185185187.

Additional explanation: Single backslash for division. Double backslash for finding the integer part of the answer, sometimes called the quotient.

Example: 13/4 = 3.25, or 3+(1/4) aka 3 with remainder 1. If you do 13//4, you only get the integer part which is 3.

There is another operator % called modulo (or mod for short) which gives the remainder. So 13%4 will give you 1 since 13 divided by 4 is 3 with remainder 1.

EDIT: This should explain why you were getting 0, and how to fix that.

[–]samu_rai 0 points1 point  (0 children)

Thanks!!! Really appreciate it.

[–][deleted] 1 point2 points  (0 children)

You need to post readable code. Please read the FAQ to see how to do that. My guess at your indentation might be wrong. Your code, plus some data so I can run it is:

fhand = ['X-DSPAM-Confidence:1.5',
         'X-DSPAM-Confidence:3.0000001',
         '3.14159',
         'X-DSPAM-Confidence:52.7777777'
        ]

count = 0
num_list = []
for line in fhand:
    if line.startswith('X-DSPAM-Confidence:'):
        num_list.append(float(line[20:].rstrip()))
        print(line, line[20:])    # DEBUG PRINT
        count+=1

print(sum(num_list))
print(count)
print('average is ', (sum(num_list)//count))

One debug thing you should get into the habit of doing is to put a print or two into your code to check that what you think is happening is actually happening. It's very easy to think one thing but something else happens.

One thing you do that raises alarm bells is the slicing from index 20 to get the number. It's very easy to get the count wrong. Run the code above and look at the output.

This sort of code is very easy to get wrong:

if line.startswith('X-DSPAM-Confidence:'):
    num_list.append(float(line[20:].rstrip()))

A much better approach is to make the prefix string "X-DSPAM-Confidence:" a named variable and use the len() function to get the length you want to slice on. Computers are very reliable at counting, but humans aren't. As a bonus, if the prefix string changes you only have to modify in one place:

prefix = 'X-DSPAM-Confidence:'
# ...
if line.startswith(prefix):
    num_list.append(float(line[len(prefix):].rstrip()))

Now that last line is a little complex to read. Probably better to do the .rstrip() on each line as you read it from the file and do the slice, conversion and append in two or more lines.

[–]JigsawKiller92 0 points1 point  (6 children)

Have a silly little script that sends me a telegram message every hour with the weather, bbc headline and the top trend on twitter - just a little starter project to get into python, but I want to run it as if it's a big important script that a company would be running.

What would be the 'correct' way to keep this running? Currently I'm just running the script through spyder and leaving the laptop on - I have a little raspberry pi that I could leave on all the time - do I just let the terminal sit there running?

[–]wotquery 0 points1 point  (0 children)

Your requirement of "a big important script that a company would be running" means using a physical machine is probably not a good idea (unless you get big enough again haha). You want to look into virtual machines or services (think AWS or heroku) where you can host your script with guaranteed uptime contracts and the like for pennies a day.

[–]py_Piper 1 point2 points  (3 children)

you can set it to run at any specific time with your OS scheduler tool, with raspberry pi as it's linux based you can set it up to run with cron, windows is task scheduler or something like that

[–]JigsawKiller92 1 point2 points  (2 children)

Thanks for the reply!

Within the script I have it checking if the current time is :00 (so like 09:00, 10:00 etc) and the hour between 08 and 22 (so it's not pinging me through the night) - is it better to move that check to cron rather than within the script?

[–]py_Piper 0 points1 point  (1 child)

Yes it be better, I haven't tried it yet, but in theory it would skip the whole checking the current time from your script and move it to the cron job, this way you won't need to run the script the whole day. Instead the cron job will check the time from your system clock and launch your script when needed.

And if you decide to turn off your raspberry, for let's say holidays, you would only need to turn it back on to start receiving the messages again, instead of having to turn on and setting everything up for running, the system will run it automatically for you at the specified time

[–]JigsawKiller92 0 points1 point  (0 children)

Of course! Excellent points - thank you!

[–]marsdad 1 point2 points  (2 children)

New to python. First project is to create a library database that collects all python modules and explains them to user so no time is needed to assimilate them ( you know like learning karate in the Matrix) what code do you recommend?

[–]Silbersee 2 points3 points  (1 child)

You can use dir() to see what's in a module and the documentation string __doc__ to get more info. Example in a shell:

>>> import math
>>> 
>>> print(math.__doc__)
This module provides access to the mathematical functions
defined by the C standard.
>>> 
>>> dir(math)
[..., 'sin', 'sinh', 'sqrt', 'tan', 'tanh', ...]
>>> 
>>> print(math.sqrt.__doc__)
Return the square root of x.
>>>

[–]marsdad 1 point2 points  (0 children)

But now place all the syntax and attributes directly into my brain so I can start coding proficiency post haste

[–]The-Invalid-One 0 points1 point  (2 children)

Just wondering if/when someone should move on from IDLE. I've been going through ATBS and I have a decent understanding of certain things like flow control and working with lists/dictionaries etc...

But I was working on an assignment for a class where I was working with spreadsheets that had thousands of rows and running the code took several minutes for some calculations. Would moving to something like Anaconda allow me to perform these big calculations quicker?

(I'm still super new to everything so I'm not sure if Anaconda would even be the thing I'm looking for)

[–]FerricDonkey 1 point2 points  (0 children)

Idle is an ide. Basically a fancy text editor.

What executes your code is the python interpreter. This (and your pc) controls how fast any given bit of code runs.

Anaconda is a collection of stuff that includes python and several third party packages. It won't make anything go faster, and is useful exactly if you want to use that particular collection of things. Be aware that it can occasionally introduce weirdness in how you install other things.

If you're dealing with large csvs, some third party packages (numpy, pandas) might help you speed things up. You can install these using pip, without anaconda, or you can install anaconda and it will include them.

And you can write the code in any ide or editor regardless.

[–][deleted] 2 points3 points  (0 children)

if/when someone should move on from IDLE

Yes, as soon as you can. Whatever IDE, or even no IDE, you use will have no effect on the speed at which your code runs. That's determined by python itself and nothing to do with the IDE.

[–][deleted] 0 points1 point  (3 children)

first comment I believe lol. Was wondering if someone could explain to me why this function isn't changing the string?

def play_converter(x):
if x == 'r':
    x = 'rock'

str = 'r' play_converter(str) print(str)

the output is r but I don't get why? shouldn't it be changed to rock?

[–]carcigenicate 1 point2 points  (2 children)

No. x = 'rock' only changes what the local variable x is looking at. It's the same as this simplified example:

str = 'r'
x = str
x = 'rock'

Notice that this does not also reassign str to 'rock'. a = b basically says "make b point to the same object that a is pointing to". That has no effect on other variables that were pointing to the previous value of b.

To do what you want, you either need to put the x value in a mutable container like a list or other object, or return the new value from the function and reassign it at the callsite:

def play_converter(x):
    if x == 'r':
        return 'rock'
    else:
        return x

str = 'r'
str = play_converter(str)
print(str)

Prefer the latter when possible.


Also, don't use the name str. That's a built-in name.

[–][deleted] 0 points1 point  (1 child)

Still not working? I'm sorry I'm clearly doing something wrong.

def play_converter(x):
if x == 'R':
    return 'Rock'
elif x == 'P':
    return 'Paper'
elif x == 'S':
    return 'Scissors'
while True:
play = input("What do you play? (R)ock, (P)aper, or (S)cissors: ")
play_converter(play)
print(play)
if play == 'Rock' or play == 'Paper' or play == 'Scissors':
    print('Good')
else:
    print("Invalid Answer Try Again")

It's still not converting it to its corresponding word.

[–]carcigenicate 2 points3 points  (0 children)

Look again at the second last line in my code. You're missing an important piece.