-4

I am working on small project using Python 2.6. Finally my list is like this.

data = ['1:Rammmm,mmmm', '2:surr.rrrrr', '3:Sam ttttt',
'1:qqqq qqqqqqq', '2:wwwwwwwwwww', '3:eeeeeeeee','4:tttttttt',
'1:zzzzzzzzzzzzz', '2:xxxxxxxxx']

Here, I want to display all 1's in one line, all 2's in one line, etc., with pipe"|" as separator. It should be like this, sorting only by the number.

{Rammmm,mmmm|qqqq qqqqqqq|zzzzzzzzzzzzz}
{surr.rrrrr|wwwwwwwwwww|xxxxxxxxx}
{Sam ttttt|eeeeeeeee}
{tttttttt}

How can this be achieved in Python?

4

6 回答 6

3

请参阅我的其他解决方案,因为我认为它比这个更好: https ://stackoverflow.com/a/10173807/1219006

>>> from operator import itemgetter
>>> from itertools import groupby
>>> data = ['1:Rammmm,mmmm', '2:surr.rrrrr', '3:Sam ttttt',
'1:qqqq qqqqqqq', '2:wwwwwwwwwww', '3:eeeeeeeee','4:tttttttt',
'1:zzzzzzzzzzzzz', '2:xxxxxxxxx']
>>> sorted_data = sorted(((int(x),y) for x,y in (el.split(':') for el in data)),key=itemgetter(0))
>>> for k,v in groupby(sorted_data,key=itemgetter(0)):
        print '{' + '|'.join(text for num,text in v) + '}'


{Rammmm,mmmm|qqqq qqqqqqq|zzzzzzzzzzzzz}
{surr.rrrrr|wwwwwwwwwww|xxxxxxxxx}
{Sam ttttt|eeeeeeeee}
{tttttttt}

测试

>>> data = ['1:Rameshrrrrrrr', '2:Sureshmmmmmm', '3:Samkkkkkkkk', '1:tttttttttt', '2:wwwwwwwwwwwwwwwww', '3:yyyyyyyyy', '4:gggggggggg', '1:qqqqqqqqqqq', '2:hhhhhhhhh']
>>> sorted_data = sorted(((int(x),y) for x,y in (el.split(':') for el in data)),key=itemgetter(0))
>>> for k,v in groupby(sorted_data,key=itemgetter(0)):
        print '{' + '|'.join(text for num,text in v) + '}'


{Rameshrrrrrrr|tttttttttt|qqqqqqqqqqq}
{Sureshmmmmmm|wwwwwwwwwwwwwwwww|hhhhhhhhh}
{Samkkkkkkkk|yyyyyyyyy}
{gggggggggg}
于 2012-04-16T06:23:45.477 回答
0

你会需要:

  • 一种将每个字符串拆分为数字部分和另一部分的方法

  • 一种将数字部分转换为整数的方法

  • 一种收集所有不同部分的方法。您需要具有相同编号部分的所有其他部分的列表。

  • 遍历列表中的所有字符串并收集所有内容。

  • 一种将其他部分连接在一起以形成最终结果的方法。

找出可以在 Python 中执行的操作的一个好方法是转到 Python 提示符并键入help(something). 将“某事”替换为您需要帮助的内容。试试这些:

help(str.split)

help(str.join)

我建议您通过调用转换为整数int()。像这样:

x = "3"
n = int(x)  # n is now the integer value 3

而且我建议你考虑用adict来收集东西。每个 dict 键将有多个值...您需要将它们连接在一起,dict或者为dict.

祝你好运!

于 2012-04-16T06:25:43.440 回答
0

我认为这是一种更好的方法

>>> from collections import defaultdict
>>> data = ['1:Rammmm,mmmm', '2:surr.rrrrr', '3:Sam ttttt',
'1:qqqq qqqqqqq', '2:wwwwwwwwwww', '3:eeeeeeeee','4:tttttttt',
'1:zzzzzzzzzzzzz', '2:xxxxxxxxx']
>>> d = defaultdict(list)
>>> for num, text in (el.split(':') for el in data):
        d[int(num)].append(text)


>>> for key in sorted(d):
        print '{' + '|'.join(d[key]) + '}'


{Rammmm,mmmm|qqqq qqqqqqq|zzzzzzzzzzzzz}
{surr.rrrrr|wwwwwwwwwww|xxxxxxxxx}
{Sam ttttt|eeeeeeeee}
{tttttttt}
于 2012-04-16T12:09:23.517 回答
0
#!/usr/bin/python

l = ['1:Rammmm,mmmm', '2:surr.rrrrr', '3:Sam ttttt', '1:qqqq qqqqqqq', '2:wwwwwwwwwww', 3:eeeeeeeee','4:tttttttt', '1:zzzzzzzzzzzzz', '2:xxxxxxxxx']

l = [ x.split(':') for x in l ]

d ={}
for k, v in l:
  try: 
    d[k]
  except:
    d[k] = []
  d[k].append(v)

l = d.values()
l = [ "|".join(x) for x in l ]
于 2012-04-16T06:44:59.767 回答
0

我们可以这样做来获得欲望输出:

l=[]
for ls in data:
    l.append(ls[:1])
m=set(l)
for i in m:
    str=''
    for ls in data:
        if i==ls[:1]:
            str=str+ls[3:]+"|"
    print '{'+str+'}'
于 2012-04-16T13:30:23.947 回答
0

可能这个变体会更简单:

data = ['1:Rammmm,mmmm', '2:surr.rrrrr', '3:Sam ttttt',
        '1:qqqq qqqqqqq', '2:wwwwwwwwwww', '3:eeeeeeeee','4:tttttttt',
        '1:zzzzzzzzzzzzz', '2:xxxxxxxxx']

d = dict()
for item in data:
    k, v = item.split(':')
    # if d already has list at key 'k', append to it; 
    # otherwise, create list, and append to newly created
    d.setdefault(k,list()).append(v)

# now print formatted list in order of sorted keys (1,2,3,4)
for k in sorted(d.keys()):
    print '{' + '|'.join(d[k]) + '}'
于 2012-04-17T01:49:14.400 回答