我的代码是:
UiManager Script using UnityEngine; using System.Collections;
public class UiManager : MonoBehaviour {
// Use this for initialization
public GameObject car;
public CarController moveCar;
public NetworkView nView;
string level1;
void Start () {
nView = GetComponent<NetworkView>();
}
// Update is called once per frame
void Update () {
ClientLeft();
// networkView.RPC;
}
public void Pause()
{
if(Time.timeScale == 1)
{
Time.timeScale = 0;
}
else if(Time.timeScale == 0)
{
Time.timeScale = 1;
}
}
string ip = "127.0.0.1" ;
int portno = 25001;
public void Server()
{
Network.InitializeServer(10, portno);
if (Network.peerType == NetworkPeerType.Server)
{
Application.LoadLevel("Menu");
}
}
public void Client()
{
Network.Connect(ip, portno);
if(Network.peerType == NetworkPeerType.Client)
{
Application.LoadLevel("clientSide");
}
}
void OnConnectedToServer()
{
Application.LoadLevel("clientSide");
}
public void ClientPaly()
{
if (Network.peerType == NetworkPeerType.Client)
{
nView.RPC("Play", RPCMode.Server);
}
}
public void ClientLeft()
{
if (Network.peerType == NetworkPeerType.Client)
{
// nView.RPC("moveLeft", RPCMode.Server);
moveCar.moveLeft();
}
}
public void ClientRight()
{
if (Network.peerType == NetworkPeerType.Client)
{
nView.RPC("moveRight", RPCMode.Server);
}
}
[RPC]
public void Play()
{
Application.LoadLevel("Level1");
}
}
如果maxlimit = 5
mystring = ' \r\n '.join([('' if (idx >= maxlimit) else str(name))
for idx,name in enumerate(queryset)])
,我怎样才能摆脱join()
方法中的for循环?
答案 0 :(得分:3)
好吧,你应该在列表理解中使用if
条件:
mystring = ' \r\n '.join([str(name) for idx, name in enumerate(queryset)
if idx < maxlimit])
这会生成仅包含5个项目的列表。
但是,更容易限制使用itertools.islice
进行迭代的项目数量;或者如果查询集(我不知道它支持什么),支持切片,那么只需用[:maxlimit]
切片:
from itertools import islice
' \r\n '.join([str(name) for name in islice(queryset, maxlimit)])
虽然对于每个元素的一个函数的简单应用,我经常使用map
,因为它需要更少的输入:
' \r\n '.join(map(str, islice(queryset, maxlimit)))