Board logo

標題: 搵字串最大值 [打印本頁]

作者: 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

回覆  jetdone


我本身做文職,吾係IT,工作上遇到呢個問題,人手可以解決,但想試下用PROGRAM 解決。 ...
bongbong3481 發表於 2024-6-27 06:59

作者: 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 編輯
  1. Sub ExtractAndGroupValues()
  2.     Dim df As Variant
  3.     Dim dfExtracted As Variant
  4.     Dim dfMax As Variant
  5.     Dim i As Long
  6.     Dim substring As String
  7.     Dim value As Long
  8.    
  9.     ' Assume your data is in the range "A1:A6" on the "Sheet1" worksheet
  10.     df = Worksheets("Sheet1").Range("A1:A6").Value
  11.    
  12.     ' Initialize the new dataframe
  13.     ReDim dfExtracted(1 To UBound(df), 1 To 2)
  14.    
  15.     ' Process each row of data one by one
  16.     For i = 1 To UBound(df)
  17.         ' Extract the substring and value
  18.         substring = Extract_Substring(df(i, 1))
  19.         value = Extract_Value(df(i, 1))
  20.         
  21.         ' Store the extracted values in the new dataframe
  22.         dfExtracted(i, 1) = substring
  23.         dfExtracted(i, 2) = value
  24.     Next i
  25.    
  26.     ' Group by the substring and find the maximum value
  27.     dfMax = GroupAndFindMax(dfExtracted)
  28.    
  29.     ' Display the result on the "Sheet2" worksheet
  30.     Worksheets("Sheet2").Range("A1").Resize(UBound(dfMax), 2).Value = dfMax
  31. End Sub

  32. Function Extract_Substring(input As String) As String
  33.     Dim match As Object
  34.     Dim regEx As Object
  35.    
  36.     Set regEx = CreateObject("VBScript.RegExp")
  37.     regEx.Pattern = "([a-zA-Z]+)(\d+)"
  38.    
  39.     Set match = regEx.Execute(input)
  40.     If match.Count > 0 Then
  41.         Extract_Substring = match(0).SubMatches(0)
  42.     Else
  43.         Extract_Substring = ""
  44.     End If
  45. End Function

  46. Function Extract_Value(input As String) As Long
  47.     Dim match As Object
  48.     Dim regEx As Object
  49.    
  50.     Set regEx = CreateObject("VBScript.RegExp")
  51.     regEx.Pattern = "([a-zA-Z]+)(\d+)"
  52.    
  53.     Set match = regEx.Execute(input)
  54.     If match.Count > 0 Then
  55.         Extract_Value = CLng(match(0).SubMatches(1))
  56.     Else
  57.         Extract_Value = 0
  58.     End If
  59. End Function
  60. Function GroupAndFindMax(data As Variant) As Variant
  61.     Dim result As Variant
  62.     Dim dict As Object
  63.     Dim key As Variant
  64.     Dim maxValue As Long
  65.     Dim i As Long
  66.    
  67.     ' Initialize the result
  68.     ReDim result(1 To 0, 1 To 2)
  69.    
  70.     ' Create a dictionary to group and find the maximum value
  71.     Set dict = CreateObject("Scripting.Dictionary")
  72.    
  73.     ' Process each row of data
  74.     For i = 1 To UBound(data)
  75.         key = data(i, 1)
  76.         If dict.Exists(key) Then
  77.             If data(i, 2) > dict(key)(2) Then
  78.                 dict(key) = Array(key, data(i, 2)) ' Fix: Store the value in the second column
  79.             End If
  80.         Else
  81.             dict.Add key, Array(key, data(i, 2))
  82.         End If
  83.     Next i
  84.    
  85.     ' Store the result in the new dataframe
  86.     i = 1
  87.     For Each key In dict.Keys
  88.         ReDim Preserve result(1 To i, 1 To 2)
  89.         result(i, 1) = dict(key)(0) ' Fix: Store the key in the first column
  90.         result(i, 2) = dict(key)(1)
  91.         i = i + 1
  92.     Next key
  93.    
  94.     GroupAndFindMax = result
  95. End Function
複製代碼
你確定vba 使用範圍太窄???

作者: 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



回覆  java2


    大量重覆既文書處理e.g. 計數,email 多,本身用開vba , 但vba 使用範圍太窄,所以學py ...
bongbong3481 發表於 2024-6-27 12:36

作者: 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

我估佢係AI找來再POST出來, 其實現在運作就係整個範例俾你參考去寫, 唔係代你去寫, 所以要有程式底會好一 ...
jetdone 發表於 2024/6/28 20:04


搞清自己用咩Ver. office好過LA,我360行到 lo
作者: Wiwacomia    時間: 2024-6-28 21:14

如果啲數字一定係個位數,case-insensitive sort.
作者: bongbong3481    時間: 2024-6-28 21:46

搞清自己用咩Ver. office好過LA,我360行到 lo
s20012797 發表於 2024-6-28 21:00



    我用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





歡迎光臨 電腦領域 HKEPC Hardware (https://www.hkepc.com/forum/) Powered by Discuz! 7.2