1

I'm trying to solve the following problem: I have a file which is build like this:

adsbhad 2.2 3.2 5.2 3.2 7.2
gsdg 1.2 2.2 5.7 8.2 10.2 
sdgghhad 1.2 0.2 3.2 2.2 5.2 
adsdfhad 0.2 1.2 5.2 8.2 12.2 
adrzertzd 1.2 13.2 2.2 10.2 9.2

I want to write a script which checks if the values in the lines are only getting bigger and then extract those lines. In this case the desired lines are line 2 and 4:

gsdg 1.2 2.2 5.7 8.2 10.2
adsdfhad 0.2 1.2 5.2 8.2 12.2

I tried to use for loops and if statements but it doesn't work. It seems that there are some type-issues. Here is my code so far (please be kind because I'm a beginner!):

Data=raw_input("Please type name of input data! ")
Data2=open(Data)

new_list=list()

for line in Data2:
    line2=line.rstrip()
    first_empty=line2.find(" ")
    line3=line2[first_empty+1:]
    List_of_line=line3.split()
    express=0
    for i in List_of_line:
       flo_i=float(i)
       express=float(express)
       if express == 0 or flo_i > express:
          express=flo_i
          express=str(express)
          if express == List_of_line[4]:
             print List_of_line

I also tried to replace the list-for loop with if statements but this doesn't work and I don't know why. I also converted to float within the if statements but there was no change.

I thought this is easy to solve but it is not (well, for me) and so I hope I get some help. Thanks in advance.

Tobias
  • 564
  • 3
  • 13

4 Answers4

3

A simple approach: a list is sorted if the sorted list is identical with the initial list and it is sorted ascending if first value is lower than the last. The case for only 1 value in the list is not accounted for.

...
for line in Data2:
    items = [float(n) for n in line.split()[1:]]
    sorted_items = sorted(items)
    if items == sorted_items and items[0] < items[-1]:
        print line
AllBlackt
  • 710
  • 6
  • 9
2

You are not that far off here but wandered off into the weeds. You needed a read though the build-in functions (these are more or less the same by python version, I use 2.7).

Here is the solution that I used, I'm sure better exist. I removed the keyboard input for the example

new_list=list()

with open('data.txt') as Data2:
    for line in Data2:
        line2 = line.split()[1:]
        line2 = [float(v) for v in line2]
        if line2 == sorted(line2):
            new_list.append(line.rstrip())

print new_list
#['gsdg 1.2 2.2 5.7 8.2 10.2', 'adsdfhad 0.2 1.2 5.2 8.2 12.2']

A couple things this does is introduce the with statement that closes the file for you after moving on. It is good practice to close files. Then I used split() same as you. Then the list comprehension to make the values into floats from strings. This then means that you can just compare the values to the sorted versions of the values, if they match they are sorted.

Community
  • 1
  • 1
Brian Larsen
  • 1,740
  • 16
  • 28
1

I think you have the right idea. The inner loop can be replaced by leveraging a lot of functionality built in to python. Here is my solution based on the code you've provided.

data_file = raw_input("Please type name of input data! ")
data = open(data_file)

new_list = list()

for line in data:
    line = line.rstrip()
    first_empty = line.find(" ")
    items_whole = line[first_empty + 1:]
    items_list = items_whole.split()
    # Use a 'list comprehension' to convert the items list into a list of floats
    floats_list = [float(i) for i in items_list]

    # Use the built in 'sorted' method to sort the floats
    # If the sorted float list is the same as the original float list, then all the items are ascending
    if floats_list == sorted(floats_list):
        print items_list
        # Add the list of floats into the 'new_list' for any post processing
        new_list += [floats_list]
0

Considering a simple test case:-

>>> s = 'adsbhad 2.2 3.2 5.2 3.2 7.2'
>>> l = map(float, s.split()[1:])
>>> l
['2.2', '3.2', '5.2', '3.2', '7.2']
>>> all(a < b for a, b in zip(l, l[1:]))
False  #this needs to be True for valid scenarios
Barun Sharma
  • 1,452
  • 2
  • 15
  • 20