Im a beginner and im tryin to do a couple things.
#1. i want to def a function "func(ListOWords)" for exampleand what i want it to do is take a list of words for example (["dod","fre","sdf","ser","ftre","dfr"]) and i want it to split them at the commas and rewrite them printing everyother word first then the remaining words after. that sounds alittle weird but it should look like this:
>>>func(["dod","fre","sdf","ser","ftre","dfr"])
["dod","sdf","ftre"],["fre","ser","dfr"]
anyone know how to do this?
this is what i got so far
def func(ListOWords):
[i.split() for i in ListOWords]
return ListOWords
can someone please help.
3 1520 bvdet 2,851
Expert Mod 2GB
Im a beginner and im tryin to do a couple things.
#1. i want to def a function "func(ListOWords)" for exampleand what i want it to do is take a list of words for example (["dod","fre","sdf","ser","ftre","dfr"]) and i want it to split them at the commas and rewrite them printing everyother word first then the remaining words after. that sounds alittle weird but it should look like this:
>>>func(["dod","fre","sdf","ser","ftre","dfr"])
["dod","sdf","ftre"],["fre","ser","dfr"]
anyone know how to do this?
this is what i got so far
def func(ListOWords):
[i.split() for i in ListOWords]
return ListOWords
can someone please help.
Maybe this will help: - >>> words = ["dod","fre","sdf","ser","ftre","dfr"]
-
>>> even = range(0, len(words), 2)
-
>>> odd = range(1, len(words), 2)
-
>>> [words[i] for i in even], [words[i] for i in odd]
-
(['dod', 'sdf', 'ftre'], ['fre', 'ser', 'dfr'])
-
>>>
Im a beginner and im tryin to do a couple things.
#1. i want to def a function "func(ListOWords)" for exampleand what i want it to do is take a list of words for example (["dod","fre","sdf","ser","ftre","dfr"]) and i want it to split them at the commas and rewrite them printing everyother word first then the remaining words after. that sounds alittle weird but it should look like this:
>>>func(["dod","fre","sdf","ser","ftre","dfr"])
["dod","sdf","ftre"],["fre","ser","dfr"]
anyone know how to do this?
this is what i got so far
def func(ListOWords):
[i.split() for i in ListOWords]
return ListOWords
can someone please help.
When you first get the list, you don't have to split it on the commas, because the commas aren't actually there. They are just a way of showing the separate elements in a list when your making a list or printing a list to the screen. You would want to use the split function to make a list if you had one long string with commas in it. - >>> words = "dod,fre,sdf,ser,ftre,dfr"
-
>>> ListOWords = words.split(",")
-
>>> print ListOWords
-
['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr']
You already have the words in the form of a list, so you can skip that part.
To accomplish what you want to do, I'd recommend slicing the list. To slice a list you put brackets after the list name like you would to access an item in the list, but instead of putting the index of one item inside the brackets, you put two separated with a semicolon. That will return a list of everything between those two indexes. - >>> ListOWords = ['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr']
-
>>> print ListOWords[1:4]
-
['fre', 'sdf', 'ser']
-
>>> print ListOWords[:4]#starts at the first index
-
['dod', 'fre', 'sdf', 'ser']
-
>>> print ListOWords[1:]#ends at the last index
-
['fre', 'sdf', 'ser', 'ftre', 'dfr']
-
>>> print ListOWords[:]#starts at the first index and ends at the last index
-
['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr']
Now there is one more option you have when slicing. By using another semicolon and number inside the brackets, you can change the "step" size. This allows you to pick every second or third or fourth or ... number. That's the key to solving your problem. - >>> ListOWords = ["dod","fre","sdf","ser","ftre","dfr"]
-
>>> list1 = ListOWords[::2]#same as list1 = ListOWords[0:6:2]
-
>>> list2 = ListOWords[1::2]#same as list1 = ListOWords[1:6:2]
-
>>> print list1, list2
-
['dod', 'sdf', 'ftre'] ['fre', 'ser', 'dfr']
If you want, you can do it all in one step. - >>> ListOWords = ['dod', 'fre', 'sdf', 'ser', 'ftre', 'dfr']
-
>>> print ListOWords[::2], ListOWords[1::2]
-
['dod', 'sdf', 'ftre'] ['fre', 'ser', 'dfr']
I hope that all makes sense.
Thanks u guys this really helped. i ahve some more questions but ill make a new discussion. hop eu read it soon!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: tjland |
last post by:
Okay so im working on a very simple encryption method using just loops.
Kind of novel i think. Okay so first i set up a list of the alphabet with
just every seperate letter, then user is prompted...
|
by: s |
last post by:
I'm getting compile errors on the following code:
<code>
#include <iostream>
#include <fstream>
#include <list>
#include <string>
using namespace std;
|
by: Little |
last post by:
I have this program and I need to work on the test portion, which tests
if a Val is in the list. It returns false no matter what could you look
at the part and see what might need to be done to fix...
|
by: comp.lang.tcl |
last post by:
I have a TCL proc that needs to convert what might be a list into a
string to read
consider this:
]; # OUTPUTS Hello World which
is fine for PHP
]; # OUTPUT {{-Hello}}
World, which PHP...
|
by: aine_canby |
last post by:
Hi,
Im totally new to Python so please bare with me.
Data is entered into my program using the folling code -
str = raw_input(command)
words = str.split()
for word in words:
|
by: Sheldon |
last post by:
Hi,
I am trying to understand linked lists and the different ways to write
a linked list and double linked list. I have been trying to get this
function called insert_word to work but to no...
|
by: nagar |
last post by:
I need to get the list of Autocorrect entries in word. Is there a way
to do it without connecting to Word? Is the list saved somewhere?
If I need to connect to Word, how can I detect if it's...
|
by: shapper |
last post by:
Hello,
How can I filter a List(Of String)?
I need to get the list elements which start with the letters contained
in the variable Text.
Thanks,
Miguel
|
by: saijin |
last post by:
I'm planning to call a list of data from an XML file but when I duplicate the content inside the <data></data> it is not showing anything
Here's the ActionScript 3.0
import...
|
by: jerry |
last post by:
i have a problem when i read c++ primer ,i don't know what the follow
codes meaing.
char *word={"frank","english","edali","slina"};
size_t word_size=sizeof(word)/sizeof(char*);...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| |