如何根据字符串所属的类别重新排列字符串?假设我有这些套装:
dogs = {'husky', 'chihuahua', 'labrador', 'beagle'}
flowers = {'dandelion', 'rose', 'tulip'}
colours = {'blue', 'yellow', 'green', 'red', 'pink'}
然后假设我想输入一个字符串,并根据它们的类别重新排列单词。
'husky tulip red orange'
将成为
'red orange husky tulip'
顺序是先是颜色,然后是狗,然后是花。也许可以按顺序创建一个类别列表?不太确定我会怎么做这个
使用带有sorted
:的键函数
def ref(s):
dogs = {'husky', 'chihuahua', 'labrador', 'beagle'}
flowers = {'dandelion', 'rose', 'tulip'}
colours = {'blue', 'yellow', 'green', 'red', 'pink', 'orange'}
if s in colours: rtr=-3
elif s in dogs: rtr=-2
elif s in flowers: rtr=-1
else: rtr=0 # this puts words not found at end of string
return rtr
s='husky tulip red orange'
>>> ' '.join(sorted(s.split(), key=ref))
red orange husky tulip
更多Python(更容易扩展(是这样做的:
def ref(s):
dogs = {'husky', 'chihuahua', 'labrador', 'beagle'}
flowers = {'dandelion', 'rose', 'tulip'}
colours = {'blue', 'yellow', 'green', 'red', 'pink', 'orange'}
key_t=(colours, dogs, flowers)
try:
return next(i for i, v in enumerate(key_t) if s in v)
except StopIteration:
return -1. # this puts words not found at beginning of string
# or use the default argument version of next:
# return next((i for i, v in enumerate(key_t) if s in v), -1)
并以同样的方式使用该键函数。
您还可以通过使用chain将集合链接到一个可迭代的集合中来迭代集合:
>>> from itertools import chain
>>> [e for e in chain(colours,dogs,flowers) if e in s.split()]
['orange', 'red', 'husky', 'tulip']
哪个更快或更好取决于字符串的大小和集合的大小。此外,如果您想进行二次排序(例如在各个类别中进行词典编纂(,则需要使用sorted
方法。
试试这个
#Define all the categories
dogs = ['husky', 'chihuahua', 'labrador', 'beagle']
flowers = ['dandelion', 'rose', 'tulip']
colours = ['blue', 'yellow', 'green', 'red', 'pink', 'orange']
#The Input String
outOfOrder = "husky tulip red orange"
#Split up the string into an array which each word seperated
outOfOrderArray = outOfOrder.split()
#Array to hold all words of each category
orderedArray = [[], [], [], []]
#loop through all the words in the array
for word in outOfOrderArray:
#Check if the word is in each category.
if word in dogs:
orderedArray[2].append(word)
elif word in flowers:
orderedArray[1].append(word)
elif word in colours:
orderedArray[0].append(word)
#If its not in the array, do whatever you want with it. I jsut stuck them at the end.
else:
orderedArray[3].append(word)
orderedString = ""
#Combine all the words in ordered Array to create a final string
for category in orderedArray:
for word in category:
orderedString = orderedString + word + " "
print(orderedString)
您可以用1
键将鲜花推到后面,用-1
键将颜色拉到前面(其他所有东西都会用0
键放在中间(:
>>> ' '.join(sorted(s.split(), key=lambda w: (w in flowers) - (w in colours)))
'red husky orange tulip'
注意,";橙色";不在你的任何类别中,这就是为什么它最终与";哈士奇";。
一种更通用的方法是将花朵向后推得最远,将狗向后推得最少,将颜色向后推得最小:
>>> ' '.join(sorted(s.split(), key=lambda w: (w in flowers, w in dogs, w in colours)))
'orange red husky tulip'
只需按以下顺序从集合中提取匹配字符串:
>>>[i for i in list(colours)+list(flowers)+list(dogs) if i in input_list]
['red', 'orange', 'tulip', 'husky']
无需执行排序或其他操作。
如果您最初将它们定义为列表而不是集合,则会更简单。此外,它将保留您将放入列表的顺序,而不是对所有项目进行集体排序:
dogs = ['husky', 'chihuahua', 'labrador', 'beagle']
flowers = ['dandelion', 'rose', 'tulip']
colours = ['blue', 'yellow', 'green', 'red', 'pink', 'orange']
input_list = 'husky tulip red orange pink blue'.split()
[i for i in colours+flowers+dogs if i in input_list]
输出:
['blue', 'red', 'pink', 'orange', 'tulip', 'husky'] # note the ordering
p.S.对我来说似乎是最具蟒蛇性、最具时空效率、最具可扩展性和最快的方法。