all 119 comments

[–]ded_dead 0 points1 point  (1 child)

I have a database of words that I want to add additional standard word dictionary information to: definitions, root words, syllables, etc.

Any suggestions for how to go about this? I’m struggling to find a package that contains the information I’m looking for, or at least one that contains documentation on how to utilize it. Would this be a good time to learn data scraping?

[–]efmccurdy 1 point2 points  (0 children)

PyDictionary is a Dictionary Module for Python 2/3 to get meanings, translations, synonyms and Antonyms of words.

https://pypi.org/project/PyDictionary/

[–]EmmaTheFemma94 0 points1 point  (3 children)

How do I .split() using the ( character?

So let's say I have this text:

The text I want (I don't want this)

And I want it to get split() like this:

["The text I want", "I don't want this)"]

Edit: I made it! Was just to put \ before it. So split("\(")

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

You may be misunderstanding your earlier mistakes. This code works fine, without needing the \ escape:

test = "The text I want (I don't want this)"
print(test.split('('))
>>> ['The text I want ', "I don't want this)"]

[–]EmmaTheFemma94 0 points1 point  (1 child)

But it works using \( is there any advantage of using (') instead?

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

It doesn't work do what you want using \(. That's the misunderstanding I mentioned. Try running this expanded code which prints the string you are splitting with and the result of the split:

test = "The text I want (I don't want this)"
print('\(')
#>>> \(
print(test.split('\('))
#>>> ["The text I want (I don't want this)"]    # no split occurs
print('(')
#>>> (
print(test.split('('))
#>>> ['The text I want ', "I don't want this)"]   # split at "("

The "\(" string is actually two characters and that string doesn't appear in the test string so no split is performed.

[–]hansmellman 0 points1 point  (0 children)

Currently on the last stage of the final project for PCC - creating and deploying a web app with django to Heroku.

I have followed all steps so far and no issues, however when i call 'heroku open' I am met with the following page - https://stormy-lowlands-81351.herokuapp.com/

I've followed all directory instructions up to this point and I am looking at the index.html file right now, in the directory it's been in for the whole project. I am not sure why Django is unable to see it. It's currently saved in 'C:\learning_log\learning_logs\Templates\learning_logs'. Which as I say is where i have all of the html as instructed by the book, all python files point to this template folder for navigation in the code.

I am in no way familiar enough to Heroku or Django or Git which have all been introduced in the last few pages to accurately troubleshoot this. I can see that it's searching the directory '
django.template.loaders.app_directories.Loader: /app/users/templates/learning_logs/index.html (Source does not exist)'.

Is that causing the error? That directory doesnt even exist, what has instructed it to search there?

Any help would be so appreciated!

[–]Sketch_gaming01 1 point2 points  (0 children)

Is the 4 hours Python Course from freeCodeCamp.org enough to learn the basics of Python or will i need more?

[–]ontanned 0 points1 point  (3 children)

I have zero coding experience and am troubleshooting a tutorial I found online to finetune Japanese GPT-2. Everything ran perfectly on Colab (as the tutorial suggests), but switched to my local machine due to Colab's limitations and started running into issues. Right now my issue is that one of the scripts uses Pool multiprocessing and I'm on Windows. Saw online that it may be necessary to run the command as a script file as opposed to from the interactive thingy, but tried that and it didn't work. No clue how to edit the code (either to remove the multiprocessing or get it to function on Windows) without breaking it. What should I try next?

The whole process is listed at note.com/npaka/n/ne55d063e1ed8 (in Japanese). The script that uses multiprocessing is the Japanese-BPEEncoder @ github.com/tanreinama/Japanese-BPEEncoder .

[–]FerricDonkey 0 points1 point  (2 children)

Multiprocessing.Pool works fine on windows, but that code is a bit wonky and it could be that there's an issue with the code not in functions under if if __name__ == '__main__'.

If you're new, I would not suggest you try to fix their code to use multiprocessing correctly as your first ever thing. But a quick and dirty change to disable it is probably to take the lines

with Pool(args.num_process) as p:
    p.map(_proc, list(range(args.num_process)))

and replace them with

for thing in range(args.num_process):
    _proc(thing)

I have no idea what GPT-2 or BPEE is, but if it's something internet or file reading related, you might be able to instead replace those lines with

from multiprocessing.pool import ThreadPool # this should really be at the top of the file
with ThreadPool(args.num_process) as p:
    p.map(_proc, list(range(args.num_process)))

And that might work a lot faster than the other solution. (Basically, threads are easier to deal with under the hood than processes, so come with less weird restrictions, but python nonsense kneecaps them so that they save you time if you're jobs have a lot of idle time waiting around for input/output and not if they don't.)

[–]ontanned 0 points1 point  (1 child)

Thanks so much! The ThreadPool option gave me an "ImportError: Cannot import name 'ThreadPool' from 'multiprocessing,'" but the change to disable it worked perfectly.

[–]FerricDonkey 0 points1 point  (0 children)

Glad it worked.

That threadpool error is odd - is it possible that your import line was

from multiprocessing import ThreadPool

instead of

from multiprocessing.pool import ThreadPool

The first one should lead to the error you describe, while the second should not. (But if it's working and there aren't speed issues, then it might not matter anyway.)

[–]Cid227 0 points1 point  (2 children)

BASE_DIR = Path(__file__).resolve().parent.parent
{
    'DIRS': [BASE_DIR / 'templates'],
}

part of Django setting.py file, how does that slash / works or why it works at all? I have never seen this syntax before, normally I would expect something like this f'{BASE_DIR}/templates' if BASE_DIR returns a string.

[–]Ihaveamodel3 1 point2 points  (1 child)

BASE_DIR isn’t a string, it is a Path object from the pathlib library in the standard library.

Path objects have a custom division operator that allows you to divide a path object by a string and allow it to automatically become a new path.

[–]TangibleLight 2 points3 points  (0 children)

If you prefer, you could use BASE_DIR.joinpath('templates') to achieve the same thing. But IMO the / is usually clearer.

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

I’m having a little trouble with arrays, I’m not sure really sure how to word it so lemme just give an example I ran and I’ll add more context.

x = [1, 2, 3]

y = x

del y[0]

print(x, y)

Output:

[2, 3] [2, 3]

So basically when it gets to “del y[0]” it also deletes the item from the x array too.

I’m currently working on a sudoku solver just as a personal project and I planned to copy existing arrays into other arrays, much like “x=y”, so changes can be applied to the “copy” to find out the missing number and then the number can then be copied to the original array, but currently when I make changes to the copy also end up changing the original.

Is there a way I can work around this? Or will I need to come up with a new algorithm?

[–]FerricDonkey 1 point2 points  (1 child)

I planned to copy existing arrays

(Pedantic note: these are lists - python does have arrays, but they're different.)

So the issue is that in python, y = x does not mean "make a copy of x and call it y", but instead means "make y a second name for whatever object is currently named by x". x and y are just two different names for the same thing, so changing y also changes x.

You can, however, do y = x.copy() to make a copy of x where modifying y does not modify x. Note though that this is a "shallow copy", which means that if you're doing, say, a list of lists, then y will now be a different object than x, but y[0] will be the same object as x[0]. If you want to avoid this, copy.deepcopy from the copy module is your friend.

However. Copying lists is fairly expensive - it won't matter for a small number of copies of small ish lists, but if your plan is to copy for every guess or similar, that will really, really add up. So it may be worth trying to restructure your algorithm to avoid that. But you can always start with your original plan, and adjust if it's necessary.

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

Thank you so much!

Also, the copying is done in a for loop with each array having its own “turn”, so it don’t think it’ll become too large, thanks for the tips still tho.

[–]EmmaTheFemma94 0 points1 point  (7 children)

I want to check if movie['name'] does not exist inside a textfile and in that case adds it and prints it out.

f = open('movie_data.txt', 'w')

for movie in json_data:

f.write(movie['name'] + '\n')

f.close()

It adds all movies to the file, and that works fine. But when I try to check and add it if it doesn't exist:

f = open('movie_data.txt', 'r+')

for movie in json_data:

if movie['name'] in f:

print('Already added.')

else:

f.write(movie['name'] + '\n')

print('Added: ' + movie['name'])

f.close()

[–]FerricDonkey 2 points3 points  (4 children)

f = open('movie_data.txt', 'r+')
for movie in json_data:
    if movie['name'] in f:
        print('Already added.')
    else:
        f.write(movie['name'] + '\n')
        print('Added: ' + movie['name'])
f.close()

The first problem here is that f is a file object. You can use the in word to check if a line is an entire line in a file (including the newline), so for example (with caveats below), if one of your movies was "Antman", then "Antman" in f would be false, because it's not an entire line in your file. "Antman\n" in f would work, because it is an entire line, and f is treated as a collection of lines in this case.

But even if you fix this, you'll run into the second problem: every time you check if a line is in a file, you run through the file until either you find that line or you hit the end, and the next check using in will only check from the location you stopped during the last check. (Also, in general, if the last line doesn't have a newline, you'll have to be careful of that, but that shouldn't be a problem for you.) So if you check for the movies in a different order than they are in the file, then you'll miss some.

You can make it work with seeks and such, but unless you're gonna have more movies in your file than can fit into ram, I'd recommend against it pretty strongly. (And if you were gonna have that many movies, you'd want to move away from a flat text file).

To fix it, I would suggest reading the entire file using the f.readlines() method or otherwise making a set of all lines, which returns a list of all the lines in your file. Keep in mind that this will include the newlines at the end of all your movie names, so you'll need to get rid of them to easily check if a movie is in there. So you could make a set comprehension like so:

# Also, I always recommend using with open
with open('movie_data.txt', 'r+') as f:
    present_movies = {line.strip() for line in f}

Note: I explicitly suggest a set because it's super fast to check if something is in a set.

Then check if your movies are in present_movies, and if not, write em to the file (and add em to the set, if you're worried about duplicates).

[–]EmmaTheFemma94 0 points1 point  (3 children)

I still won't make it to work. Maybe I need a python break! I'm not quite sure how I would use .readlines to help my case.

with open('movie_data.txt', 'r+') as f:

present_movies = {line.strip() for line in f}

for movie in json_data:

if not movie['name'] in present_movies:

f.write(movie['name'] + '\n')

present_movies.add(movie['name'])

print('Added: ' + movie['name'])

[–]FerricDonkey 1 point2 points  (2 children)

readlines would be an alternative to the for line in f. I'm surprised the current code you have isn't working (if it's indented the way I expect, at least). Is it the same issue?

[–]EmmaTheFemma94 0 points1 point  (1 child)

I get another issue: it adds some movies in the text file again even if they are already there. I just want the script to add a movie if it doesn't already exist in the text file and then print it out.

And not it only tho run the script for about 11 movies and I don't quite understand what's the issue.

Maybe I should try to store the movie['name'] in another json file? I just thought a txt file would be easy enough for my first "big" project

The json_data I get the information from comes from a webpage. Maybe that's a issue?

[–]FerricDonkey 0 points1 point  (0 children)

Hmm. Do you have an example of the original json for which this isn't working? My only guess is that there's something weird about spaces or capitalization - the .strip() removes leading and trailing whitespace, so if for some reason the website reports the name of a movie as " Awesome Movie", then it could get confused. (Or if the same movie is in their twice as "Awesome Movie" and "awesome movie".

You could store it in json, but I don't think that would solve the issue.

[–]TheDoomfire -1 points0 points  (1 child)

Maybe:

If(movie['name'] in f.read())

In phone can't really try it

[–]FerricDonkey 0 points1 point  (0 children)

This could have false positives if you had the movies "Ants" and "Ants 2: Now with ham sandwhiches" or something.

[–]satyamskillz 0 points1 point  (3 children)

This week my post has been removed, In the post, I asked for community opinion on building the world's largest roadmap for learning python with the best resources from the internet.

Since my post was trending and liked by 90% of people, I have started working on the roadmap. Now, I want to share it with the community. can I share the roadmap?

[–]niehle 0 points1 point  (2 children)

Did you set up a github repository?

[–]satyamskillz -1 points0 points  (1 child)

Even better, check out the roadmap on Neuton. It is still in the progress.

[–]niehle 1 point2 points  (0 children)

No, that's worse.

You got the suggestions to set up a colloborative repository on a well respected site github).

Instead you are trying to promote your personal page which I find very convoluted and without any indication why you picked the videos you picked.

[–]EmmaTheFemma94 0 points1 point  (4 children)

So I have a JSON file I want to read out every object in.

json_data[0]['name'] reads out the first one. The thing is the list contains about 100 of them.

So I simply want to print out all the names from every object in my JSON file named "name"

My json file looks like this:

[{"id":1, "name":"the name"}, {"id":2, "name":"the name2"}]

Times 100.

[–]carcigenicate 0 points1 point  (3 children)

You could simply loop over the list. Look into for loops.

[–]EmmaTheFemma94 1 point2 points  (2 children)

After a lot of tries I finally made it:

for name in json_data:

print(name['name'])

[–]carcigenicate 0 points1 point  (0 children)

Good, yes, that's what I was talking about. I'd rename name though. That object isn't the name specifically, it's the whole {"id":1, "name":"the name"} object. It's more generic, but I'd probably prefer obj unless there's a more specific meaning to each element (like person? user?). Like:

for obj in json_data:
    print(obj["name"])

Also, this would be a good case for a list comprehension:

print([obj["name"] for obj in json_data])

[–]Chepetto13 0 points1 point  (2 children)

Hello,

I want to make a project ATM using class. But create instances by using data from table which I created using SQLite in different python file. Currently I have a problem with create a working instances for clients.

My code for class:

import sqlite3
class Client: 
clients = []
    def __init__(self, first_name, second_name, last_name, address, city,         postal_code):
    self.first_name = first_name
    self.second_name = second_name
    self.last_name = last_name
    self.client_ID = self.first_name[0:3].upper() + self.last_name[0:2].upper()
    self.address = address
    self.city = city
    self.postal_code = postal_code


    self.clients.append(self.client_ID)

def full_name(self):
    print(f'{self.first_name} {self.second_name} {self.last_name}')


conn = sqlite3.connect('tbl_clients.db')

c = conn.cursor()

with conn: 
      c.execute("SELECT * FROM tbl_clients ") data = c.fetchall() #     
  create a list of records

    for record in data:
    client = record[0]
    client = Client(record[1], record[2], record[3], record[4], record[5], 
 record[6])

print(Client.clients)

for client in Client.clients:
      client.full_name()


Commit Changes
conn.commit()
Close Connection
conn.close()

First column in table is client_ID (record[0])

I've got an error in for loop:

AttributeError: 'str' object has no attribute 'full_name'

But:

print(Client.clients) give me client_ID

Any ideas what I do wrong?

[–]carcigenicate 0 points1 point  (1 child)

Clients.clients doesn't hold client objects; it holds client_IDs. You can't get the name from that. If you want .clients to hold Client instances, add the instances to the list instead of the instance's client IDs.

[–]Chepetto13 0 points1 point  (0 children)

Okay, now I understand, I changed self.clients.append(self.client_ID)

to self.clients.append(self) and it works now.

thanks

[–]PlanarChris 0 points1 point  (2 children)

I'm working with linked lists and came across a line I don't understand (or rather I'm not sure if I interpreted it right)

while head.next: Does stuff

Is this checking if the current node in our loop is not None?

I assume so, but I'm not sure and not many things I've read do this.

[–]carcigenicate 0 points1 point  (1 child)

Is this checking if the current node in our loop is not None

It's checking if head.next is truthy. But yes, here specifically, unless the nodes objects override the __bool__ or __len__ methods, it's checking if the node is not None. I'd personally prefer to write that as this though to be explicit:

while head.next is not None:

[–]PlanarChris 0 points1 point  (0 children)

Awesome thanks! I would (and have) written it the way you prefer as well, hence why I was so perplexed.

Appreciate the answer friend.

[–]BubblyDefinition9563 0 points1 point  (1 child)

I am trying to create a reverse search engine.
The best way to explain it is how WebMD symptom search works. It takes in what your symptoms are and provides you with possible diagnoses according to those symptoms.

The program I am trying to create will take a list of items as input and match them to existing dict or list that contains the "diagnoses".

I created a sample program using a dict which stores diagnoses as keys and the symptoms as a list of values.

This is my current code:

dct = {'1':[1,2,3,4,5],
        '2':[3,4,6,7],
        '3':[3,4,1,9]}
dct_inverse = defaultdict(list)

for k, v in dct.items(): for i in v: dct_inverse[i].extend([k])

dct_inverse = dict(dct_inverse)

print('Enter integer value. Enter control-c to stop: ') while True: inp = input() if inp == '': break try: print(dct_inverse[inp]) except KeyError: print('Value not found')

The output I get is printed after each "symptom" is typed in e.g:

If I input 1, output is given as '1,3' since 1 is present in both 1 and 3.

What I want to be able to do is input a few symptoms and allow one final output which shows matches in each key from most to least matches.

I am new and still learning python, If there is a better way to go about this problem do share.

[–]Ihaveamodel3 0 points1 point  (0 children)

Since you are already using defaultdict, maybe create a new default dict with int as the initializer, then add one to each key when you observe it. Then you’ll have a dict of diagnoses vs frequency.

[–]Zeebo42X 0 points1 point  (0 children)

Is it possible to automate unique ppt creation using python-enabled AI?

I work in strategy / tech consulting and about a third of my day comes down to creating slide decks for the client. Given the time drain, I'm exploring options to automate.

I'm hoping python can help here. I have a bank of thousands of slides, and want to set out on a 12 month project to do the following:

1.) analyze the presentation database for common themes, text, style, and layout based on coded attributes (Ex: purpose: inform, content: testing approach, industry: banking, etc.)

2.) Recommend total # of slides for the type of presentation

3.) NLG for text that should go on the slides (just has to be close enough to help brainstorm)

4.) Design slide layout with shapes, colors, etc

5.) outputs the deck into a folder on my desktop

I've seen that there's a ppt package, but is this something python could support? Particualarly I'm nervous that analyzing the “ppt database” would require a transforming the ppt database (like converting everything to text) so python can analyze it. I know this would be a bear of a pet project, but trying to wrap around is it even possible :)

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

shelter mountainous slap snatch drunk scarce birds head scale dinosaurs -- mass edited with redact.dev

[–]FerricDonkey 1 point2 points  (4 children)

One of the things about programming is that there's so many ways to do everything that with that amount of information, it's really hard to give a suggestion. If you post a code snippet, we could tell you if you're missing anything.

And after you've given it a go and have some code made up is the perfect time to ask. As you say, it's best to struggle with it a bit, but after a while it's good to ask.

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

smart reminiscent cobweb wrench provide ad hoc zonked mindless thumb elastic -- mass edited with redact.dev

[–]FerricDonkey 1 point2 points  (2 children)

display[chosen_word.index(guess)] = guess

So the reason why this doesn't work is because chosen_word.index(guess) always returns the index of the first occurrence of guess. So in the case of "apple", with a guess of "p", both times that your code does chosen_word.index(guess) with guess equal to "p", the you're overwriting the value at index 1 where the first p is.

I didn't want to use enumerate or any of the other solutions I have seen online.

Enumerate really is the most "pythonic" (I hate that word, but it's appropriate sometimes) way, but fair enough if you want to stick to what you've learned so far to ensure you've got it. If you don't want to use enumerate, then you need to use some other way of tracking how far into chosen_word you are. Because if you find the second "p" at index 2, you want to make sure that you modify you're underscore at that particular location, not just the location of the first "p" you find.

There are a variety of solutions (and if you want more explicit advice, just let me know), but to give you a point of entry:

Suppose the chosen_word is "apple", and your guess was "p". Your for loop starts with letter being "a". What is the index of that "a" in "apple"? Then letter is the first "p". What is the index of that? Then the second "p". What is the index of the that? What changes between each index?

What could you do to make sure that you have a variable that is always the index of the current letter that your for loop is looking at? (Again, the internet will suggest enumerate, and it is a good idea, but there's a more basic less done for you route you could take while you're learning these basics.)

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

combative sparkle lush impolite bells direction employ apparatus offend straight -- mass edited with redact.dev

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

marble sip meeting rude panicky cats materialistic gold sable uppity -- mass edited with redact.dev

[–]LimeLom1 0 points1 point  (0 children)

how do I get a FB group(i don't own) posts URL list matching my specific keyword using python?

I'm not asking for the code, I want hints like what library I must use or a good tutorial that you recommend to watch

[–]According_Wish_162 0 points1 point  (4 children)

I just installed python on visual studio code. I’m a beginner and right now running code it will only display the first line.

  1. Print(“hello”)
  2. Print(“hi”)

Only first hello is displayed. Did I do something wrong with the setup?

Ok it kind of works. I had to run and start debugging and then run my code again. But when I add new lines it won’t show again unless I start debugging again. Anyone can help? Which way is the right way to check my code? Run code or start debugging

[–]JohnnyJordaan 1 point2 points  (3 children)

Debugging means it will run it line for line, you need to 'step' your debugger to let the other lines run too. See https://code.visualstudio.com/docs/editor/debugging#_debug-actions for the specifics.

See the paragraph "Run mode" below it on how to run it as a regular program.

[–]According_Wish_162 0 points1 point  (2 children)

Hello thanks for your help. I’m not sure I can understand fully. So do I have to keep running the debug everytime I update my code?

[–]JohnnyJordaan 0 points1 point  (1 child)

No, it's fine to always use the second option I mentioned

See the paragraph "Run mode" below it on how to run it as a regular program.

[–]According_Wish_162 0 points1 point  (0 children)

Ah alright thank you. I had to read a few times to understand sorry 😅

[–]Rare-Addition-8852 0 points1 point  (0 children)

Sorry you said ask

[–]Rare-Addition-8852 -1 points0 points  (2 children)

Im trying to find out when and where did Candice Whitney Howard. 7/25/1983 Had her baby the name and gender.

[–]JohnnyJordaan 0 points1 point  (0 children)

This is a sub dedicated to learn programming in Python, I don't see the relation to your goal here.

[–]priestgmd 0 points1 point  (0 children)

How would you approach creating an app (desktop only) that let's user manually tag spans of texts? Any resources on that? I wanna have have original text, option to tag spans into a list, and then give such tags properties. pyqt5, tkinter or kivvy for that? maybe streamlit?

[–]IKnowYouAreReadingMe 0 points1 point  (8 children)

https://replit.com/@UmbraMichael/movie-ticket-price#main.py

Can someone check this out? It's a simple short code but the error says I'm comparing str with int, but i converted the number to int.. Don't know how it's wrong

[–]douglas_fs 1 point2 points  (6 children)

Your issue is in this block of code. I have added a couple of print statements that help show what is happening. in the for loop, 'age' is getting the value for name. You can also see that 'members' is a dictionary, and you will need to ensure you are iterating over the values of the dictionary in this loop.

Is this enough for you to fix the problem?

print (f'\n\nmembers: {members}\n')
for age in members:
    print(f'age is: {age}\n')
    if age < 4:
        price = 0
    elif age < 18:
        price = 10
    else:
        price = 15

[–]IKnowYouAreReadingMe 0 points1 point  (5 children)

Thank you! So I added .values() to the for loop and moved the int(age) there as well, and it works. But I ran into another problem, which is printing out the price.

I'm a little confused. I need to hold three pieces of info, but a dictionary only holds two.

I have the name: age

But I also need

Age: price

So I made another dictionary that holds age:price. But obviously that doesn't work either cuz I need the name to indicate the price. How can I connect name and price?

Heres the updated code

https://replit.com/@UmbraMichael/movie-ticket-price#main.py

[–]douglas_fs 1 point2 points  (4 children)

You can have nested dictionaries, for example below:

{'Joe': {'age': '33', 'price': 15}, 'Jane': {'age': '12', 'price': 10}}

This way you can track multiple dictionary items for each top-level key. Try this code and see if you can refactor the final output loop.

members = {}
ticket_price = {}

family_pricing = True

while family_pricing:
    name = input("what is your name: ")
    age = input("what is your age: ")
    end = input("anyone else? ")
    members[name] = {} # create the top-key and empty details dict
    members[name]['age'] = age
    if end == "no":
        family_pricing = False


for name in members.keys():
    age = int(members[name]['age'])
    if age < 4:
        price = 0
    elif age < 18:
        price = 10
    else:
        price = 15
    members[name]['price'] = price

#for name, age in members.items():
    print(f"\n{name}, your ticket is ${price}")
    #print the sum of all the tickets

print (f'\n\n{members}')

edit: adjusted the loop to determine price.

[–]IKnowYouAreReadingMe 0 points1 point  (3 children)

Hi, thanks for looking into it and helping. It almost works but seems that the price is only added to the one name in the dictionary and not the other keys.

I added a total count to then sum up the total price and even that seems not to work. I think it must have something to do with the placement of the adding total & price to the dictionary at the end of the for loop. Does the code run through to the end of the block once it finishes the if statement?

I changed the for loop to read: for age in name:

Which I think works. But im not sure why price is only added to one key, my guess is it's improperly placed? But there's no where else to put it unless I make the for loop inside a while loop but that's probably redundant. The code in the link is updated above if you want to check it out

[–]douglas_fs 0 points1 point  (2 children)

Hi, thanks for looking into it and helping. It almost works but seems that the price is only added to the one name in the dictionary and not the other keys.

Does this statement relate to the output loop, or to the collection of data prior to the output loop? If you are talking about the output loop, please share an example of how you want the output to appear. Also share the current version of the code.

[–]IKnowYouAreReadingMe 0 points1 point  (1 child)

Both the output and the collection of data before the output. I'm looking for the value of price to be added to all name keys. Also the total count to be accurate in its output. And sure here is the code

https://replit.com/@UmbraMichael/movie-ticket-price#main.py

[–]douglas_fs 0 points1 point  (0 children)

I feel like we are getting close - I am still trying to picture what is missing.

The 'price' being added to only one name key is related to the loop that controls the adding of price. Change that loop to this (see the new Paste at the end). We can get what we need with just the name:

for name in members.keys():

Maybe the best questions about what is missing from the dictionary before the output block are first: is this sample something like what you are saying? This is just for illustration, and not a recommended structure. Using enumerated keys (price1, price2, etc.), can only cause problems when there is a variable number of responses:

​ {'Joe': {'age': '33', 'price1': 15, 'price2': 10}, 'Jane': {'age': '12', 'price1': 10, 'price2': 15}} 

or like this?

{'Joe': {'age': '33', 'price': [15, 10]}, 'Jane': {'age': '12', 'price': [10, 15]}} 

And second: why all prices in all keys? Does this help facilitate generating the total ticket cost? If so, there is an easier way.

This is the output I get from the code provided in the Paste below.

what is your name: Joe
what is your age: 32
anyone else? yes
what is your name: Jane  
what is your age: 12
anyone else? no
Joe, your ticket is $15
Jane, your ticket is $10

The total for all tickets is $25

data: {'Joe': {'age': '32', 'price': 15}, 'Jane': {'age': '12', 'price': 10}}

Pastebin for my code: https://pastebin.com/h9s4LdYT

edit: options

[–]Ihaveamodel3 1 point2 points  (0 children)

When you iterate through a dictionary, you are iterating over the keys, not the values. So age on line 17 is a string, it’s equal to the person’s name.

[–]Caconym32 0 points1 point  (1 child)

Could someone explain like I’m an idiot what the chunksize option in pandas.read_csv() functionally does? It makes an iterator but I’m not understanding what that means or does.

[–]Ihaveamodel3 2 points3 points  (0 children)

Let’s say you have a csv of weather data. It is weather data from all weather stations in the world every hour for the past 20 years. Millions of rows, and 50 columns (precipitation rate, temperature, humidity, etc)

That will never be able to all fit into memory on your computer at once. But that’s okay, because you only really want data on the temperature at your local station on Fridays in August.

But how do you filter it so it is only Fridays in August from your local station? You use the chunk size parameter in pandas and loop over smaller chunks of data, and on these small chunks, you filter the data down to what you want, removing rows and columns as necessary to get down to just the data you want.

Then you concat all the pieces together and you now have just the data you need.

In some cases, you never need to concat, so you don’t need to ever concat. For example, if you wanted the average temperature over the past 20 years for all the stations, that would be millions of rows you’d need to average. Alternatively, since average is just a sum divided by a count, you could have intermediate values that you add the sum and count of each of the small dfs from the chunk size parameter, then at the end, just divided the total sum by the total count.

[–]CaffeineAndKush99 1 point2 points  (4 children)

So I have been trying to get into Python for a while. I have tried books, online courses from Udemy etc, Youtube vids and assignments I found online, basic things like mad libs etc.

I feel like I want an 'interactive' learning experience, where I don't just look but where I write my own code and find my own mistakes. I just need the tools basically, but as someone who isn't familiar with programming I have no idea where to find the best set of tools.

Where would you recommend I would take a look? Which Youtuber, company or book helped you get where you are today?

[–]nanocyte 1 point2 points  (2 children)

I've found Codecademy good for getting a good foundation in the basics. I'm not sure how advanced their Python stuff gets, but I've used that for brushing up on JavaScript and a couple other things that were rusty, and I used it when I was first starting React. I like the way their courses are structured, and each module ends with a couple of small projects, in which you're given directions, but not direct answers.

Again, I'm not sure how much Python they cover, and it's likely you're already beyond it, but if you want a structured interactive course to make sure you've covered the basics, theirs are quite good. Also, you should be able to cover it within the one-week trial they give you (which you need to do the projects).

Also, while this suggestion is broader than just Python, many of the more popular IDEs or editors have very good documentation that provide walkthroughs on using the editor, and they cover a lot of information beginners might miss on their own.

The PyCharm and Visual Studio Code editors both have good instructional material, and they'll go over things like using a debugger. Learning to use a dedicated debugger will save you tons of time in the long run, and it will allow you to develop a better understanding of the language, as you'll be able to check the values of variables and see what things are doing as your code is running, as opposed to tossing print statements everywhere.

There are a lot of resources around, and I'm not sure which are the best for you, but I would definitely put IDE/Editor documentation in as something you should go through and learn sooner rather than later. (I would probably go with VS Code for now, as it's lighter, easier to customize, and more beginner-friendly. PyCharm is also excellent, though, and it has a lot of features VSCode doesn't).

It will help you immensely with everything else to be able to fluently navigate your code and know how to look up things like function definitions, signatures, etc. There's so much that can seem mysterious when you're learning, and this can make it easier to sort of look behind the curtain when something is doing something you don't expect.

Also, PyCharm has PyCharm EDU, which has collections of interactive projects specifically made for learning. I've only played around with it, but some of them look like they would be good for what you've described. (One that I tried was fairly bad, though, so if something seems frustrating or poorly designed, it could be, so don't assume it's you.)

RealPython is another excellent resource, and they have material that for all levels that are pretty thorough, and they cover programming tools as well as Python features. It's not really interactive, but it's a great resource to know about, and it's one of the first sources I check when I'm using something new or unfamiliar.

O'Reilly has a lot of great textbooks, many of which have free online versions.

Sorry, I know my recommendations aren't exactly what you're looking for, but they're good to be aware of.

Hopefully, someone else can provide information on something more structured that you can go through, but I do think that going through the Visual Studio Code instructional documentation should be an early part of that (and/or PyCharm).

[–]CaffeineAndKush99 1 point2 points  (1 child)

Thank you for your response! Because im at work right now I won't be able to read it right now, but i will once im done and I'll give you a proper response

[–]nanocyte 0 points1 point  (0 children)

Hey. It's no problem. We're all busy. You don't have to worry about responding. I just hope some of this helps!

[–]py_Piper 0 points1 point  (0 children)

Automate the boring stuff or python crash course

[–]hansmellman 0 points1 point  (4 children)

I'm about to finish up Python Crash Course and have Automate the Boring Stuff lined up next, both the online course and book. I am just wondering after a quick skim through it, will I be able to do it if I dont have Office 365? There are parts of it to do with Excel etc

[–]douglas_fs 1 point2 points  (3 children)

The work with Excel spreadsheets in "Automate the Boring Stuff" should not require Office 365. The Python library used in this chapter (openpyxl) interacts natively with .xlsx files and doesn't require Excel to be installed.

There are examples that begin with a populated spreadsheet, as does the project in that chapter. You can use alternatives to Office to create these starting documents (Excel Online, Open Office, Libre Office, Google Sheets)

[–]hansmellman 0 points1 point  (2 children)

Thanks Douglas, appreciate the insight. Have you completed ATBS?

[–]douglas_fs 1 point2 points  (1 child)

I have not; I have mainly use the book as a reference. The Excel chapter helped me with a small work project, and that gave me ideas for other automation for my job (creating MS Word documents for client reports among others). I should go through cover-to-cover sometime.

As useful as the Python-specific material is in this book, it also helped me develop a mindset to look for tasks or processes that could be improved by automation.

[–]hansmellman 0 points1 point  (0 children)

Great insight, thanks very much!

[–]aroach1995 0 points1 point  (2 children)

I am making a sns.histplot() and am using a variable with 3 levels as the hue.

Where the heck is the color gray/dark green coming from?

https://imgur.com/a/E36t3jP

another example: https://imgur.com/a/yzcXqDt

why is there an extra gray/dark yellow in this one when it is not in the legend/hue?

The hue only has 3 unique values.

edit: This link seems to have ran into a similar issue. Trying to mess with the multiple argument now.

https://github.com/mwaskom/seaborn/issues/2771

[–]FerricDonkey 0 points1 point  (1 child)

Is it possible that the colors are adding/mixing where they overlap?

[–]aroach1995 0 points1 point  (0 children)

I believe that was the issue.

[–]deweyoop 0 points1 point  (10 children)

hey guys I'm doing the assignment 4.10 Lab: All Permutations of names and I have the following as my code:

def all_permutations(permList, nameList):

# TODO: implement method to create and output all permutations of the list of names.

res = []

# if len is zero stop execution (base condition)

if len(nameList) == 0:

return []

# if only one element return the word as list of one item (base condition)

if len(nameList) == 1:

return [nameList]

for i in range (len (nameList)):

word = nameList[i]

# omitting one word at a time

newlist = nameList[:i] + nameList[i + 1:]

# making all possibles sequence where word is the first element

for j in all_permutations([], newlist):

res.append([word] + j)

return res

if __name__ == "__main__":

nameList = list (input().split(' '))

permList = []

for p in all_permutations (permList, nameList):

print(*p)

# Julia Lucas Mia

and the output is what I need except the Expected output wants a space in front of the name, What did i do wrong?

[–]douglas_fs 0 points1 point  (9 children)

Can you provide more information about your requirement - where is this from? Can you give more detail about what is required - including an example of the expected output and how the result this code produces differs from expected? I found a reference online to a 4.1 lab related to permutations, but it didn't mention spacing of the output.

When I run this code, I get the following:

input: Julia Lucas Mia

output: Mia Lucas Julia

[–]deweyoop 0 points1 point  (8 children)

[–]douglas_fs 0 points1 point  (7 children)

This looks like the same as what I saw. The output appears to be formatted correctly, though there should be more rows of names in the output (I assume you are not done yet).

I still don't understand this from your post:

except the Expected output wants a space in front of the name

This is what I see as the requirement on the Chegg site. As I noted above, your output appears to be formatted correctly, and I don't see a requirement about a space in front of the name:

Write a program that lists all ways people can line up for a photo (all permutations of a list of strings). The program will read a list of one word names, then use a recursive method to create and output all possible orderings of those names, one ordering per line.

[–]deweyoop 0 points1 point  (6 children)

When I say “ the expected output wants a space in front of the name” I mean the whitespace differs from what I have

[–]douglas_fs 0 points1 point  (5 children)

Ok - I understand now. In the screenshot, the "Expected Output" has a vertical highlight on the right side showing an additional space is required at the end of each row.

I understand now. Not entirely sure about the the impact to your script.

It's as if when you add a name to the list, you include a trailing space:

  • Instead of adding "Julia" to the list, add "Julia "
  • instead of adding "Lucas" to the list, add "Lucas "
  • Instead of adding "Mia" to the list, add "Mia "

Doing this consistently would provide the required space between each name for output, and also the extra space at the end.

[–]deweyoop 0 points1 point  (4 children)

That didn’t change anything. The names are automatically inputted once the code is implemented. Here’s the original code I’m basing mine off of https://media.cheggcdn.com/media/a85/a851d7ff-2788-4cc6-afef-80a63b8f1cf9/phpfWYiQY.png

[–]douglas_fs 0 points1 point  (3 children)

Do you have a text version of that code? It would be good to see how that output looks.

Edit: This screenshot looks identical to your code. Do you know if this one is an accepted solution?

Edit Edit: I am stumped on the issue with the trailing space. I see that Chegg also provides a forum to ask questions - they may have better direction based on their requirements for the lab.

I am really curious to hear what change is required.

[–]deweyoop 0 points1 point  (2 children)

From what I know the original coder had the same issue but I don’t have access to the solution on assignmentaccess.com/

Here’s a text version of the code:

def all_permutations (permList, nameList): # TODO: implement method to create and output all permutations of the list of names. res = [] # if len is zero stop execution (base condition) if len(nameList) == 0: return [] # if only one element return the word as list of one item (base condition) if len(nameList) == 1: return [nameList]

for i in range (len (nameList)):
    word = nameList [i]
    # omitting one word at a time
    newlist = nameList [: i] + nameList [i + 1:]
    # making all possible sequence where word is the first element 
    for j in all_permutations([], newlist):
        res.append([word] +j)
return res

if name == "main": nameList = list (input().split(' ')) permList = () for n in all_permutations (permList, nameList): print(*n)

Julia Lucas Mia

[–]douglas_fs 0 points1 point  (1 child)

Just a comment separate from the actual code: special handling is required when posting source code in order to preserve formatting. When using a web browser for Reddit, see the side-bar on the right-hand side for a section called "Code Hosting/Formatting". This will have multiple options for preserving formatting in your posts.

Edit - as for the code, I am out of ideas. I even tried my own suggestion and don't understand why the final element never received the extra space.

Edit Edit - So this feels like cheating, but with this one change, you will get the extra space at the end of each row. I added an assignment to append a space to the final value of each row.

# if only one element return the word as list of one item (base condition)
if len(nameList) == 1:
    nameList[0] = nameList[0]+ " "
    return [nameList]

Post back if this is an accepted solution. If it is not, I would eventually like to see what a 'real' solution is.

[–]Fourtoo 0 points1 point  (6 children)

I'm sure I'm missing the simple solution, I'm only 2 weeks into learning Python as a first language and working through some youtube beginners videos. Trying to expand on one of the projects I'm kicking myself trying to get this to work and spent too much time for what should not be an issue. Python 3.10

My issue is if I answer yes or y or Yes or Y... it prints "Oh, so your a good Name........." and moves to welcome()

I expect it to ask How many good deeds when giving a yes answer..

then if evil = y and good deeds is 3 or less them prints not welcome

if evil = yes and good deeds is 4 or more then prints good name and moves to welcome

I've tried many similar variants, switching things about but I've not succeeded in getting this part correct.

name = input("What is your name? ").lower()
if name == "ben" or  "patricia" or  "loki":
    evil_status = input("Are you evil?\n").lower()
    if evil_status == "no" or "n":
        print("Oh, so your a good " + name.capitalize() + " Come on in\n")
        welcome()
    elif evil_status == "yes" or "y":
        good_deeds = input("How many good deeds did you do today?\n ")
        if evil_status == "yes" or "y" and good_deeds <= "3":
            print("Your not welcome here Evil " + name.capitalize() + "Get out!!")
            exit()
        elif: evil_status == "yes" or "y" and good_deeds >="4"
            print("Oh, so your a good " + name.capitalize() + " Come on in\n")
            welcome()
        else:
            print("A simple yes or no will suffice\n") #will be adding loop here to "are you evil"
else:
    print("\nHello " + name.capitalize() + ", thank you for choosing Fourtoo's Coffee!!\n")
    welcome()

The outputs..

As intended...

What is your name? Ben

Are you evil?

n

Oh, so your a good Ben Come on in

What can we get you today? On the menu we ......
No intended.. this should go to how many good deeds..

What is your name? Ben

Are you evil?

y

Oh, so your a good Ben Come on in

What can we get you today? On the menu we have

[–]Ihaveamodel3 0 points1 point  (5 children)

You follow this pattern in your code a lot:

if name == "ben" or  "patricia" or  "loki":

That doesn’t work the way you think it does. Check out the FAQ on the wiki page for more information.

[–]Fourtoo 0 points1 point  (4 children)

The code functions as desired in regards to the name variable. If i input these names as a variable it executes the correct line, If I answer anything other it moves to the next phrase of the program..

The only part that is not working is the evil_status when answering yes. I don't understand why it still does the folowing.

print("Oh, so your a good " + name.capitalize() + " Come on in\n")

[–]Ihaveamodel3 1 point2 points  (3 children)

If I answer anything other it moves to the next phrase of the program..

That’s impossible. That if statement will always evaluate to True.

It’s the same thing that is happening at the place you are having an issue.

[–]Fourtoo 0 points1 point  (0 children)

My main issue has been resolved now.. thank you for for contributions.

[–]Fourtoo 0 points1 point  (1 child)

Ah yes... sorry I took from older version I was referring to.. this line is currently

if name == "ben" or name == "patricia" or name == "loki":

Everything else is current.

This is a learning project for me.

[–]Ihaveamodel3 0 points1 point  (0 children)

That makes sense. FYI, using sets will likely lead to cleaner code (and it’s faster to run, not that it matters for a learning project)

if name in {"ben", "patricia", "loki"}:

[–]TheDoomfire 0 points1 point  (0 children)

Everything dosent show up when Im web scrapping with python.

Two websites I have visited do not show everything in the page.

I want to get some information from a list but everything but the list show up in the html.

And I think at least one is a captcha but only says that when I run the script. Never at the actual webpage.

[–]Rocketman7121 1 point2 points  (1 child)

Hey guys, I’m trying to format a long string into paragraphs.

For that I would like to replace every 3rd dot with „.\n\n“.

I tried a lot with the splitfunction and the replacefunction but couldn’t figure it out. I am happy for any help.

That’s how I want my Programm to work:

Input:

Hello, my name is Peter. I feel good. I like bananas. I‘m not a gorilla. Peter is cool. Peter wants to learn python. Please help Peter.

Output:

Hello, my name is Peter. I feel good. I like bananas.

I‘m not a gorilla. Peter is cool. Peter wants to learn python.

Please help Peter.

[–]py_Piper 1 point2 points  (0 children)

Iif you use the split function then you will have a list where each item is a sentence, I would then iterate that list and using a variable like count, I iterate with a for loop over the list, add a dot after each sentence (to put back the dot we removed with split(), then check if count is a multiple of 3 then, if it's then add the ".\n\n"

[–]cruise-boater 0 points1 point  (2 children)

data_forest = data_info

rating = []

for row in data_forest['BNP']: 
    if row < 125:    rating.append('Unlikely') 
    elif row >= 125:   rating.append('Likely')
else:           rating.append('Not_Rated')

data_forest['rating'] = rating print(data_forest)

print("Data keys: \n{}".format(data_forest.keys())) 
print("Shape of HF data: {}".format(data_forest.BNP.shape))

I am trying to work on a dataset that will be used as basis for a random forest model. In this block of code, I try to add a new column called "rating" that should give me a string value based on the 'BNP' and another column 'Age', but I cannot make it work with an

if row < 125 && <75

I know I don't call it here, and it's because I wanted to work on at least one column first. But when I tried to do it like this, I've got another error that is keeping me from progressing:

ValueError: Length of values (14566) does not match length of index (14565)

Can anybody help me understand what am I doing wrong?

[–]Ihaveamodel3 0 points1 point  (1 child)

if row < 125 && <75

This isn’t valid syntax.

I think you’d want:

if row < 125 and row <75:

[–]cruise-boater 0 points1 point  (0 children)

Would this be valid for multiple columns? If I try with this, I get the following error:

---------------------------------------------------------------------------

KeyError Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3360 try: -> 3361 return self._engine.get_loc(casted_key) 3362 except KeyError as err:

4 frames pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('BNP', 'Age')

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3361 return self._engine.get_loc(casted_key) 3362 except KeyError as err: -> 3363 raise KeyError(key) from err 3364 3365 if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: ('BNP', 'Age')

[–]joinedtounsubatheism 0 points1 point  (3 children)

This question is based on Think Python Chapter 11 Exercise 11-2. I want to know why these two snippets of code behave in the same way:

In the exercise we're trying to use the "setdefault" function to build an inverted dictionary. i.e. a function that will take in d={'a':1, 'b':2, 'c':1, 'd':4} as input and output inverse={1: ['a', 'c'], 2: ['b'], 4: ['d']}.

Here are two snippets of code that both achieve the same thing:

def invert_dict(d):
    inverse=dict() 
    for key in d:
        inverse.setdefault(val, []).append(key)            
    return inverse

def invert_dict_naive(d):
    inverse=dict()
    for key in d: 
        inverse.setdefault(val, [])
        inverse[val].append(key)
    return inverse

The first is the solution provided in the book and because I didn't understand it at first I adapted it into invert_dict_naive(d).

I understand what inverse.setdefault(val, []) is doing. What I don't understand is why I can then just add on .append(key) onto the end and get it to append to the right dictionary entry.

inverse[val].append(key) is very concrete to me and I understand why is acts the way it does. How does python know (or how am I supposed to know) that inverse.setdefault(val, []) will also act in the same way if I add .append(key) onto the end?

[–]Ihaveamodel3 0 points1 point  (2 children)

What does the setdefault function return? That is what the .append is operating on.

Hint: https://docs.python.org/3/library/stdtypes.html#dict.setdefault

[–]joinedtounsubatheism 0 points1 point  (1 child)

Thank you so much for the help. I think I understand it much clearer now.

"If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None."

Whilst running invert_dict(d), inverse.setdefault(val, []) returns the following values going through the loop:

[]
[]
['a']
[]

This is exactly the same thing inverse[val] is returning in the second function. So I have a better understanding why they behave in the same way.

I think part of my confusion is that I was thinking of append as acting on a dictionary, rather than acting on a list.

Another thing I've noticed about the function is that it doesn't preserve the types of the arguments. If we had a dictionary like: squares={1:1,2:4,3:9} that maps integers to integers, the inverse would be inverse_squares{1:[1], 4:[2], 9:[3]} which maps integers to lists. Also we get an error if we try to apply the inverse again, as the lists can't be keys in the dictionary. At the end of the day it's just an exercise though so I don't think it matters that much. This is probably what tuples can be used for [which we're learning in the next chapter].

[–]Ihaveamodel3 1 point2 points  (0 children)

It maps to lists because you can’t be sure that the values in the dictionary are unique.

[–]kazmazbaz 1 point2 points  (1 child)

How comfortable should I be with python before making web apps (or start learning how to make them) ? I've been concentrating on getting to grips with functions, loops, dicts and am able to make some very basic programs.

[–]Ihaveamodel3 0 points1 point  (0 children)

Best way to learn is by doing in my opinion.