如何拆分列表列表

时间:2017-03-12 23:10:22

标签: python

所以我想拆分一份清单。

代码是

<?php session_start();

require_once "vendor/autoload.php"; //include library

//define scopes required to make the api call
    $scopes = array(
  "https://www.googleapis.com/auth/plus.stream.write",
  "https://www.googleapis.com/auth/plus.me"
);

// Create client object
$client = new Google_Client(); 
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/index.php');
$client->setAuthConfig("client_secret.json");
$client->addScope($scopes);

if( isset($_SESSION["access_token"]) ) {

  $client->setAccessToken($_SESSION["access_token"]);
  $service = new Google_Service_PlusDomains($client);

  $activity = new Google_Service_PlusDomains_Activity(
    array(
      'access' => array(
          'items' => array(
              'type' => 'domain'
          ),
          'domainRestricted' => true
      ),
      'verb' => 'post',
      'object' => array(
          'originalContent' => "Post using Google API PHP Client Library!" 
      ), 
    )
  );

  $newActivity = $service->activities->insert("me", $activity);


  var_dump($newActivity);


} else {

  if( !isset($_GET["code"]) ){

    $authUrl = $client->createAuthUrl();
    header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));

  } else {

    $client->authenticate($_GET['code']);
      $_SESSION['access_token'] = $client->getAccessToken();

      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/index.php';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

  }
}

?>

我试过用这个:

myList = [['Sam has an apple,5,May 5'],['Amy has a pie,6,Mar 3'],['Yoo has a Football, 5 ,April 3']]

但它一直给我错误信息

我想得到:

for i in mylist: i.split(",") 这种格式

3 个答案:

答案 0 :(得分:2)

这是你如何做到的。我使用内联for循环来遍历每个项目并用逗号分隔它们。

myList = [item[0].split(",") for item in myList]
print(myList)

OR 您可以枚举以正常迭代列表,随时重命名项目:

for index, item in enumerate(myList):
    myList[index] = myList[index][0].split(",")
print(myList)

OR 您可以在使用改进的值进行迭代时创建新列表:

newList = []
for item in myList:
    newList.append(item[0].split(","))
print(newList)

答案 1 :(得分:0)

这是因为它是一个列表清单。您的代码正在尝试拆分子列表,而不是字符串。简单地:

for i in mylist:
    i[0].split(",")

答案 2 :(得分:0)

拆分子列表中的每个字符串:)

{{1}}