Why this Python "loop code" does not work? - python

Why this code does not work?
ffi5_1 = pd.read_csv('/Users/d/bm_ffi5_1.csv')
ffi5_2 = pd.read_csv('/Users/d/bm_ffi5_2.csv')
ffi5_3 = pd.read_csv('/Users/d/bm_ffi5_3.csv')
ffi5_4 = pd.read_csv('/Users/d/bm_ffi5_4.csv')
ffi5_5 = pd.read_csv('/Users/d/bm_ffi5_5.csv')
s_list = list(range(1,6))
for x in s_list:
ffi5_x.jdate = pd.to_datetime(ffi5_x.jdate)
Here jdate is the column of dataframe.

Your code probably fails with a message that you attempt to refer to
a non-existing variable ffi5_x.
In order to replace x in the DataFrame name with the current value
of x - loop control variable (in 2 places), change your loop to:
for x in s_list:
exec('ffi5_' + str(x) + '.jdate = pd.to_datetime(ffi5_' + str(x) + '.jdate)')

Related

My function returns None when the variable I'm returning has a string associated to it

database_id = []
def makeID(name, count=1):
y = None
x = str(name + str(count))
if x in database_id:
count = count + 1
makeID(name, count)
if x not in database_id:
y = str(name + str(count))
database_id.append(y)
if y != None:
count = 1
return y
I am running this:
print(makeID('Eric'))
and I received None.
But when I print the database_id, it shows the list, ['Eric1'].
The variable y is also the string 'Eric1'.
I know it's not the most efficient but once it works fundamentally then I can make it more efficient.
In this case it is returning None, because when you already have your name in your database_id, the first execution of your recursive method won't set the local y variable to your return value. Here's an example :
Let's say database_id already contains 'eric1'.
makeID('eric') -> x is contained in database -> makeID will be recursively called with (name, count=2) that will return y who is equal to to eric2 but it won't be catched by anything.
So in short, you should add a return at line 8.
And if you are interested here's a quick way to refactor your method to keep it recursive :
def makeID(name, count=1):
unique_name = str(name + count)
if unique_name not in database_id:
database_id.append(unique_name)
return unique_name
else:
return makeID(name, count+1)enter code here
In your condition if x in database_id: you never assign y meaning your condition if y != None: is False and does not execute (you have a return statement in only that condition) essentially returning None. Furthermore you don't need to use recursion for such problems, you can easily solve it iteratively:
database_id = []
def makeID(name):
count = 1
while name + str(count) in database_id:
count += 1
user_id = name + str(count)
database_id.append(user_id)
return user_id

How can I fix SyntaxError: invalid syntax when trying to pad zeros to a numeric string?

for i in range(1,50):
path = if i < 10:
url + '0' + str(i)
else:
url + str(i)
df = pd.read_html(path)
in this situation, I got
SyntaxError: invalid syntax for 'if'.
how can I fix this code?
Keep it simple and explicit and just do:
if i < 10:
path = url + '0' + str(i)
else:
path = url + str(i)
Or, use Python's string formatting capabilities to create your string. If you want a zero-padded string with a minimal length of 2 characters, you can use the following format:
>>> a = 3
>>> f'{a:0>2}'
'03'
>>> a = 33
>>> f'{a:0>2}'
'33'
>>> a = 333
>>> f'{a:0>2}'
'333'
You actually want to "reformat" the path, converting i to a zero-padded string.
So the most natural way is to use just the zero-padded formatting, accessible
among others in f_strings. Something like:
for i in range(1,50):
path = url + f'{i:02}'
# Do with your path whatever you wish
If you want to use if statement in assignment you can do the following:
path = url + '0' + str(i) if i < 10 else url + str(i)
thus your code inside the loop will be like the following:
for i in range(1,50):
path = url + '0' + str(i) if i < 10 else url + str(i)
df = pd.read_html(path)
...
There is an another approach for your goal. You need to zero pad the number to make it 2 characters, so you can use zfill method of str class.
for i in range(1,50):
padded_i = str(i).zfill(2)
path = '{url}{id}'.format(url=url, id=padded_i)
df = pd.read_html(path)
...
You can use traditional way as well but it's not sweet as the previous ones:
for i in range(1, 50):
if i < 10:
i = '0' + str(i)
else:
i = str(i)
path = url + i
df = pd.read_html(path)
...

Removing from a list without affecting a dummy variable

from random import choice
inputs=['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','1','2','3','4','5','6','7','8','9','0']
func={}
code=""
z=len(inputs)
x=z-1
temp=inputs
while x>=0:
y=choice(temp)
print(str(x)+" "+inputs[x]+" "+y)
func[inputs[x]]=y
code=code+inputs[x]+y
del temp[x]
x=x-1
print(temp)
print(inputs)
Why does this code not asign every element of inputs to a unique and random element of inputs(as the temp dummy set)? it seems to delete items from both temp and inputs when only told to delete items from the dummy set.
Thanks for any help.
You are not making a copy of 'inputs' when you do 'temp=inputs', but making a new variable to access the same content. If you want a new copy of the list, then use 'temp = inputs[:]'. Otherwise you are just creating a new reference to the same object, but not duplicating the object itself.
You can find more about this in the official Python FAQ.
You are creating an alias of your list instead of a true copy of it:
replace temp=inputs with temp=inputs[:]
import random
inputs = ['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','1','2','3','4','5','6','7','8','9','0']
func = {}
code = ""
z = len(inputs)
x = z-1
temp = inputs[:] #<-- here
while x >= 0:
y = random.choice(temp)
print(str(x) + " " + inputs[x] + " " + y)
func[inputs[x]] = y
code = code+inputs[x] + y
del temp[x]
x = x - 1
print(temp)
print(inputs)

Python: Calculating difference of values in a nested list by using a While Loop

I have a list that is composed of nested lists, each nested list contains two values - a float value (file creation date), and a string (a name of the file).
For example:
n_List = [[201609070736L, 'GOPR5478.MP4'], [201609070753L, 'GP015478.MP4'],[201609070811L, 'GP025478.MP4']]
The nested list is already sorted in order of ascending values (creation dates). I am trying to use a While loop to calculate the difference between each sequential float value.
For Example: 201609070753 - 201609070736 = 17
The goal is to use the time difference values as the basis for grouping the files.
The problem I am having is that when the count reaches the last value for len(n_List) it throws an IndexError because count+1 is out of range.
IndexError: list index out of range
I can't figure out how to work around this error. no matter what i try the count is always of range when it reaches the last value in the list.
Here is the While loop I've been using.
count = 0
while count <= len(n_List):
full_path = source_folder + "/" + n_List[count][1]
time_dif = n_List[count+1][0] - n_List[count][0]
if time_dif < 100:
f_List.write(full_path + "\n")
count = count + 1
else:
f_List.write(full_path + "\n")
f_List.close()
f_List = open(source_folder + 'GoPro' + '_' + str(count) + '.txt', 'w')
f_List.write(full_path + "\n")
count = count + 1
PS. The only work around I can think of is to assume that the last value will always be appended to the final group of files. so, when the count reaches len(n_List - 1), I skip the time dif calculation, and just automatically add that final value to the last group. While this will probably work most of the time, I can see edge cases where the final value in the list may need to go in a separate group.
I think using zip could be easier to get difference.
res1,res2 = [],[]
for i,j in zip(n_List,n_List[1:]):
target = res1 if j[0]-i[0] < 100 else res2
target.append(i[1])
n_list(len(n_list)) will always return an index out of range error
while count < len(n_List):
should be enough because you are starting count at 0, not 1.
FYI, here is the solution I used, thanks to #galaxyman for the help.
I handled the issue of the last value in the nested list, by simply
adding that value after the loop completes. Don't know if that's the most
elegant way to do it, but it works.
(note: i'm only posting the function related to the zip method suggested in the previous posts).
def list_zip(get_gp_list):
ffmpeg_list = open(output_path + '\\' + gp_List[0][1][0:8] + '.txt', 'a')
for a,b in zip(gp_List,gp_List[1:]):
full_path = gopro_folder + '\\' + a[1]
time_dif = b[0]-a[0]
if time_dif < 100:
ffmpeg_list.write("file " + full_path + "\n")
else:
ffmpeg_list.write("file " + full_path + "\n")
ffmpeg_list.close()
ffmpeg_list = open(output_path + '\\' + b[1][0:8] + '.txt', 'a')
last_val = gp_List[-1][1]
ffmpeg_list.write("file " + gopro_folder + '\\' + last_val + "\n")
ffmpeg_list.close()

while loop error in Python

I'd appreciate some help debugging this code:
testing = """There is something unique about this line
in that it can span across several lines, which is unique and
useful in python."""
listofthings = []
i = 0
while i < len(testing):
if testing[i] == " ":
listofthings.append(i + 1)
i = i + 1
listofthings.insert(0, 0)
listofthings.append(len(testing))
print listofthings
word_list = []
i = 0
while i < len(listofthings):
l = i + 1
x = listofthings[i]
y = listofthings[l]
word = testing[x:y]
word_list.append(word)
i = l
print word_list
I am not sure why I am getting the index out of range error. I understand what the error means obviously, but am not sure what I am doing wrong. Weirdly enough, this only happens when I run the above code. It doesn't give me any errors when I run this:
word = testing[x:y]
print word
I am fairly new with Python(going on three days) so I am sure it is a stupid overlooked syntactical error...
l = i + 1
x = listofshit[i]
y = listofshit[l]
word = testing[x:y]
word_list.append(word)
When i=length-1,then y=length, which is an error.Python array indexing starts from 0, hence max address is length-1
The length of list listofshit is 21 with the range of index from 0 to 20. And when it comes to the final loop, i is 20 and l is 21, so there is a out of range error. And I think the following code is what you want:
testing = """There is something unique about this line
in that it can span across several lines, which is unique and
useful in python."""
listofshit = []
i = 0
while i < len(testing):
if testing[i] == " ":
listofshit.append(i)
i = i + 1
listofshit.insert(0, 0)
listofshit.append(len(testing))
word_list = []
i = 0
while i < len(listofshit) - 1:
l = i + 1
x = listofshit[i]
y = listofshit[l]
word = testing[x:y]
word_list.append(word)
i = l
print word_list
while i < len(listofshit):
l = i + 1
x = listofshit[i]
y = listofshit[l]
When i corresponds to the last element,
y = listofshit[l]
You are trying to access the element next to the last element. Thats why it is throwing the error.
On the last iteration of the second while loop, l is set to len(listofshit). This is past the end of listofshit; the last valid index is len(listofshit) - 1.

Categories