[Solved] Using Python regex matches in eval()

You don’t need eval. In fact, you want to avoid eval like the plague. You can achieve the same output with match.expand: mystr=”abc123def456ghi” user_input1 = r'(\d+).+?(\d+)’ user_input2 = r’\2\1′ match = re.search(user_input1, mystr) result = match.expand(user_input2) # result: 456123 The example about inserting 999 between the matches is easily solved by using the \g<group_number> syntax: … Read more

[Solved] How do you train a neural network without an exact answer? [closed]

TLDR; Reinforcement learning In general, training agents uses reinforcement learning. It is different than what you explained, because it seems as if you want to define a fitness heuristic to tell the agent whether it is doing OK or not, which might be biased. Reinforcement learning also has biases, but they are researchedand studied. A … Read more

[Solved] Try to plot finance data with datetime but met error TypeError: string indices must be integers, not str

The following could be used to plot your data. The main point is that you need to specify the (rather unusual) format of the datetimes (“%Y/%m/%d/%H/%M”), such that it can be converted to a datetime object. import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv(“data/minuteData.csv”) df[“minute”] = pd.to_datetime(df[“minute”], format=”%Y/%m/%d/%H/%M”) plt.plot(df[“minute”],df[“spreadprice”], label=”spreadprice” ) plt.plot(df[“minute”],df[“bollup”], … Read more

[Solved] Sorting a list based on associated scores [closed]

I would approach this as follows: from collections import defaultdict # using defaultdict makes the sums easier correlations = defaultdict(int) # default to int (i.e. 0) for i1, i2, correl in strScoresDict: # loop through data correlations[i1] += correl # add score for first item correlations[i2] += correl # and second item output = sorted(correlations, … Read more

[Solved] Capitalization of the first letters of words in a sentence using python code [closed]

.split(sep): Specifically split method takes a string and returns a list of the words in the string, using sep as the delimiter string. If no separator value is passed, it takes whitespace as the separator. For example: a = “This is an example” a.split() # gives [‘This’, ‘is’, ‘an’, ‘example’] -> same as a.split(‘ ‘) … Read more

[Solved] Creating text files in a python script

If your professor is running on Linux, Unix or Mac they you can just run the python file by a) ensuring it starts with a comment reading: #!/usr/bin/env python and then setting the file as executable with chmod +x scriptname.py If he/she is running on windows either he/she will have to install python, (then they … Read more

[Solved] How do you make this shape in Python with nested loops?

for row in range(6): # this outer loop computes each of the 6 lines line_to_print=”#” for num_spaces in range(row): # this inner loop adds the desired number or spaces to the line line_to_print = line_to_print + ‘ ‘ line_to_print = line_to_print + ‘#’ print line_to_print prints this output: ## # # # # # # … Read more

[Solved] How to check pep8 standards in my code [closed]

The term for this is “linting”. A python module called Pylint is available. It checks for coding standards and errors with full customizability. It can be run from the command line, as part of a continuous integration workflow, integrated into various IDEs. 6 solved How to check pep8 standards in my code [closed]

[Solved] Define a function count_down() that consumes a number a produces a string counting down to 1 [closed]

Not a code-writing service nor a tutorial. But for its simplicity’s sake, a possible solution could look like def count_down(n): return ‘, ‘.join(str(i) for i in range(n, 0, -1)) # return ‘, ‘.join(map(str, range(n, 0, -1))) >>> count_down(5) ‘5, 4, 3, 2, 1’ join, str, range are very fundamental functions/methods you should read up on … Read more

[Solved] Can´t join a list the way I would like

str converts the list into its string representation. Use map to convert each number in the list to str: lista = [1,2,3,4,9,5] print(“<“.join(map(str, sorted(lista)))) >>> 1<2<3<4<5<9 0 solved Can´t join a list the way I would like