分子的隐蔽分子捷径

时间:2017-11-10 20:04:20

标签: python string int

请不要进口。试图掌握python的基础知识。 说我想要一个简单的分子输入给我实际的分子

例如:

分子(' C3H8O3')会给我' CCCHHHHHHHHOOO'

def desimplify(molecule):
    findnum = 0
    findchar = 0
    for i in range(0, len(molecule)):
        findnum += 1
        if molecule[i].isdigit(): #not sure if this is the right method
            findchar = findnum - 1 
            res = molecule[:findnum] + res * int(res[findnum]) + molecule[findchar:]
    return res

我确定我完全错了,请指出我正确的方向。

编辑:修正拼写错误

1 个答案:

答案 0 :(得分:0)

为了向您提供一个工作示例,我将如何实现该功能。我发现$teams = array(); // Empty array for your teams $results = array('wins' => 0, 'draws' => 0, 'losses' = 0); // Starting array for each team foreach ($array as $a) { // Loop over your database array $id = $a['TeamID']; // Use TeamID as your array keys if (!isset($teams[$id])) { // Check if team is already in your teams array $teams[$id] = $results; // If not then insert them with the starting array } if ($a['Punten'] == 0) { // Now loop over the points column and increment results $teams[$id]['losses'] += 1; } if ($a['Punten'] == 1) { $teams[$id]['draws'] += 1; } if ($a['Punten'] == 3) { $teams[$id]['wins'] += 1; } } 的建筑有点难以理解你是怎么做到的,所以我把它分开了。评论应该解释发生了什么,如果有什么不清楚,我会很乐意详细说明。

请注意,这可能不是最恐怖的做事方式。

Option Explicit

Sub Macro1()
    Dim c As Long, firstaddress As String, f As Range, ffs As Range

    With Worksheets("sheet1").Rows(3).Cells
        .Resize(2, .Columns.Count).UnMerge
        Set f = Nothing
        For c = 65 To 90
            Set f = .Find(Chr(c), LookIn:=xlValues, Lookat:=xlWhole)
            If Not f Is Nothing Then
                Set ffs = f
                firstaddress = f.Address
                Do
                    Set ffs = Union(f, ffs)
                    Set f = .FindNext(after:=f)
                Loop While f.Address <> firstaddress
                With Union(ffs, ffs.Offset(1))
                    .Merge
                    .BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
                End With
            End If
        Next c
    End With
End Sub

运行此结果:

res

编辑:通过直接在输入字符串上迭代,使其更加Pythonic,没有循环变量。