我有一個長文本文件,有卡車配置。在每一行中,卡車的一些屬性被列爲一個字符串。每個屬性都有字符串中的自己固定寬度的空間,如:使用字典代替Python中的動態變量名稱
2 chracters = number of axles
2 characters = weight of the first axle
2 characters = weight of the second axle
...
2 characters = weight of the last axle
2 characters = length of the first axle spacing (spacing means distance between axles)
2 characters = length of the second axle spacing
...
2 characters = length of the last axle spacing
舉個例子:
031028331004
是指:
number of axles = 3
first axle weight = 10
second axle weight = 28
third axle weight = 33
first spacing = 10
second spacing = 4
現在,您有一個想法我的文件結構,這裏是我的問題:我想將這些卡車分組在不同的列表中,並根據軸間距命名列表。假設我使用布爾類型的方法,並且如果間距小於6,布爾值爲1,如果它大於6,則布爾值爲0.爲了說明,三軸卡車中的可能結果變爲:
00 #Both spacings > 6
10 #First spacing < 6, second > 6
01 #First spacing > 6, second < 6
11 #Both spacings < 6
現在,正如你所看到的,3軸卡車沒有太多的結果。但是,如果我有一輛12軸卡車,那麼「可能」組合的數量就會出現問題。事實是,在現實中,你不會看到12軸卡車中軸間距的所有「可能」組合。有一些組合(我不知道哪些組合,但要弄清楚是我的目標),其數量遠遠少於「可能」數量的組合。
我希望代碼創建列表並填充它們以定義我上面提到的屬性的字符串如果只有這樣的組合存在。我想也許我應該創建具有變量名稱的列表,例如:
truck_0300[]
truck_0301[]
truck_0310[]
truck_0311[]
在飛行中。然而,從我在SF和其他來源讀到的內容,這是非常不鼓勵的。你如何使用字典概念來做到這一點?我知道詞典就像2維數組,有一個鍵(在我的情況下,鍵將是類似於truck_0300,truck_0301等)和值對(再次在我的情況下,值可能是列表,其中包含實際的字符串屬於相應的卡車類型),但我無法弄清楚如何創建該字典,並用變量鍵和值填充它。
任何見識都會受到歡迎! 非常感謝!
你有代碼來解析'031028331004'成有用的東西? – Eric
@Eric我已經做了解析部分。 – marillion