作者: bongbong3481 時間: 2024-6-26 20:05 標題: 搵字串最大值
本帖最後由 bongbong3481 於 2024-6-28 21:50 編輯
有一個column 字串, 請問一下, 如果搵T的最大值,會出t5, 如果搵Tea 的最大值會出Tea7 , 如此類推, 請問python 點樣寫? (念左好耐都解決吾到
)T1
t5
T2
TEa3
Tea7
TeA2
.
.
.
/////////////////////////////////////////COPILOT 寫的VBA 版
Sub FindMaxTeaValue()
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Dim maxValue As Double
Dim currentValue As Double
' 假設你的資料在 Sheet1 的 A 列(第一列)
Set ws = ThisWorkbook.Sheets("Sheet1")
Set rng = ws.Range("A1:A" & ws.Cells(ws.Rows.Count, 1).End(xlUp).Row)
' 初始化最大值
maxValue = 0
For Each cell In rng
If UCase(cell.Value) Like "T*" Then
' 檢查是否以 "T" 開頭(不區分大小寫)
currentValue = Val(Mid(cell.Value, 2))
If currentValue > maxValue Then
maxValue = currentValue
End If
End If
Next cell
MsgBox "以 T 開頭的最大值為 " & maxValue
End Sub
作者: javacomhk 時間: 2024-6-26 20:13
本帖最後由 javacomhk 於 2024-6-26 13:23 編輯
ask chatgpt or Github Copilot
提示:可以使用正则表达式 regular expression 来查找字符串并获取包含指定子字符串的最大值
作者: jetdone 時間: 2024-6-26 21:05
這條題目是基本功深度, 如果咁都要問人, 就要想想是否應該向寫程式的方向發展.
作者: bongbong3481 時間: 2024-6-26 22:15
回覆 2# javacomhk
問左Ai 解決了
作者: bongbong3481 時間: 2024-6-27 06:59
回覆 3# jetdone
我本身做文職,吾係IT,工作上遇到呢個問題,人手可以解決,但想試下用PROGRAM 解決。
作者: java2 時間: 2024-6-27 09:38
文職要用python
作者: bongbong3481 時間: 2024-6-27 12:36
回覆 6# java2
大量重覆既文書處理e.g. 計數,email 多,本身用開vba , 但vba 使用範圍太窄,所以學python
作者: s20012797 時間: 2024-6-27 17:43
本帖最後由 s20012797 於 2024-6-28 21:10 編輯
- Sub ExtractAndGroupValues()
- Dim df As Variant
- Dim dfExtracted As Variant
- Dim dfMax As Variant
- Dim i As Long
- Dim substring As String
- Dim value As Long
-
- ' Assume your data is in the range "A1:A6" on the "Sheet1" worksheet
- df = Worksheets("Sheet1").Range("A1:A6").Value
-
- ' Initialize the new dataframe
- ReDim dfExtracted(1 To UBound(df), 1 To 2)
-
- ' Process each row of data one by one
- For i = 1 To UBound(df)
- ' Extract the substring and value
- substring = Extract_Substring(df(i, 1))
- value = Extract_Value(df(i, 1))
-
- ' Store the extracted values in the new dataframe
- dfExtracted(i, 1) = substring
- dfExtracted(i, 2) = value
- Next i
-
- ' Group by the substring and find the maximum value
- dfMax = GroupAndFindMax(dfExtracted)
-
- ' Display the result on the "Sheet2" worksheet
- Worksheets("Sheet2").Range("A1").Resize(UBound(dfMax), 2).Value = dfMax
- End Sub
- Function Extract_Substring(input As String) As String
- Dim match As Object
- Dim regEx As Object
-
- Set regEx = CreateObject("VBScript.RegExp")
- regEx.Pattern = "([a-zA-Z]+)(\d+)"
-
- Set match = regEx.Execute(input)
- If match.Count > 0 Then
- Extract_Substring = match(0).SubMatches(0)
- Else
- Extract_Substring = ""
- End If
- End Function
- Function Extract_Value(input As String) As Long
- Dim match As Object
- Dim regEx As Object
-
- Set regEx = CreateObject("VBScript.RegExp")
- regEx.Pattern = "([a-zA-Z]+)(\d+)"
-
- Set match = regEx.Execute(input)
- If match.Count > 0 Then
- Extract_Value = CLng(match(0).SubMatches(1))
- Else
- Extract_Value = 0
- End If
- End Function
- Function GroupAndFindMax(data As Variant) As Variant
- Dim result As Variant
- Dim dict As Object
- Dim key As Variant
- Dim maxValue As Long
- Dim i As Long
-
- ' Initialize the result
- ReDim result(1 To 0, 1 To 2)
-
- ' Create a dictionary to group and find the maximum value
- Set dict = CreateObject("Scripting.Dictionary")
-
- ' Process each row of data
- For i = 1 To UBound(data)
- key = data(i, 1)
- If dict.Exists(key) Then
- If data(i, 2) > dict(key)(2) Then
- dict(key) = Array(key, data(i, 2)) ' Fix: Store the value in the second column
- End If
- Else
- dict.Add key, Array(key, data(i, 2))
- End If
- Next i
-
- ' Store the result in the new dataframe
- i = 1
- For Each key In dict.Keys
- ReDim Preserve result(1 To i, 1 To 2)
- result(i, 1) = dict(key)(0) ' Fix: Store the key in the first column
- result(i, 2) = dict(key)(1)
- i = i + 1
- Next key
-
- GroupAndFindMax = result
- End Function


作者: bongbong3481 時間: 2024-6-27 22:40
回覆 8# s20012797
我表達得吾好,我指vba 範圍窄,主要是指office 軟件以外既野,eg. 圖像處理,pdf,網上資料,等等
作者: java2 時間: 2024-6-27 22:48
今時今日做文職咁高要求
去返你個case, 可以每個字串用regulatr expression 去拆開key 同 value, 用hashmap (vba 叫dictionary )記下最大值, 在相同key 下將大於前一個值的put 入去, 然后想搵某key 最大值可直接用key 由hashmap 取.
vba 範圍唔窄也可以寫到.
Dim dict As Object
Sub a()
Set dict = CreateObject("Scripting.Dictionary")
For i = 1 To 6
Debug.Print findMax(Cells(i, 1))
Next i
For Each Key In dict.Keys
Debug.Print Key, dict(Key)
Next Key
End Sub
Function findMax(s As String) As String
Dim oRe As Object
Set oRe = CreateObject("vbscript.regexp")
oRe.Pattern = "([a-zA-Z]+)(\d+)"
Set oMatches = oRe.Execute(s)
If oMatches.Count > 0 Then
Set oMatch = oMatches(0)
skey = UCase(oMatch.SubMatches(0))
sval = oMatch.SubMatches(1)
If sval > dict(skey) Then dict(skey) = sval
findMax = skey + ":" + sval
Else
findMax = "no data"
End If
End Function
作者: bongbong3481 時間: 2024-6-28 19:19
回覆 10# java2
公司沒要求識寫vba or python,吾理過程,只要結果,用vba or python 可以輔助自己,減輕工作量
作者: bongbong3481 時間: 2024-6-28 19:21
回覆 8# s20012797
我copy and paste 你 d code ,vba editor 內見到d function 變紅字,好似run 吾到
作者: jetdone 時間: 2024-6-28 20:04
回覆 s20012797
我copy and paste 你 d code ,vba editor 內見到d function 變紅字,好似run 吾到 ...
bongbong3481 發表於 2024-6-28 19:21
我估佢係AI找來再POST出來, 其實現在運作就係整個範例俾你參考去寫, 唔係代你去寫, 所以要有程式底會好一點.
作者: s20012797 時間: 2024-6-28 21:00
搞清自己用咩Ver. office好過LA,我360行到 lo


作者: Wiwacomia 時間: 2024-6-28 21:14
如果啲數字一定係個位數,case-insensitive sort.
作者: bongbong3481 時間: 2024-6-28 21:46
我用2016, 我用COPILOT搵到一個簡單得黎,我又理解到既VBA 版
Sub FindMaxTeaValue()
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Dim maxValue As Double
Dim currentValue As Double
' 假設你的資料在 Sheet1 的 A 列(第一列)
Set ws = ThisWorkbook.Sheets("Sheet1")
Set rng = ws.Range("A1:A" & ws.Cells(ws.Rows.Count, 1).End(xlUp).Row)
' 初始化最大值
maxValue = 0
For Each cell In rng
If UCase(cell.Value) Like "T*" Then
' 檢查是否以 "T" 開頭(不區分大小寫)
currentValue = Val(Mid(cell.Value, 2))
If currentValue > maxValue Then
maxValue = currentValue
End If
End If
Next cell
MsgBox "以 T 開頭的最大值為 " & maxValue
End Sub

