I think this easy for experts but not for me. i have list see below mylist. I have created a fuction but its not doing what i am expecting. i am trying to get if any string begins with "M" in after the first space( index is 1 in mylist).
mylist:
[G64 (Default cutting) G17 (XY plane) G40 (Cancel radius comp.) G49 (Cancel length comp.)]
[G0 Z 15.000]
[(*** LAYER: 0 ***)]
[T1 M6]
[S6000]
[(* SHAPE Nr: 3 *)]
[G0 X 322.696 Y 22.557]
[M3 M8]
[G0 Z 3.000]
[F150]
[G1 Z -1.500]
[F400]
myfunction:
def My_function(Element_find, mylist):
if Element_find in mylist:
position = mylist[1]
field = position
return field
else:
return 'Null'
My_function('M', mylist)
my_output:
Null
Null
Null
Null
Null
Null
Null
Null
Null
Null
expected output is:
Null
Null
M6
Null
Null
M8
Null
Null
Null
Null
your help will be fine if you can correct my function
CodePudding user response:
I'm not very sure about the constraints of your question but from what I have understood, you're checking if the second word (if present) of every value in the list matches with the element specified.
There needs to be some clarification regarding the input list, but besides that I think this should work for your question.
def My_function(Element_find, Data_list):
for line in Data_list:
arr = line.split(" ")
if len(arr)>1:
if arr[1][0] == Element_find:
print(arr[1])
else:
print("Null")
else:
print("Null")
mylist = [
'G64 (Default cutting) G17 (XY plane) G40 (Cancel radius comp.) G49 (Cancel length comp.)'
'G0 Z 15.000',
'(*** LAYER: 0 ***)',
'T1 M6',
'S6000',
'(* SHAPE Nr: 3 *)',
'G0 X 322.696 Y 22.557',
'M3 M8',
'G0 Z 3.000 ',
'F150',
'G1 Z -1.500',
'F400',
]
My_function('M', mylist)
Output
Null
Null
M6
Null
Null
Null
M8
Null
Null
Null
Null
CodePudding user response:
- the if condition in your def-
if Element_find in Data_list:
is not correct, the condition will be True only if there is an element in the list which is identical to the elemnt you are looking for, and will be false if it is only a substring of it.
I did not see where you defined "Data_list", I guess you ment "mylist"
in your output and exepcted output there are many outputs of the def you wrote.
this def will do what you asked for:
def My_function(Element_find, mylist):
for field in mylist:
if field[1] == Element_find:
return field
else:
return 'Null'
CodePudding user response:
string = '''G64 (Default cutting) G17 (XY plane) G40 (Cancel radius comp.) G49 (Cancel length comp.)
G0 Z 15.000
(*** LAYER: 0 ***)
T1 M6
S6000
(* SHAPE Nr: 3 *)
G0 X 322.696 Y 22.557
M3 M8
G0 Z 3.000
F150
G1 Z -1.500
F400'''
string = string.split('\n')
mylist = []
for i in range(len(string)):
mylist.append(string[i].split(' '))
print(mylist)
print('------------------------------------')
def My_function(Element_find, mylist):
results = []
for i in range(len(mylist)):
if mylist[i] == ['']:
line = ''
else:
for j in range(len(mylist[i])):
try:
if mylist[i][j][0] == Element_find:
line = (mylist[i][j])
break
else:
line = 'Null'
except IndexError:
None
results.append(line)
return results
My_function('M', mylist)
Output:
[['G64', '(Default', 'cutting)', 'G17', '(XY', 'plane)', 'G40', '(Cancel', 'radius', 'comp.)', 'G49', '(Cancel', 'length', 'comp.)'], ['G0', 'Z', '', '15.000', ''], [''], ['(***', 'LAYER:', '0', '***)'], ['T1', 'M6'], ['S6000'], [''], ['(*', 'SHAPE', 'Nr:', '3', '*)'], ['G0', 'X', '322.696', 'Y', '', '22.557'], ['M3', 'M8'], ['G0', 'Z', '', '', '3.000', ''], ['F150'], ['G1', 'Z', '', '-1.500'], ['F400']]
------------------------------------
['Null',
'Null',
'',
'Null',
'M6',
'Null',
'',
'Null',
'Null',
'M3',
'Null',
'Null',
'Null',
'Null']
CodePudding user response:
Try using the str.join()
method:
def My_function(Element_find, mylist):
for Data_list in mylist:
if Element_find in ''.join(Data_list):
print(Data_list[1])
else:
print('Null')
mylist = [['G64', '(Default', 'cutting)', 'G17', '(XY', 'plane)', 'G40', '(Cancel', 'radius', 'comp.)', 'G49', '(Cancel', 'length', 'comp.)'], ['G0', 'Z', '15.000]'], [], ['(***', 'LAYER:', '0', '***)'], ['T1', 'M6'], ['S6000'], [], ['(*', 'SHAPE', 'Nr:', '3', '*)'], ['G0', 'X', '322.696', 'Y', '22.557'], ['M3', 'M8'], ['G0', 'Z', '3.000]'], ['F150'], ['G1', 'Z', '-1.500'], ['F400']]
My_function('M', mylist)
Output:
Null
Null
Null
Null
M6
Null
Null
Null
Null
M8
Null
Null
Null
Null
CodePudding user response:
Here is another approach using python regex
:
import re
data = [['G64 (Default cutting) G17 (XY plane) G40 (Cancel radius comp.) G49 (Cancel length comp.)'],
['G0 Z 15.000'],
['(*** LAYER: 0 ***)'],
['T1 M6'],
['S6000'],
['(* SHAPE Nr: 3 *)'],
['G0 X 322.696 Y 22.557'],
['M3 M8'],
['G0 Z 3.000'],
['F150'],
['G1 Z -1.500'],
['F400']]
def My_function(element, mylist):
for item in mylist:
item = item[0]
if re.search(rf'.*?\s{element}.*', item):
print (item.split()[1])
else:
print ('Null')
My_function('M', data)
Output:
Null
Null
M6
Null
Null
Null
M8
Null
Null
Null
Null
Null
CodePudding user response:
Does this work for you:
def find_letter(element, iterator):
if ' ' in iterator:
if element == iterator[(index := (iterator.index(' ') 1))]:
return element iterator[index 1]
return 'Null'
for string in mylist:
print(find_letter('M', string))
For Python versions before 3.8:
def find_letter(element, iterator):
if ' ' in iterator:
index = iterator.index(' ') 1
if element == iterator[index]:
return element iterator[index 1]
return 'Null'
Output:
Null
Null
Null
M6
Null
Null
Null
M8
Null
Null
Null
Null
Input:
mylist = ['G64 (Default cutting) G17 (XY plane) G40 (Cancel radius comp.) G49 (Cancel length comp.)',
'G0 Z 15.000',
'(*** LAYER: 0 ***)',
'T1 M6',
'S6000',
'(* SHAPE Nr: 3 *)',
'G0 X 322.696 Y 22.557',
'M3 M8',
'G0 Z 3.000',
'F150',
'G1 Z -1.500',
'F400']
Your code has several problems:
- Your parameter name does not match the variable name referenced in the function
[1]
does not return the first element after a space, but the letter at the second index.- If you pass a list to this function, it won't work, like you would expect. You would have to iterate to your list first, to get the strings.