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 1526 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: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |