Re: Ask for help on using re

2021-08-07 Thread jak

Il 07/08/2021 04:23, Jach Feng ha scritto:

jak 在 2021年8月6日 星期五下午4:10:05 [UTC+8] 的信中寫道:

Il 05/08/2021 11:40, Jach Feng ha scritto:

I want to distinguish between numbers with/without a dot attached:


text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
re.compile(r'ch \d{1,}[.]').findall(text)

['ch 1.', 'ch 23.']

re.compile(r'ch \d{1,}[^.]').findall(text)

['ch 23', 'ch 4 ', 'ch 56 ']

I can guess why the 'ch 23' appears in the second list. But how to get rid of 
it?

--Jach


import re
t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M)

res = r.findall(t)

dot = [x[1] for x in res if x[1] != '']
udot = [x[0] for x in res if x[0] != '']

print(f"dot: {dot}")
print(f"undot: {udot}")

out:

dot: ['ch 4', 'ch 56']
undot: ['ch 1.', 'ch 23.']

The result can be influenced by the order of re patterns?


import re
t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M).findall(t)

[('ch 1.', ''), ('ch 23.', ''), ('', 'ch 4'), ('', 'ch 56')]


re.compile(r'(ch +\d+)|(ch +\d+\.)', re.M).findall(t)

[('ch 1', ''), ('ch 23', ''), ('ch 4', ''), ('ch 56', '')]

--Jach


Yes, when the patterns intersect each other as in your case. the
difference between the 2 patterns is the "." in addition. The logical or
does not continue checking when the condition is satisfied, so it is a
good idea, in these cases, to search for the most complete patterns
before the others.


--
https://mail.python.org/mailman/listinfo/python-list


Re: Ask for help on using re

2021-08-07 Thread jak

Il 07/08/2021 11:18, jak ha scritto:

Il 07/08/2021 04:23, Jach Feng ha scritto:

jak 在 2021年8月6日 星期五下午4:10:05 [UTC+8] 的信中寫道:

Il 05/08/2021 11:40, Jach Feng ha scritto:

I want to distinguish between numbers with/without a dot attached:


text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
re.compile(r'ch \d{1,}[.]').findall(text)

['ch 1.', 'ch 23.']

re.compile(r'ch \d{1,}[^.]').findall(text)

['ch 23', 'ch 4 ', 'ch 56 ']

I can guess why the 'ch 23' appears in the second list. But how to 
get rid of it?


--Jach


import re
t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M)

res = r.findall(t)

dot = [x[1] for x in res if x[1] != '']
udot = [x[0] for x in res if x[0] != '']

print(f"dot: {dot}")
print(f"undot: {udot}")

out:

dot: ['ch 4', 'ch 56']
undot: ['ch 1.', 'ch 23.']

The result can be influenced by the order of re patterns?


import re
t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M).findall(t)

[('ch 1.', ''), ('ch 23.', ''), ('', 'ch 4'), ('', 'ch 56')]


re.compile(r'(ch +\d+)|(ch +\d+\.)', re.M).findall(t)

[('ch 1', ''), ('ch 23', ''), ('ch 4', ''), ('ch 56', '')]

--Jach


Yes, when the patterns intersect each other as in your case. the
difference between the 2 patterns is the "." in addition. The logical or
does not continue checking when the condition is satisfied, so it is a
good idea, in these cases, to search for the most complete patterns
before the others.



PS
... the behavior of the logical or that I have described is not typical
of regular expressions but it is common in all programming languages.

--
https://mail.python.org/mailman/listinfo/python-list


Re: on slices, negative indices, which are the equivalent procedures?

2021-08-07 Thread Greg Ewing

On 6/08/21 12:00 pm, Jack Brandom wrote:

It seems
that I'd begin at position 3 (that's "k" which I save somewhere), then I
subtract 1 from 3, getting 2 (that's "c", which I save somewhere), then
I subtract 1 from 2, getting 1 (that's "a", ...), then I subtract 1 from
1, getting 0 (that's J, ...), so I got "kcaJ" but my counter is 0 not
-13, which was my stopping point.


You need to first replace any negative or missing indices with
equivalent indices measured from the start of the string.

When you do that in this example, you end up iterating backwards from 3
and stopping at -1.

--
Greg

--
https://mail.python.org/mailman/listinfo/python-list


Re: Ask for help on using re

2021-08-07 Thread Jach Feng
jak 在 2021年8月6日 星期五下午4:10:05 [UTC+8] 的信中寫道:
> Il 05/08/2021 11:40, Jach Feng ha scritto: 
> > I want to distinguish between numbers with/without a dot attached: 
> > 
>  text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n' 
>  re.compile(r'ch \d{1,}[.]').findall(text) 
> > ['ch 1.', 'ch 23.'] 
>  re.compile(r'ch \d{1,}[^.]').findall(text) 
> > ['ch 23', 'ch 4 ', 'ch 56 '] 
> > 
> > I can guess why the 'ch 23' appears in the second list. But how to get rid 
> > of it? 
> > 
> > --Jach 
> >
> import re
> t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
> r = re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M) 
> 
> res = r.findall(t) 
> 
> dot = [x[1] for x in res if x[1] != ''] 
> udot = [x[0] for x in res if x[0] != ''] 
> 
> print(f"dot: {dot}") 
> print(f"undot: {udot}") 
> 
> out: 
> 
> dot: ['ch 4', 'ch 56'] 
> undot: ['ch 1.', 'ch 23.']
The result can be influenced by the order of re patterns?

>>> import re
>>> t = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
>>> re.compile(r'(ch +\d+\.)|(ch +\d+)', re.M).findall(t)
[('ch 1.', ''), ('ch 23.', ''), ('', 'ch 4'), ('', 'ch 56')]

>>> re.compile(r'(ch +\d+)|(ch +\d+\.)', re.M).findall(t)
[('ch 1', ''), ('ch 23', ''), ('ch 4', ''), ('ch 56', '')]

--Jach
-- 
https://mail.python.org/mailman/listinfo/python-list


CODING PAGE ACCESS

2021-08-07 Thread MICHAEL J W SMITH via Python-list
I downloaded python. I selected it from the start menu. I clicked on:-
Python 3-9New
I got:-
IDLE (Python 3.9 64-bit)
Python 3.9 (64-bit)
Python 3.9 Manuals (64-bit)
Python 3.9 Module Docs (64-bit)
I wish to access the page where I do coding.I would appreciate help, either 
directly, or with information as to where I can get help.Thank you so much!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: CODING PAGE ACCESS

2021-08-07 Thread MRAB

On 2021-08-07 04:34, MICHAEL J W SMITH via Python-list wrote:

I downloaded python. I selected it from the start menu. I clicked on:-
Python 3-9New
I got:-
IDLE (Python 3.9 64-bit)
Python 3.9 (64-bit)
Python 3.9 Manuals (64-bit)
Python 3.9 Module Docs (64-bit)
I wish to access the page where I do coding.I would appreciate help, either 
directly, or with information as to where I can get help.Thank you so much!


The Manual contains a tutorial.

You can use IDLE to use Python interactively or edit programs.
--
https://mail.python.org/mailman/listinfo/python-list


Re: CODING PAGE ACCESS

2021-08-07 Thread Terry Reedy

On 8/6/2021 11:34 PM, MICHAEL J W SMITH via Python-list wrote:

I downloaded python. I selected it from the start menu. I clicked on:-
Python 3-9New


'New' goes away after the first access


I got:-
IDLE (Python 3.9 64-bit)


This is the included editor and enhanced interactive shell for writing 
and testing Python code.  Doc is available on the Help menu and at

https://docs.python.org/3/library/idle.html


Python 3.9 (64-bit)


This is the standard interactive shell.


Python 3.9 Manuals (64-bit)


The online manuals


Python 3.9 Module Docs (64-bit)


The 'help' version of module docs generated from the installed modules.


I wish to access the page where I do coding.I would appreciate help, either 
directly, or with information as to where I can get help.Thank you so much!




--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list