我有一个字符串数字数组,例如:
numbers = ['10', '8', '918', '101010']
当我使用sorted(numbers)
时,我会按字典顺序对它们进行排序,例如'8' > '17'
。
如何迭代根据数值排序的字符串?
答案 0 :(得分:6)
您可以使用带有键console.log('Script loaded...');
$(".btn-reg").on("click", reg);
function reg(event) {
console.log('Klick, klick...');
event.preventDefault(); // prevents the form to be submitted
var newusername = $(".newusername").val();
var newpassword = $(".newpassword").val();
var newclub = $(".newclub").val();
$.post('classCalling.php', {
newusername: newusername,
newpassword: newpassword,
newclub: newclub
},
function(data) {
console.log(data);
}
});
}
的内置sorted()
函数将列表中的每个项目映射到整数之前的整数:
int
<强>输出强>
numbers = ['10', '8', '918', '101010']
numbers = sorted(numbers, key=int)
print(numbers)
使用此方法将根据需要输出字符串列表。
答案 1 :(得分:3)
您可以使用内置的map
函数将数组元素转换为整数。
MapObject mapPlayer = currentMap.getLayers().get("objects").getObjects().get("player");
MapObjects mapObject = currentMap.getLayers().get("objects").getObjects();
for (Iterator<String> iter = mapPlayer.getProperties().getKeys(); iter.hasNext(); )
{
System.out.println("#############"+iter.next());
}
System.out.println("**************** player name : " + mapPlayer.getProperties().get("name", String.class));
RectangleMapObject rect = (RectangleMapObject) mapObject.get("player");
float x = (float) rect.getRectangle().x;
float y = (float) rect.getRectangle().y;
float width = rect.getRectangle().width;
float height = rect.getRectangle().height;
System.out.println("**************** player coordinates X : " +x);
System.out.println("**************** player coordinates Y: "+y);
System.out.println("**************** player coordinates width : " +width);
System.out.println("**************** player coordinates height: "+height);
System.out.println("**************** player coordinates X : " +mapPlayer.getProperties().get("x", Integer.class));
System.out.println("**************** player coordinates Y: "+mapPlayer.getProperties().get("y", Integer.class));
System.out.println("**************** player velocity : "+mapPlayer.getProperties().get("velocity", Integer.class));
输出:
print sorted(map(int, numbers))
答案 2 :(得分:1)
如果您想保留为字符串,可以将int
作为list.sort
的密钥传递给numbers = ['10', '8', '918', '101010']
numbers.sort(key=int)
,这将对原始列表进行排序:
/dir1/dir2/dir3/filename
答案 3 :(得分:0)
我喜欢这样做,这非常快。
composer install
答案 4 :(得分:0)
所有元素类型都是字符串,
>>> x=['4', '5', '29', '54', '4', '0', '-214', '542', '-64', '1', '-3', '6', '-6']
>>> max(x)
'6'
它按字母顺序对单词进行“排序”,并返回字母列表底部的单词
更多示例:
>>> list1 = ['kyle', 'dhamu']
>>> max(list1)
'kyle'
返回kyle,因为k在d之后
还请记住,从python3.7开始,您不能将字符串和整数混合使用max函数。下面是示例
>>> mix_list = ['my', 'name', 'is', 1 ]
>>> max(mix_list)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'int' and 'str'