Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I have a list Let's say
m = [["'ghvghvgh hgdghdh', 'hxjhsdhb.com - Error 404:validation', 'jhhscbhjbcsd', 'hghs'"],["'ghvh', 'hxjhsdhb', 'jhhcsd', 'hs'"]]
and I want my output something like this (removing the double quotes).
m = [['ghvghvgh hgdghdh', 'hxjhsdhb.com - Error 404:validation', 'jhhscbhjbcsd', 'hghs'],['ghvh', 'hxjhsdhb', 'jhhcsd', 'hs']] ```
m[0][0].replace('"', '') # I have tried this
To normalise the data, you need to replace the single quotes and not the double-ones the trim away extra spaces.
lst = [
i[0].replace("'", '').strip().split(', ')
for i in m
]
import re
a=re.compile('[a-zA-Z]+')
m = [["'ghvghvgh', 'hxjhsdhb', 'jhhscbhjbcsd', 'hghs' "],[" 'ghvh', 'hxjhsdhb', 'jhhcsd', 'hs'"]]
res= [[a.findall(i) for i in j][0] for j in m]
print(res)
output
[['ghvghvgh', 'hxjhsdhb', 'jhhscbhjbcsd', 'hghs'], ['ghvh', 'hxjhsdhb', 'jhhcsd', 'hs']]
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How can I split a 'time' value into two 'day' 'time' lists?
See attached image:
If you have it as datetimeobject:
datos['day'] = dados['time'].dt.date
datos['time'] = dados['time'].dt.time
If you have it as string object:
datos['day'] = dados['time'].str[:11]
datos['time'] = dados['time'].str[11:]
Or
data[['day', 'time']] = data['time'].str.split(' ').apply(pd.Series)
data[['day', 'time']] = data['time'].str.split(' ', expand=True)
Or using regex
data[['day', 'time']] = data['time'].str.extract('(.*) (.*)')
To convert it to string:
datos['time'] = dados['time'].astype(str)
It is better then converting to normal list [str(x) ...]
To convert it to datetime
datos['time'] = pd.to_datetime(dados['time'])
It may use options - ie. yearfirst=True, dayfirst=True, format="%Y-%m-%d %H:%I:%S"
I use this for resolve the problem, is there any cleaner way?
dia = [str(x) for x in dados.time]
hora = [str(x) for x in dados.time]
for x in range(len(dia)):
dia [x]=dia [x][0:11]
hora[x]=hora[x][11:20]
dados.insert(0, 'dia', dia)
dados.insert(1, 'hora', hora)
dados.drop(columns='time',inplace=True)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I've done a function to generate a list of passwords, but it returns a pointer instead of a value:
#!/usr/bin/python3
import sys
import string
from random import *
characters = string.ascii_letters + string.digits
def generator():
password = "".join(choice(characters) for x_num in range(randint(18, 19)))
return password
def pass_list(num_of_passwd):
paswd_list = []
for index in range(num_of_passwd):
paswd_list.append(generator())
passwords = pass_list
print (passwords)
why is this happening?
This should work for you:
#!/usr/bin/python3
import sys, string, random
def generator(chars):
password = "".join(random.choice(chars) for _ in range(random.randint(18, 19)))
return password
def pass_list(chars, num_of_passwd):
pass_list = [generator(chars) for _ in range(num_of_passwd)]
return pass_list
characters = string.ascii_letters + string.digits
passwords = pass_list(characters, len(characters))
print(passwords)
Output:
['R63qhL4pzkwyb4DxyJ', 'iStYwujZ9hbGhKhCvr', 'CawJ3qAdqSL0Bf8phZ', '48PPQj8btdxaqshDkk', '318rubpkcR5mOrPMYOT', 'iOkINL0TkjL4sq9Mc3', 'Fw64Tj5KyNVNbJRYLD', 'voALiyI3vn70F3H7Ua6', 'ofAGywZqrEMcJkRwjWL', 'wMlYVakjZzz6Sm04jcv', 'PbPT2Hr9nR0eLhX0aKj', 'Fqa3oGBFj4HmdJR8C3', 'Wva99eZyXZZHaKrwrlT', 'kC7kRHuDYx9njsttQG', 'r5IQZF61lLRowiXQ6e9', 'Lh50xONU63ftvNUoAY', 'ByaxlBt8qVB9RbdQZz', 'b6VNghr4mhT4wfNGec', 'UB3qnHHdEF9OEvk5BHc', 'yA3j7sH4oClb5EybDN', 'E9ejvelWBUFVWCfAKL', 'ladx1niGhZv8bGoghY', 'E2EerTp7sMN6VBAV7Q', 'IxELC2nB5f4zCinvjK', 'DqF38rzrcihfFJwCJN', 'YeakZZHZKRAIM3Bt1pE', 'oJrLBQhyepaF1LiBfZJ', 'gNsgLR9Berqowg6CII', 'hLldZQ9MqxYySQ9Kti', 'mzGE71SiVHqdIbxDMwh', 'qkfAXnEWUkkxPTvaL3', 'FUx1xoKGFpDSM1SB8A', '0vH3AePPLAmbJonqTP3', 'thf80WN1JK4WdtrEySk', '9GlqeSobGMND6LzqYU', 'CqPPeqyAjM1YVjiN3UF', 'gdAHZdIxYSgdBCRli3', 'i2ltodn3npecEjV6bXu', 'ejSBXWalzp6QvQFzuE', 'usnv3Wwb4drwX6KUIkg', 'f24RszgtS8KyNE3A8u', 'lVYyoQoR9QVmUC4F7M', '5avz5UvQqMjC2oCPCCs', '1yVSXljlVqx83pCuPdo', 'sUkmkR2Otb89YFPtRQ', 'sVOnTTVBhr3ss6JjFi', 'o3ZIR1aRd579G6YPrBI', 'PP1WxXisQqYfB0OSYz', 'o6qcvRrAOqJDvf5HxB', 'UlrdaaIR3QVU8haF3qn', 'MrfKJcbNq4GjuPUPkNF', 'vU3aWCQFSpAVzZBp5E', 'ZzpLG4bqLrjENf8Uv6t', 'cy15Fqtw9rCKoOtCuN', 'Y7Iwjr9BjoDcMuz9l9', 'hsdc6B45lgtgsbD5fux', 'uZPp7buh4faAJSymD4V', 'u15I7Jy8hhCXRhGdEL', 'wucZvSYhQ3du5gR0H6', 'W1lzh3kSmDB8ZJyEGX', 'UkEE85JGJvhbNhbS8RM', 'oZiqjgW5GfCstWf09Hb']
Problems with original code:
pass_list() was not actually called in the script.
Appended to an empty list rather than using comprehension - drastically slower.
What changed:
Cleaner syntax (in general) - for example when looping over a variable which is not used anywhere it is better to use "_" in my opinion.
Function call at the end to produce your results.
list comprehension to replace appending for loop.
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
For example from the below string,
abc6:ContextData abc6:xyz1 iCare abc6:xyz2 abc6:xyz3 abc6:xyz4 <abc6:xyz5 abc6:xyz6 abc6:xyz7 abc6:ContextData
I want to extract the words preceded by "abc6." For "abc6:xyz3" I want the suffix xyz3. For the longer example, the output would be like:
ContextData,xyz1,xyz2,xyz3,xyz4,xyz5,xyz6,xyz7,ContextData
do we need any regular expression for these?
An R-base Solution based on substr function is:
z <- "abc6:ContextData abc6:xyz1 iCare abc6:xyz2 abc6:xyz3 abc6:xyz4 <abc6:xyz5 abc6:xyz6 abc6:xyz7 abc6:ContextData"
z1 <- unlist(strsplit(z, split=" "))
z2 <- z1[substr(z1, start=1, stop=5)=="abc6:"]
z3 <- substr(z2, start=6, stop=nchar(z2))
cat(z3, sep=",")
Result:
ContextData,xyz1,xyz2,xyz3,xyz4,xyz6,xyz7,ContextData
Your post is tagged with both the languages r and python
In r, you can use gsub() to replace the pattern "abc6:" by the empty string.
In python, you can implement gsub as follows:
import re
def gsub(old, new, search_space):
return re.sub(old, new, search_space)
Replacing abc6: with the empty string:
z = "abc6:ContextData abc6:xyz1 iCare abc6:xyz2 abc6:xyz3 abc6:xyz4"
z2 = gsub("abc6:","",z)
> z2
[1] "ContextData xyz1 iCare xyz2 xyz3 xyz4"
If you want commas instead of spaces, you can then use
z3 = gsub(" ",",",z2)
> z3
[1] "ContextData,xyz1,iCare,xyz2,xyz3,xyz4"
Or if you are looking for a vector,
> strsplit(z2," ")[[1]]
[1] "ContextData" "xyz1" "iCare" "xyz2" "xyz3" "xyz4"
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 4 years ago.
Improve this question
I'm a new python user. I try to write a script to deal with multiple text files all of them the same name and indexed like
sample1_freq780Hz_accRate600Hz_V3Volt_1_.txt
sample1_freq780Hz_accRate600Hz_V3Volt_2_.txt
sample1_freq780Hz_accRate600Hz_V3Volt_3_.txt
I used this code:
def get_numbers_from_filename(filename):
result = re.search(r'\d+', filename).group(0)
return result for filename in os.listdir('input/'):
print(get_numbers_from_filename(filename))
but it didn't give me the wanted results
I need a code to get the numbers as follow:
freq: 780 Hz
accRate: 600 Hz
V: 3 Volt
and how to deal and use these numbers as parameters inside the full script?
You can use regex:
import re
import os
new_s = [dict(zip(['freq', 'accRate', 'V'], ['{} {}'.format(c, a) for a, c in zip(['Hz', 'Hz', 'Volt'], re.findall('(?<=freq)\d+|(?<=accRate)\d+|(?<=V)\d+', i))])) for i in os.listdir('input/')]
print(new_s[0])
Output:
{'freq': '780 Hz', 'accRate': '600 Hz', 'V': '3 Volt'}
To access the values in the list, iterate over new_s:
values = [new_s[0][i] for i in ['freq', 'accRate', 'V']]
Output:
['780 Hz', '600 Hz', '3 Volt']
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose we have a function processed_filter_description(a, b, c) which takes 3 parameters.
a = user input description
b = file extensions for potential images (jpg, gif),
c = the list of all the images (['image1.jpg', 'image1.gif', 'image2.jpg', 'image2.gif', 'image3.jpg', 'image3.gif'])
The function checks the description variable (a), and checks the matching extension or image type variable (b), and return the matching criteria from the images list (c).
If I understand you spec correctly, it looks something like this:
>>> def processed_filter_description(desc, exts, all_images):
result = []
for image in all_images:
base, ext = os.path.splitext(image)
if base.startswith(desc) and ext in exts:
result.append(image)
return result
>>> filenames = ['rainbow1.gif', 'sunset1.jpg', 'rainbow1.idx',
'sunset2.jpg', 'rainbow2.jpg', 'sunset3.gif']
>>> processed_filter_description('rainbow', ('.jpg', '.gif'), filenames)
['rainbow1.gif', 'rainbow2.jpg']