我试图操纵包含如下数据的csv文件:
['193T','4234234234'],['30T','54353456346'],['203K','4234234234'],['19E','4234234234']
字母数字字符串应该分成数字和单个字符,然后作为int()和字符串放回到数组中。然后第二步是聚集所有相同的字符并按整数排序。
结束这样的事情:
[19,'E',4234234234],[203,'K',4234234234],[30,'T',54353456346],[193,'T',4234234234]
我希望你能掌握它背后的想法。
非常感谢你提前帮助。
答案 0 :(得分:1)
l = [['193T','4234234234'],
['30T','54353456346'],
['203K','4234234234'],
['19E','4234234234']]
# Using a list comprehension
[[int(i[0][:-1]), i[0][-1], int(i[1])] for i in l]
输出
[[193, 'T', 4234234234],
[30, 'T', 54353456346],
[203, 'K', 4234234234],
[19, 'E', 4234234234]]
然后,您可以使用第二个元素作为key
进行排序。
sorted([[int(i[0][:-1]), i[0][-1], int(i[1])] for i in l], key = lambda x: x[1])
输出
[[19, 'E', 4234234234L],
[203, 'K', 4234234234L],
[193, 'T', 4234234234L],
[30, 'T', 54353456346L]]
答案 1 :(得分:1)
如果字母字符始终位于字符串的末尾,则可以使用:
>>> entries = (['193T','4234234234'],['30T','54353456346'],['203K','4234234234'],['19E','4234234234'])
>>> entries = tuple([int(entry[0][:-1]), entry[0][-1], entry[1]] for entry in entries)
>>> print entries
([193, 'T', '4234234234'], [30, 'T', '54353456346'], [203, 'K', '4234234234'], [19, 'E', '4234234234'])
答案 2 :(得分:1)
import re
from operator import itemgetter
import pprint
# Pretty printer (for nice output)
pp = pprint.PrettyPrinter(indent=4)
# Test data
arr = [
['193T','4234234234'],['30T','54353456346'],['203K','4234234234'],['19E','4234234234']
]
for x in arr:
# Regex search of string
m = re.search("(?i)(\d+)([a-z])", x[0])
if m:
number = int(m.group(1))
character = m.group(2)
x.pop(0)
x.insert(0, character)
x.insert(0, number)
# Sort the results
result = sorted(sorted(arr, key=itemgetter(0)), key=itemgetter(1))
pp.pprint(result)