Put a set of characters into list in python [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Essentially I want to put characters inside a list so I can make the for loop work.
Example characters (supposedly listed individually vertically. not like this showing as one line)
N/A
N/A
None
Test
Dev
I want to put these characters into a list like below
a = [N/A, N/A, None, Test, Dev]
Reason is when I use for loop into these characters (let's say there are all inside "test_report". For loop is not working because it is reading the "test_report" as like this:
N
/
A
N
/
A
N
o
n
e
count = 0
for i in test_report:
if i == "Test":
count += 1
return count

Try this...
with open('test_report.txt', 'r') as f:
test_arr = f.read().split()
print(test_arr)
Considered your test_report.txt like this...
N/A
N/A
None
Test
Dev

Related

Merge two consecutive rows based on condition [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to merge two consecutive rows. If there is "SMN" in Pos column followed by B-PER then merge the two rows. If SMN is not followed by B-PER then don't merge
This is the desired result that I want to achieve
for i in range(1, df.shape[0]):
if df['Pos'][i] == 'B-PER' and df['Pos'][i-1] == 'SMN':
newString = df['names'][i - 1].strip() + " " + df['names'][i].strip()
df['Pos'][i-1] = newString
df = df.drop(index=i)
This should work. Not sure the datatype of Arabic strings, so you might have to play with python datatypes to get this to work.

convert multiple columns vertical text to horizontal [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I tried these, but its only for one column.
using notepad++ and python. Any solution that can do for multiple columns of text?
# Input:
tim
oso
d n
a d
y a
y
# Output:
today
is
monday
You can do this fairly easily with a loop, something like:
s = "tim\noso\nd n\na d\ny a\n y"
bits = s.split('\n')
lines = ['' for i in range(0, len(bits[0]))]
for bit in bits:
for i in range(0, len(bit)):
if bit[i] != ' ':
lines[i] += bit[i]
'\n'.join(lines)

Python index error when trying to make a mean [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm trying to make an average but for some reason when I try to make one it doesn't work.
I have global variables and array defined at the begining of my document :
vent_moyenne_km = []
compteur_moyenne=0
I have one of my function that is called every X time. In that one, I calculate a velocity with some value that are display on a label of my interface. that part is working, but not the mean
global compteur_moyenne
compteur_moyenne += 1
ventkmh = (vent_1[3][0]*256 + vent_1[4][0]) /100 *3.6
label_vent2_2.config(text= "%.2f" % ventkmh)
vent_moyenne_km.append("%.2f" % ventkmh)
vent_1.clear()
if compteur_moyenne == 5:
compteur_moyenne = 0
print(vent_moyenne_km)
label_vent4_2.config(text=statistics.mean(vent_moyenne_km))
vent_moyenne_km.clear()
of course in my imports I have :
import statistics
When I comment the line label_vent4_2.config(text=statistics.mean(vent_moyenne_km)), everything works and I see in the terminal my array with 5 values. I also tried numpy and even tried to make a for items in array: then add then manually, and everytime I get the error : class 'IndexError'
I'm really not sure how to fix that.
For calculating an average of a list just use numpy:
def function():
value = random.randint(0,1)
return value
list = []
for i in range(100):
list.append(function())
if i%5 == 0:
print(np.average(list))

Get specific words from a string in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to to extract some information from a data file. The following is the format I have in my data file:
44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)
From this line, I want to extract 256 which is the last number and 4 which is the first number from
(4,0x00002aab 4c000c00)
Could you please recommend some functions which will be useful for my case?
You should use str.split().
What it does is split the string every place there is a space, so you would get a list of strings like so:
n = '44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)'
o = n.split()
print o
Output:
['44', '2.463181s>', '(G)', 'GET_NBI:', '0x00002aaa', 'ecc00e90', '<-', '(4,0x00002aab', '4c000c00)', '(256', 'bytes)']
Then simply get the second-to-last index like o[-2] -> '(256'
Remove the extra parenthesis: '(256'[1:] -> '256', and If you wanna, turn it into an integer. int('256') -> 256
You could also use regular expressions, which in this case might be a bit more clear.
import re
txt = "44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)"
results = re.findall(r"\((\d+)", txt)
# ["4", "256"]

How to count frequencys in python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I had a file called 'words.txt' which contains things such as. #+=%£%= and i need to go through and count each symbol without allowing any duplicates and then print my answer. for example it should look like this when printed:
# : 1
+ : 1
= : 2
% : 2
£ : 1
I know I need to use a for loop in this and that I need to use a set so It doesnt't allow any duplicates but how would I do it? Thanl you
A set isn't so useful here as it doesn't have a place to store the counts
from collections import Counter
with open("words.txt") as fin:
c = Counter(fin.read())
for item in c.items():
print("{} : {}".format(*item))
Use python dictionary:
symbols = {}
for c in string:
if c not in symbols:
symbols[c] = 0
symbols[c] += 1
Look up dictionary.
Don't want to post more as it would only spoil the exercise.

Categories