Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use a list of floats
#11
When I tried append() it said I can't do that to a tuple. So not a list. But, I still get a float error when trying enumerate().

I'm trying simplify the code to less lines.
Reply
#12
We assume that 'total' is defined as a list. How becomes a tuple?


total = []
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#13
I'm not sure. I borrowed the code for converting bytes to MB. There was no total=[] outside of it, but when I add it in, I still get the float error. Below, total and s are the same, but both give errors.

    for i in xrange(len(filepaths)):
        filepaths[i] = (os.path.getsize(filepaths[i]))
        nums = filepaths[i]
        i = int(math.floor(math.log(nums, 1024)))
        p = math.pow(1024, i)
        s = round(nums/p, 2)
        total = s
        print total
Reply
#14
You try to iterate a single value. Doesn't matter where you've got the code from
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#15
As and addition to what @wavic say,there are bad stuff in this code.
Never use range(len(something),not gone give up on this Dodgy
Also overwrite of the original filepaths,you are getting back byte size so do not call it filepaths.
So look at this:
# Bad
import os

filepaths = ['E:/1py_div/uni.txt', 'E:/1py_div/h.py', 'E:/1py_div/cloud9.txt']
for i in range(len(filepaths)):
    filepaths[i] = os.path.getsize(filepaths[i])

print(filepaths) # [22, 190, 42043]
# Good
import os

filepaths = ['E:/1py_div/uni.txt', 'E:/1py_div/h.py', 'E:/1py_div/cloud9.txt']
file_byte_size = []
for index,item in enumerate(filepaths):
    file_byte_size.append(os.path.getsize(item))

print(file_byte_size) #[22, 190, 42043]
Ahh come to think of this,it's easy to forget the the easiest solution.
# Best
import os

filepaths = ['E:/1py_div/uni.txt', 'E:/1py_div/h.py', 'E:/1py_div/cloud9.txt']
file_byte_size = []
for item in filepaths:
    file_byte_size.append(os.path.getsize(item))

print(file_byte_size) #[22, 190, 42043]
list comprehension is also okay.
import os

filepaths = ['E:/1py_div/uni.txt', 'E:/1py_div/h.py', 'E:/1py_div/cloud9.txt']
file_byte_size = [os.path.getsize(item) for item in filepaths] #[22, 190, 42043]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  When is it safe to compare (==) two floats? Radical 4 649 Nov-12-2023, 11:53 AM
Last Post: PyDan
  floats 2 decimals rwahdan 3 1,588 Dec-19-2021, 10:30 PM
Last Post: snippsat
  rounding and floats Than999 2 3,045 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  int, floats, eval menator01 2 2,402 Jun-26-2020, 09:03 PM
Last Post: menator01
  Stuck comparing two floats Tizzle 7 2,956 Jun-26-2020, 08:23 AM
Last Post: Tizzle
  rounding floats to a number of bits Skaperen 2 2,269 Sep-13-2019, 04:37 AM
Last Post: Skaperen
  comparing fractional parts of floats Skaperen 4 3,264 Mar-19-2019, 03:19 AM
Last Post: casevh
  Integer vs Floats Tazbo 2 2,841 Jan-09-2019, 12:06 PM
Last Post: Gribouillis
  Formatting floats Irhcsa 6 4,067 Oct-04-2018, 04:23 PM
Last Post: volcano63
  How do you sort a table by one column?? (of floats) sortedfunctionfails 3 12,197 Jan-11-2018, 09:04 AM
Last Post: sortedfunctionfails

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020