2012-03-28 21 views
2

我對Python很新,但我認爲我趕上了。在python中使用條件變量字符串

無論如何,我正在制定一個計劃(不是爲了上課,而是爲了幫助我)並且遇到了問題。

我試圖記錄一些事情的清單,我的意思是接近一千的事情,有一些重複。所以我的問題是這樣的:

我不想在列表中添加多餘的名稱,而是我只想在它之前(或之後,取其較簡單的地方)添加一個2x或3x,然後將其寫入txt文檔。

我很喜歡閱讀和寫文本文件,但我唯一的問題是條件聲明,我不知道如何寫它,我也不能在網上找到它。

for lines in list_of_things: 
    if(lines=="XXXX x (name of object here)"): 

然後根據if語句。我唯一的問題是,「XXXX」可以用任何字符串編號替換,但我不知道如何在字符串中包含變量,如果這是有道理的。即使它變成了一個int,我仍然不知道如何在一個條件中使用一個變量。

我能想到的唯一的事情就是製作多個if語句,這會很長。

有什麼建議嗎?我爲文本的牆壁道歉。

+0

爲了澄清,你是否說你有一個潛在的冗餘線源,並最終你想輸出以數字爲前綴的唯一行?另外,命令重要嗎? – jdi 2012-03-28 17:43:29

回答

5

我建議遍歷輸入文件中的行,並在字典中插入一個關鍵爲您找到每個人,然後在遞增值對於您在其中找到的每個值的每個實例,都要使用該關鍵字,然後從該字典中生成輸出文件。

catalog = {} 
for line in input_file: 
    if line in catalog: 
     catalog[line] += 1 
    else: 
     catalog[line] = 1 

或者

from collections import defaultdict 
catalog = defaultdict(int) 
for line in input_file: 
    catalog[line] += 1 

然後,只需通過字典運行,並打印出來的文件。

+0

我想這是他問的。和我剛纔提議的一樣。 – jdi 2012-03-28 17:45:07

+1

@NolenRoyalty:最終我會建議它,但它應該是一個附加位的信息,首先解釋標準字典的方式後,因爲他是一個新的Python程序員。 – jdi 2012-03-28 17:58:33

+0

@jdi足夠公平,解決方案無論如何都是正確的(假設我們已經正確理解了這個問題)。 – 2012-03-28 17:59:36

1

你可能會尋找regular expressions和類似

for line in text: 
    match = re.match(r'(\d+) x (.*)', line) 
    if match: 
     count = int(match.group(1)) 
     object_name = match.group(2) 
     ... 
+0

我意識到OP顯示的模式真的是一種尷尬的方法來計算線條,而且他已經將這些字符串預先格式化了,因爲他攝入了這些字符串,然後想要對它們進行重新分析。 – jdi 2012-03-28 17:45:52

0

這應做到:

a = [1,1,1,1,2,2,2,2,3,3,4,5,5] 
from itertools import groupby 
print ["%dx %s" % (len(list(group)), key) for key, group in groupby(a)] 
+1

特別是在OP被公認爲是一個新的Python程序員的時候,你應該對這個答案稍微不太熟悉。 – jdi 2012-03-28 17:57:37

+0

他說他抓得很快:)呵呵我很抱歉,如果這太高級了,我只希望它對OP有幫助,但請記住,這是一個公開的問答環節,而不是初學者的人可能會遲來。 – Trufa 2012-03-28 18:05:30

+0

那麼如果你要推薦一些先進的東西,至少要修正它,所以它不使用常量字符串連接:'[「%dx%s」%(len(list(group)),key)for key,group in groupby(a)]' – jdi 2012-03-28 18:10:20

0

像這樣的事情?

list_of_things=['XXXX 1', 'YYYY 1', 'ZZZZ 1', 'AAAA 1', 'ZZZZ 2'] 

for line in list_of_things: 
    for e in ['ZZZZ','YYYY']: 
     if e in line: 
      print line 

輸出:

YYYY 1 
ZZZZ 1 
ZZZZ 2 

您還可以使用if line.startswith(e):或正則表達式(如果我理解你的問題...)

0

要在一個字符串變量,可以使用format()

 
>>> i = 123 
>>> s = "This is an example {0}".format(i) 
>>> s 
'This is an example 123' 

在這種情況下,{0}指示你會在那裏設置一個變量。如果你有更多的變量,使用"This is an example {0} and more {1}".format(i, j)"(所以每個變量的數字,從0開始)。

0

有兩種方法可以解決這個問題。 1)像的使用字典來捕獲項的計數,然後列表的每個項目具有其計格式化以下

list_of_things = ['sun', 'moon', 'green', 'grey', 'sun', 'grass', 'green'] 
listItemCount = {} 
countedList = [] 
for lines in list_of_thing: 
    if lines in listItemCount: 
     listItemCount[lines] += 1 
    else: 
     listItemCount[lines] = 1 
for id in listItemCount: 
    if listItemCount[id] > 1: 
     countedList.append(id+' - x'str(listItemCount[id])) 
    else: 
     countedList.append(id) 
for item in countedList: 
    print(item) 

的輸出上面會

sun - x2 
grass 
green - x2 
grey 
moon 

或2)使用集合使事情更加簡單如下所示

import collections 

list_of_things = ['sun', 'moon', 'green', 'grey', 'sun', 'grass', 'green'] 
listItemCount = collections.Counter(list_of_things) 
listItemCountDict = dict(listItemCount) 
countedList = [] 
for id in listItemCountDict: 
    if listItemCountDict[id] > 1: 
     countedList.append(id+' - x'str(listItemCountDict[id])) 
    else: 
     countedList.append(id) 
for item in countedList: 
    print(item) 

的輸出上面會

sun - x2 
grass 
green - x2 
grey 
moon