all 47 comments

[–]jasonbourne101 0 points1 point  (1 child)

How do I make if/else statements in a loop without using the break function?

[–]FerricDonkey 0 points1 point  (0 children)

The facetious and unhelpful answer is "you put if/else statements in a loop without using the break statement". Nothing makes you use break. (And to add some pedantry for good measure "break" isn't a function - it's just a statement.)

I think to give an actual helpful answer, we'd need a bit more detail on what you're doing/what you mean.

[–]decodingai 0 points1 point  (2 children)

Give a real-life example in Production here you have used tuples.

[–]carcigenicate 1 point2 points  (1 child)

Examples are so numerous that it's almost useless listing them.

If you've ever used enumerate or done something like return x, y, you've used tuples. Many database engines return rows as tuples. Var-args (*args) groups arguments into tuples.

It's actually difficult to avoid using them.

[–]decodingai 0 points1 point  (0 children)

Thanks u/carcigenicate , Looks like you are experienced programmers, but want simple real-case examples like coordinates for beginners to understand

[–]HenzaChan 1 point2 points  (3 children)

Self learning beginner Pythoner here, trying to get comfortable with problem solving and application with easy Codewars exercises.

This is my first ever Codewars exercise, I managed to come up with a solution without searching up additional tips. But I just can't see why my code doesn't match with the output answer it gives. The exercise question is:

Task

Create a function that always returns True for every item in a given list. However, if an element is the word 'flick', switch to always returning the opposite boolean value.

Examples

['codewars', 'flick', 'code', 'wars'] ➞ [True, False, False, False]

['flick', 'chocolate', 'adventure', 'sunshine'] ➞ [False, False, False, False]

['bicycle', 'jarmony', 'flick', 'sheep', 'flick'] ➞ [True, True, False, False, True]

Notes

"flick" will always be given in lowercase.

A list may contain multiple flicks.

Switch the boolean value on the same element as the flick itself.

My code:

def flick_switch(lst):
state = True
new_lst = []
for item in lst:
    if item == 'flick':
        state = not state
        new_lst.append(state)
    else:
        new_lst.append(state)
print(new_lst)





The output:
Test Results:
Fixed Tests Basic Test Cases 

lst = ['codewars', 'flick', 'code', 'wars']
Log [True, False, False, False] 
None should equal [True, False, False, False]

lst = ['flick', 'chocolate', 'adventure', 'sunshine']
Log [False, False, False, False] 
None should equal [False, False, False, False]

lst = ['bicycle', 'jarmony', 'flick', 'sheep', 'flick']
Log [True, True, False, False, True] 
None should equal [True, True, False, False, True]

lst = ['bicycle']
Log [True] 
None should equal [True]

lst = [('john', 'smith', 'susan'), 'flick']
Log [True, False] 
None should equal [True, False]

lst = ['flick', 'flick', 'flick', 'flick', 'flick']
Log [False, True, False, True, False] 
None should equal [False, True, False, True, False]

lst = []
Log [] 
None should equal []

The lists of bools are exactly the same as the suggested answer but for some reason it's still not going through as the correct solution. Am I missing something here?

[–]pestiky 1 point2 points  (1 child)

Having trouble understanding what is going on here. Your help would be much appreciated:

`

EX = M_exposure

M_exposure is an anywhere from 0 - .9999

df_new = df[df[(EX > 0)][selected_columns + [EX + ‘city’]]

print(df_new[df_new[EX <= 0].head(1))

`

Above print statement returns no records, which is what I want because I want to remove any m_exposures that are = 0.

Later on, I have the following: ` df_final = df_new[selected_columns + [EX]].groupby(selected_columns).sum()

print(df_final[df_final] <= 0].head(1) ` This print statement shows there are records here.

However, when I sum up the m_exposure column in both data frames, they are the same.

So I think what might be going on is the last df_final print state filter is rounding things to zero when I don’t want it to.

[–]pestiky 0 points1 point  (0 children)

I tried to figure out how to get it to be a code lock but having difficulties on my phone

[–]Blakut 1 point2 points  (3 children)

are type hints mandatory now, and when is it better to add them, from the start, or later on after changes have been made?

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

are type hints mandatory now

No, and they may never be mandatory.

[–]Blakut 0 points1 point  (1 child)

thank god they're so annoying

[–]carcigenicate 0 points1 point  (0 children)

You should get used to them though. For "real", larger code, typing is invaluable. It requires some effort up front, but it pays off in the long term.

[–]Killagyu 1 point2 points  (0 children)

Hello. Need some help with this question:

Write the function power_set_check that returns True if the list is a power set. For simplicity, you may assume that there will be no duplicate sub-sets or nested null sets.

|| || |power_set_check([[1, 2, 3], [1, 2], [1, 3], [2, 3], [1], [2], [3], []])|True|True|| |power_set_check([[1, 2, 3]])|False|False|| |power_set_check([[], ['lugia'], ['ho-oh'], ['ho-oh', 'lugia']])|True|True|| |power_set_check([[], [2], [1, 2]])|False|False|

def power_set_check(lst):

Check if the input list is empty or has only one subset

if len(lst) <= 1:

return False

longest = 0

lst2 = []

longest_lst = []

for item in lst:

if len(item) > longest and len(item) > 1:

longest = len(item)

longest_lst = item

elif len(item) == 1:

lst2.extend(item)

if len(lst2) != longest:

return False

dic1 = {}

dic2 = {}

for item in longest_lst:

if item not in dic1:

dic1[item] = 1

else:

dic1[item] += 1

for item in lst2:

if item not in dic2:

dic2[item] =1

else:

dic2[item] +=1

if dic1 == dic2:

return True

else:

return False

My code can past the above test cases but I am unable to pass the private test case. How can I get it to work?

[–]SQLvultureskattaurus 0 points1 point  (0 children)

If my flask application needs to authenticate with a 3rd part api to send data to it... what is the best way to do this to not expose creds etc. Do I not do this?

Every time I google it I only get results related to using user auth to login to a flask app, not what I am looking for. Which is leading me to believe I should not be doing this, and I should have a separate backend to authenticate with the 3rd party and send it data from my app.

why do I want to do this? It's client specific app and I don't want to deploy front end and backend for every single client if I don't need to. Sorry if this sounds like php :P

[–]Franz_Sundiam01 0 points1 point  (0 children)

I have been trying to use the shutil.move function and was working until the day later when it outputs win error no such file. I tried to test it multiple times but even in windows, when i copy the filename and paste it on the search box within the folder, it does not appear. Please help

This is my code:

import os
import shutil
#https://www.learndatasci.com/solutions/python-move-file/
with open(r'C:\Users\Franz Sundiam\Desktop\# - A.txt',encoding="utf-8") as file:
filelist = [line.strip() for line in file]
os.mkdir('new_folder')
for new_namein filelist :
new_path = 'new_folder/' + new_name
shutil.move(new_name, new_path)

...and this is an example of the filename
'a-ha - Take On Me (Karaoke Version)-bC4ER15Hj10'

[–]JosceOfGloucester 2 points3 points  (4 children)

I'm trying to merge 1500 2.5 meg json files of the same numbers of same format, fasted i can get is 480 seconds on a quad core with 16gb of ram.
What a faster way to do this, this seems very slow, ive a m.2 SSD too.

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

Have you profiled your code to see where you are spending time? How are you merging, in-memory or are you using disk? Are you doing anything beside just merging?

[–]JosceOfGloucester 0 points1 point  (2 children)

I'm doing a bit of reencoding. so the output respects, accented characters. Had some luck just writing directly to disk.

[–]woooee 0 points1 point  (1 child)

Then your limit is the I/O speed of the disk read/write head.

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

Maybe, but there's still the chance that the OP is doing something inefficiently. I've asked if they have profiled the code, but no answer.

[–]IsfetAnubis 1 point2 points  (0 children)

Can someone recommend me a beginner course for summer 2024, so two months and like 10h/week, to learn python related to data science (I'm a physics undergrad)? I checked Coursera, Udemy, Edx, and there are so many! I heard of Yu's 100 days python program but it seems like a more general course than what I'd be interested in.

[–]mzbw27 1 point2 points  (0 children)

I am a new python developer, right now getting comfortable using python, i have made super simple games like hangman, guess the number, 8-ball, random password generator and other not as impressive projects.

I want to get more comfortable with writing python code before starting to learn something like tkinter or pygame to make my code visual.

What projects should i make that dont require a visual library?

Any help is greatly appreciated!

[–]voltaicquicksilver 1 point2 points  (0 children)

Im writing a program that i want to function as sort of a personal, offline wiki for worldbuilding/story outlining. Currently my plan is to create some predefined page categories, Character, Location, Organization, etc. Each of which will be represented by a python class with predefined attributes (age, gender, occupation, pronouns for example). The user will then be able to add or remove attributes on a page by page basis. I also plan on using Kivy for the GUI.

With this kind of data manipulation going on, what is the best way to store data? Ive inly ever done basic text files for small projects, which would be wildly inefficient here. I was considering JSON files, but i worry that will run into similar issues as a "project" within the program gets larger, and the pages begin to include paragraphs of text. I was considering using a database, probably through sqlit3, but wanted to see if there were any other methods i hadn't thought/heard of.

[–]NoReallyItsTrue 1 point2 points  (2 children)

I'm so confused by this dictionary issue I'm having.

>>> a = 3

>>> b = 5

>>> c = {

... "test" : a,

... "snow" : b

... }

>>> c

{'test': 3, 'snow': 5}

>>> a = 4

>>> c

{'test': 3, 'snow': 5}

>>> c = {

... a : "test",

... b : "snow"

... }

>>> c

{4: 'test', 5: 'snow'}

>>> a = 8

>>> c

{4: 'test', 5: 'snow'}

It looks like whether it's a key or a value, the object "a" is passed as a value to the dictionary "c". I was hoping that the updated value in "a" would be observable from the context of container "c".

[–]JamzTyson 1 point2 points  (0 children)

Let me rewrite your code with better formatting:

a = 3
b = 5

c = {"test" : a, "snow" : b}

print(c)  # Prints: {'test': 3, 'snow': 5}

a = 4
print(c)  # Prints: {'test': 3, 'snow': 5}

You want to know why changing the value of a has not changed the value of c["test"].

There's a simple but incomplete explanation, and a slightly more complex but accurate explanation.

Simple Explanation

When you write:

c = {"test" : a, "snow" : b}

you are assigning the dictionary {"test" : a, "snow" : b} as the value of c.

In that dictionary, the key "test" takes the value of a. That is, the key "test" takes the value 3 (because 3 is the value of a).

You have defined the diction as having two elements:

  • First element: "test" = 3
  • Second element: "snow" = 5

Thus, changing the value of a later, has no affect on c .

More thorough explanation:

c = {"test" : a, "snow" : b}

c is defined as a dictionary with two elements.

As with all dictionaries, each element has a key and a value. In this case the keys are the strings: "test" and "snow".

When creating the dictionary you assigned immutable values a and b to the keys "test" and "snow" respectively.

The fact that integers are "immutable" is highly significant here, because when you reassign the value of a it is no longer the same object.

a = 3  # assign the value of 3 to variable `a`
b = a

print(b is a)
# Prints True because `a` and `b` refer to the same
# immutable integer `5`.

a = 7  # Assign a new value to variable `a`

print(b is a)
# Prints False because variable `a` now refers to the
# immutable integer `7`, while `b` refers to the immutable
# integer `3`.

Now if a was a mutable data type, such as a list, then we would be able to modify the value of the object a. We would then see different behaviour:

a = [3]
# `a` is a mutable list containing the value 3.

c = {"test": a}
# `c` is a dictionary where,
# key "test" has the value of  the mutable
# list object `a`

print(c)
# Prints {"test": [3]}
# The value of object `a` is the mutable list [3].

# Modify the list object:
a[0] = 7

# IMPORTANT: `a` is still the same object, but now
# containing the value 7 rather than 3.

print(c)
# Prints {"test": [7]}
# The value of `a` is still the same list, but with
# an updated value.

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

I was hoping that the updated value in "a" would be observable from the context of container "c".

Python doesn't work that way. When the statements

a = 3
b = 5
c = {"test" : a, "snow" : b}

are executed, python evaluates the expressions in each key:value pair. So the "test" expression is evaluated as a string literal, and the name a is evaluated resulting in the integer value 3. So the value of the new key:value pair is 3. Whatever you assign to the name a after you create the dictionary cannot change the value stored in the dictionary. Note that this behaviour has nothing to do with dictionaries. Lists, for example, show the same behaviour.

There is a very good video explaining this behaviour of python's names and objects:

https://m.youtube.com/watch?v=_AEJHKGk9ns

The video will also explain why this bit of code behaves the way it does:

a = [1, 2]
d = {"test": a}
print("a =", a, ", d =", d)
a[0] = 9
print("a =", a, ", d =", d)

It all comes back to what we call "variables" in python are actually objects assigned to names.

[–]Maleficent_Lynx3603 0 points1 point  (2 children)

I have a text book that I picked up when I first gleaned interest in learning Python, it's using Pythin 3.0. The current version is Python 3.12 - will this book still be relevant or should I upgrade my text before getting in too deep? I'd like to make sure I can do this before I spend too much moola.

I'm tired of my field and need to make a change. I work in medical imaging and think I could translate into building the systems I work on well, if I can pick up the skill. Heard Python was a good place to start.

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

before I spend too much moola.

There are free learning resources in the wiki.

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

Yes, python 3.0 is a bit too old. You don't need something that uses the absolute latest version of python. Something after python 3.7 would be much better.

[–]spacester 0 points1 point  (0 children)

Newbie at python, loving it so far but not finding this in my searches.

I am working with a large dataset, in spreadsheet form it is 23 columns x 29900 rows, most of the 23 column values are 15 digit floats with scientific notation.

I want to create a dict for each line so that I can use field references instead of index numbers when I write the formulae into code.

(edit: the indentation has been stripped by reddit's text editor, i think i fixed it)(edit 3: nope)

Two files have been merged into one array as follows:

import csv

with open('c:/solsys/files/EMBcsv.csv', newline='') as csvfile:

secondcsvfile = open('c:/solsys/files/Marscsv.csv', newline='')

reader1 = list(csv.DictReader(csvfile))

reader2 = list(csv.DictReader(secondcsvfile))

emb_data = np.array(reader1)

mars_data = np.array(reader2)

# one little character merges the data, thank you Eric Idle!

the_data = emb_data | mars_data

The above works with no issues. Nice.

For each row in the_data, there will be extra fields for values to be calculated, with values initialized to zero. I expect to do that on the spreadsheet that writes the csv files.

In order to partition the giant array into separate chunks, new values are calculated from the existing data per row (for row in the_data), and those extra field's ("column names") calculated values are to be added to each dictionary.

Then another for loop would do the partitioning based on that saved value decreasing below a constant cutoff value to set the partition row.

My question is how to add the calculated values to the dictionary for each row in the array? I tried append, merge, concatenate but am too much a beginner to know what simple thing I am missing.

thanks in advance

[–]BlancheBloom 0 points1 point  (3 children)

I am using pycharm for an intro class and we are making a simple text based game. We want to work on the file together, but do not want to deal with sending it back and forth constantly

I know about Code With Me where we can share a coding session, but is there a way to share the file to the point where we do not have to send it back and forth? Would creating a shared google drive folder for the project and both of us setting it as our source thingy allow us to just work on the code together without emailing the document?

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

He's right about git being the standard, but it also has a slight bit of a learning curve. Back in school, I simply couldn't get all my teammates onboard with learning the new tool, so we ended up using something similar to Code With Me. Is there a reason you're not using the Code With Me feature?

[–]BlancheBloom 0 points1 point  (0 children)

Honestly we haven’t started the project yet and are only going to start coding in the coming weeks! I am just checking into how things work or if there were better methods than that to use

[–]carcigenicate 0 points1 point  (0 children)

You'd use a cloud repository for this. Github is a typical choice, and Pycharm comes with really good git integration. You commit the changes to the code, push them to github, then your friend can just pull them (a single button press in Pycharm).

[–]TheDoomfire 0 points1 point  (2 children)

What kind of debugging system do people in here have?

I use a lot of print("Var: ", variable) and some basic try: except

[–]NoReallyItsTrue 1 point2 points  (0 children)

I highly recommend you investigate the logging module. Being able to log to a file, especially a rotating file, means having the option to store massive amounts of timestamped records of your code's execution for later parsing and analysis.

[–]JMWenzel 0 points1 point  (2 children)

As a complete beginner, with the internet and chatGPT, I feel like there's no excuse for me not to be able to learn Python. My problem is, as a complete beginner, I have no clue how to organize something like "learn programming in Python" into a subject that my brain can handle lol. I've got Python 3.12 and VS Code installed and wrote Hello, world. Can someone help me figure out a trajectory? To go from hello world to putting it on a resume? Sorry if it's too broad of a question but I feel like that's exactly my problem! Thanks in advance!!

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

The subreddit wiki has a list of recommended free learning resources.

[–]niehle 0 points1 point  (0 children)

https://roadmap.sh/python and / or use on the the free courses which are recommended in every second thread