[Solved] How to get a list in below format? [closed]

Use strip and split as below to get the value separated as you want. file = open(‘file.txt’) my_arr = [] for line in file: fields = line.strip().split() my_arr.append([fields[0], “https://stackoverflow.com/”, fields[1][1:]]) print(my_arr) Output: [[‘22882367’, “https://stackoverflow.com/”, ‘pgc-orc-hive-output’], [‘13454914’, “https://stackoverflow.com/”, ‘pqs’], [‘2254110952’, “https://stackoverflow.com/”, ‘processed-nrt-export’]] 3 solved How to get a list in below format? [closed]

[Solved] If x = 2 y = 5 z = 0 then find values of the following expressions: a. x == 2 b. x != 5 c. x != 5 && y >= 5 d. z != 0 || x == 2 e. !(y < 10) [closed]

There are several differences between Java and Python. One of which is the use of ‘if’ statements. In python, an ‘if’ statement follows this structure: if CONDITION: elif CONDITION: else: The operators are also slightly different. Rather than || it’s or Rather than & it’s and Boolean works similarly, except python capitalizes the first letter. … Read more

[Solved] What does if __name__ == “__main__”: do in Python?

Short Answer It’s boilerplate code that protects users from accidentally invoking the script when they didn’t intend to. Here are some common problems when the guard is omitted from a script: If you import the guardless script in another script (e.g. import my_script_without_a_name_eq_main_guard), then the latter script will trigger the former to run at import … Read more

[Solved] python calculating average grade from a reading file

fin = open(“students”,”r”) for line in fin: line = line.split() if len(line) > 1: name = line[0] grades = map(float, line[1:]) mean = sum(grades) / len(grades) print( “{:<10}: {:>5.2f}”.format(name,mean) ) solved python calculating average grade from a reading file

[Solved] tokenizing and parsing with python

To retrieve the data from the file.txt: data = {} with open(‘file.txt’, ‘r’) as f: # opens the file for line in f: # reads line by line key, value = line.split(‘ : ‘) # retrieves the key and the value data[key.lower()] = value.rstrip() # key to lower case and removes end-of-line ‘\n’ Then, data[‘name’] … Read more

[Solved] Construct a game so that two random numbers appear and the user has to choose which one is bigger

The simplest thing you can do is ask for the value of the biggest number, and compare it to the biggest number: biggest = max(a1, a2) user_num = int(input(“What is the biggest number?”)) if user_num == biggest: print(‘Correct!’) else: print(‘Wrong! The biggest number is {}’.format(biggest)) Note the use of int() for converting the input to … Read more

[Solved] Break outside the loop

You use break when you want to break free from a loop, to exit the loop, to jump to the nearest code after the loop. Your code doesn’t contain a loop, so nothing to break free from, hence the error. solved Break outside the loop

[Solved] == vs. in operator in Python [duplicate]

if 0 in (result1, result2, result3): is equivalent to: if result1==0 or result2==0 or result3==0: What you want is this: if (0,0,0) == (result1, result2, result3): Which is equivalent to: if result1==0 and result2==0 and result3==0: You could actually even do this: if result1==result2==result3==0: since you’re checking to see if all 3 variables equal the … Read more

[Solved] one self for class in class

You only defined classes inside the namespace of class User. An instance of User don’t have magically instances of the inner classes (i.e. in Python there is nothing like a inner class). So class definitions inside other classes in normally not useful. You have to give the other classes an explicit reference to your user … Read more