Changing a list during iteration while programming with python

The program I use at work uses Python as a scripting language. I am perplexed in my attempts to change the list while interacting with it. In addition, I have some logic inside the for loop that I would like to convert parts of the list from float to text. At the end, the entire text should be exactly 4 spaces. In my research to solve this problem, I came across numerous problematic problems for other people trying to change the list, iterate over it. However, I feel that my problem is unique because I am trying to change the list from float to text, iterating over it.

I really found work around the problem, but I feel that the solution was more complicated than necessary, and I am looking for a simpler solution, which is why I am here. I would like to improve the code.

Here is the code that is causing me problems:

#these come in as floats with a single decimal space, ex:99.9
val = [AvailibilityLine1, AvailibilityLine2,
       PerformanceLine1, PerformanceLine2,
       QualityLine1, QualityLine2]

for i in val:
    j = 0
    if i >= 100:
       val[j] = "100 "                                                           
    elif i >= 10 and i < 100:
       val[j] = str(val)
    elif i > 0 and  i < 10:
       val[j] = " " + str(val)
    elif i <= 0:
       val[j] = "    "    #insert 4 spaces
    else:
       val[j] = "    "    #insert 4 spaces if all else fails
    j=j+1
+3
source share
5 answers

This should work

AvailibilityLine1 = 1.9
AvailibilityLine2 = 45.9
PerformanceLine1 = 76.5
PerformanceLine2 = 99.9
QualityLine1 = 100.0
QualityLine2 = 0.0

#these come in as floats with a single decimal space, ex:99.9
val = [AvailibilityLine1, AvailibilityLine2, PerformanceLine1, 
       PerformanceLine2, QualityLine1, QualityLine2] 

j = 0
for i in val:
    if i >= float(100):
        val[j] = "100 "
    elif i >= 10 and i < 100:
        val[j] = str(val[j])
    elif i > 0 and  i < 10:
        val[j] = " " + str(val[j])
    elif i <= 0:
        val[j] = "    "    #insert 4 spaces
    else:
        val[j] = "    "    #insert 4 spaces if all else fails
    j = j + 1

print val

Output

>>> [' 1.9', '45.9', '76.5', '99.9', '100 ', '    ']

problems in your code:

  • Initialization of counter J must be outside the for loop
  • You assign a complete list (val) to the list index in the second and third if-else states
0
source

you can access and change the value of the list using the index as follows:

val= [1,2,3,4,5,6,50,110]
for i in xrange(len(val)):
    if val[i] >= 100:
       val[i] = "100 "                                                           
    elif val[i] >= 10 and val[i] < 100:
       val[i] = str(val[i])
    elif val[i] > 0 and  val[i] < 10:
       val[i] = " " + str(val[i])
    elif val[i] <= 0:
       val[i] = "    "    #insert 4 spaces
    else:
       val[i] = "    "    #insert 4 spaces if all else fails

print val
+1
source
def formatter(vals):
    def clamp(x):
        '''Fixes the values between 0 and 100'''
        return max(0, min(100, x))

    def string_formatter(x):
        '''Formats the values into a string with width 4'''
        if x == 100:
            return "{:<4g}".format(x)
        elif x == 0:
            return ' '*4
        else: 
            return "{:>4.1f}".format(x)

    return [string_formatter(clamp(i)) for i in vals]
0

format, . shoud . , , . , . :

fmtStr = lambda x: ' 100' if x >= 100 else ('    ' if x < 0 else '{:4.1f}')

, , . , , , , , . Pythonic - . , ...

vals    = random.random(103)*300 - 150
strVals = [fmtStr(i).format(i) for i in val]

, .

0

You can use enumerate and string formatting for this:

num_list = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
for _index, _value in enumerate(num_list):
    if val <= 0:
        _value = ""  # If value is 0 or lower, just change it to empty string
    elif val >= 100
        _value = 100  # If value is greater than or equal to 100.0, make it 100
    num_list[_index] = "{:>4}".format(_value)

>> num_list
>> [' 1.0', ' 2.0', ' 3.0', ' 4.0', ' 5.0', ' 6.0']

The format "{:<4}"determines the formatting of the text, <means alignment of the text on the left, and >means alignment on the right, and ^means its centering. 4defines the final length. It applies the preferred alignment and fills the rest with spaces. If you want to use the fill character instead of spaces, you can use it as

num_list[_index] = "{:*<4}".format(_value)

>> num_list
>> ['1.0*', '2.0*', '3.0*', '4.0*', '5.0*', '6.0*']

Where *is your fill character

0
source

All Articles