-4

I have a situation where

if len(a) = 0
 a = ''
if len(b) = 0
 b = ''
if len(c) = 0
 c = ''

If len( any object) is zero , declare it as null. I have to keep doing this for all the alphabets . What is the best way to code it.

Additional information :

import test2
x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] # a b c are declared in test2 file as a = set()...b = set() ...
for y, z in enumerate(x):
 if len(getattr(test2, x[y])) >= 5:
 setattr(test2, x[y], '') = ''
 else:
 pass

Why am I not able to use setattr there?

asked Aug 1, 2017 at 20:44
7
  • 1
    If the length is 0 and it's a string, you already have the empty string. Commented Aug 1, 2017 at 20:46
  • "If len( any object) is zero , declare it as null" - this isn't declaring anything as null. What do you even think "declare it as null" means? Commented Aug 1, 2017 at 20:46
  • 4
    Why do you have 26 single letter variables to start with. Use a dictionary or list instead. Commented Aug 1, 2017 at 20:47
  • They are of type set() and when the set is empty I need it to declare them as null so that it doesnt print set() . Commented Aug 1, 2017 at 20:54
  • 1
    You don't need to "declare them as null". Wherever you're printing these sets, if they're empty, don't print them. (Also, seriously, don't use 26 single-letter variables.) Commented Aug 1, 2017 at 20:57

1 Answer 1

0

The below code exemplifies some horrible programming practices, but is my attempt at trying to understand your request (26 single-letter variables, really?).

a = "fshdj"
b = ""
c = "fshdj"
d = "fshdj"
e = ""
f = "fshdj"
g = "fshdj"
h = "fshdj"
i = "dj"
j = "fshdj"
k = "fshdj"
j = "fsj"
l = "fshdj"
m = "fshdj"
n = "fshdj"
o = "f"
p = "fshdj"
q = "fshdj"
r = " "
s = "fshdj"
t = "fshdj"
u = ""
v = "fshdj"
w = "fshdj"
x = "fshdj"
y = ""
z = "fshdjfshdj"
alpha = [a , b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
for i in alpha:
 if len(i) == 0:
 i = None
 print i

Try it here!

answered Aug 1, 2017 at 21:27
Sign up to request clarification or add additional context in comments.

8 Comments

Why does your test2 file define the letters are variables, but they are treated as string by your list in the provided code?
a -z are in a different file where I will have to import them. Thats the reason I was using getattr and setattr. i am not sure how to use if the variables are in other file. Please advice
What's the context of your problem? Is this some sort of homework assignment? If so, please update your post with all the information you have, regardless of what you think is useful. Right now, we don't know what you're trying to accomplish and what you have to do so.
Do you know what getattr and setattr do?
I have an file which has all the data( Records of members). I will need to read the data files and assign values to variables. Once the record is done i will have to write the record to csv and move to next record( Records is . not standard and the number of variables keep changing. In this process I will have to keep all the variables in one file so that I can write them into csv later on. Once I right to csv will clear the variables and reuse them for other record.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.