or is the 123 being stored under 1 node

i thought that for example in my case the 3 would be the data element for the head since it's the last value in the list

nevermind i interpreted it wrong with the data element which was adding another element to the list. additionally for example i deleted lines 17 & 18 from your program to print and i noticed when printing list traversal it prints

1 2 3 7088664

where did this number come from?

If i wanted to know the # of nodes in the linked list, couldnt i do something of genre

printf("%d",*p)

your program actually does the same thing as mine, prints the same list, i tried 123 as input and after shows 123

additionally i want to say thanks for helping me reviewing this material, i am struggling to understand linked lists and pointers

i just tried doing that but it is still printing the same output

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
       int data;
       struct node *next;

};

void addtobegining(int);
void traverse();
struct node *head;
struct node *tail;
void removefromend();

int main()
{
    head = '\0';
    addtobegining(3);
    addtobegining(2);
    addtobegining(1);
    traverse();
    removefromend();
    traverse();
    system("pause");
}

void addtobegining(int i)
{
     struct node*p;
     p = malloc(sizeof(struct node));
     p ->data = i;
     p ->next = head;
     head = p;
}

void traverse()
{
     struct node *p;
     p = head;
     while(p)
     {
              printf("%d\n",p ->data);
              p=p ->next;
     }
}

void removefromend()
{
    struct node *curr, *prev;
    curr=head;
    prev='\0';
    while(curr!=tail)
    {
                     prev=curr;
                     curr=curr->next;
    }
    free(curr); 
    tail=prev;
    tail->next=NULL;
}

ok lets say now i wanted to create a function that would start removing from the end with the struct node above, how would i proceed with this?

i cant use next because i want it to go backwards and not forward

in main i would have to add

stuct node *end;

then create a function such as

void removefromend()
{
    struct node*p;
    p = malloc(sizeof(struct node));
    p->data = '\0';
    end = p; // i knw this part is incorrect
 }

I am reviewing material i have covered in c and i wrote up some notes and i am getting an error, i was hoping if someone could help me with this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node{
       int data;
       struct node *next;

};

void addtobegining(int);
void traverse();
struct node *head;

int main()
{
    head = (struct node*)malloc(sizeof(struct node));
    addtobegining(3);
    addtobegining(2);
    addtobegining(1);
    traverse();
}

void addtobegining(int i)
{
     struct node*p;
     p = malloc(sizeof(struct node));
     p ->next = i;
     head = p;
}

void traverse()
{
     struct node *p;
     p = head;
     while(p);
     {
              printf("%d\n",p ->data);
              p=p ->next;
     }
}

i am getting an error at function addtobegining

warning assignment makes pointer to interger without a cast

thanks in advance

I made a function which will look up ages in dictionary and give out there name

my dictionary si for example {george:16,amber:19}

i made this

[CODE]
search_age = raw_input("Provide age")
for age in list.values():
if age == search_age:
name = list[age]
print name[/CODE]

but i know how to compare and find the age i just dont know how to show the name of the person, additionally i am getting key error, i know it because of line 4, i know its not correct but i cant figure out to make it search backwards.

any help would be appreciated

Thanks in advance

[CODE][/CODE]ok this is what i get

[CODE]
my_dict = {1:"1"}

n = open('file.csv',"w")
for item in my_dict.items():
n.write(item)

Traceback (most recent call last):
File "<pyshell#12>", line 2, in <module>
n.write(item)
TypeError: expected a character buffer object
[/CODE]

i tried to edit the first post but it didnt allow me so i had to create another post and i actually included the error message in the first post.

Anyways can someone help with my problem?

Thanks in advance

[CODE]n = open("file.csv","w")
for item in my_dict.items():
n.write(item)
n.close()
[/CODE]

[QUOTE=woooee;1681170]f is never declared and so is not a character buffer.[/QUOTE]

please see my updated post above yours

im sorry i wrote it wrong

[CODE]
n = open("file.csv","w")
for item in my_dict.items():
f.write(item)
n.close()
[/CODE]

i get the error "error: expected a character buffer object"

but for example if i put it like this
[CODE]
n = open("file.csv","w")
for item in my_dict.items():
print item
n.close()
[/CODE]

it displays everything correctly on the screen, cant figure out what i am doing wrong

I am trying to write my dictionary to a csv and i am getting errors, was wondering if someone could tell me what i am doing wrong

this is my function

[CODE]

n = open(file.csv,"w")
for item in my_dict.items():
f.write(item)
f.close()
[/CODE]

my my_dict is for example {john ['25']}

when i use the function i get the error: expected a character buffer object but if i replace the write with print it works.

thanks in advance

I have a list for example

list = ['a','b','c','d']

and i give and i want to give the letters in the list numeric value for example

a=2,b=1,c=4,d=3

i want to display the list like this list = ['a','b','c','d']

but than i wanted to do a sum(list) and get a value in this case 10

is this possible to do and if so how?

[QUOTE=woooee;1669865]reverse=True means that it is sorted in reverse order, (largest to smallest), and [:3] only prints the first 3 items. To print them all in ascending order, use: [code]repeat = {"c":1, "b":2, "a":1}
print sorted([ (freq,elem) for elem, freq in repeat.items()]) [/code][/QUOTE]

i know the reverse= true, i just dont know about lambda, i have to look into that or if anyone could give me a definition i would appreciate it

no i have not, i actually dont know what it means, i have to look that up and thanks

my bad i left the repeat out

[CODE]
for elem in sentence:
repeat.setdefault(elem, 0)
repeat[elem] += 1

print sorted([ (freq,elem) for elem, freq in repeat.items()],reverse=True)[:3]  
print repeat

[/CODE]

[CODE]
for elem in sentence:
.setdefault(elem, 0)
repeat[elem] += 1

print sorted([ (freq,elem) for elem, freq in repeat.items()],reverse=True)[:3]  
print repeat    

[/CODE]

i have this loop which analyses the elements in the loop and then prints by frequency, i wanted to print by frequency and alphabetically.
example
[(5,apple),(5,orange)]

i got this

[CODE]
def text():
sentence = 0
while sentence !="EOF":
sentence = raw_input()
line1 =[]
processed_line = ""
for char in line:
if char.isalpha() or char.isalnum():
processed_line = processed_line+char

        else:
            line1.append(processed_line)
            processed_line = " "
    print processed_line

    print line1

text()

[/CODE]

[QUOTE=woooee;1668321]Your indentation is funky, probably using tabs instead of spaces. First, "print processed_line.split()" at the end of your code. Lists do not have a split() method.

You can use split() on the original string, but then you still have to iterate letter by letter and test for alpha as you do in your code. To get your code to run correctly, you should append "processed_line" when a non-alpha character is found (under the else) and then set "processed_line" to an empty string, ready to accept the next new word. Note that if you have some input like "I am a dog, I am not a cat.", you will get a break on the comma and on the space, leading to an empty "processed_line" being appended to the list. So, check processed_line for a positive length before appending. It would also be a good idea to print both "processed_line" and "line1" both before and after the append, while testing this code, so you know what is going on.

Also, the while() loop will never exit since "line" is never defined and therefore will never equal "EOF".
while line !="EOF":[/QUOTE]

i did some changes but there is a flaw with putting the append in the else statement, for example if i input this

i.going.to.work
it will only list [i am going to] and skip work because there is no non alpha character after work

and thanks for looking at my thread

[QUOTE=pyTony;1668332]I meant this:
[CODE]>>> import itertools

input = "i.am/lost"
...

i was trying to use the .split() but i am implementing wrong somehow

i am beginning to learn python so please be easy on me, i am trying to create a function that will accept user input and store it in a list, separating words and ignoring punctuation example:

input = i.am/lost

stores in list as "[i],[am][lost]"

this is what i got so far

[ICODE]
def text():
sentence = 0
while line !="EOF":
sentence= raw_input()
line1 = []
processed_line = ""
for char in sentence:
if char.isalpha():
processed_line = processed_line+char
else:
processed_line = processed_line+" "

    line1.append(processed_line)    
    line1.split()

    print processed_line

    print line1

text()
[/ICODE]

my problem is that its adding it all under 1 item in the list instead of separating word by word

thanks in advance

I am trying to create a python program but i have a question about a certain part of the loop for example i have

[CODE]
question = input("press 1 or 2")
if statement == 1:
print "somthing"

elif statement == 2:
print "abc"
[/CODE]

ok my question now is for example the user were to press 10, python would just skip the whole if loop, how can i make the message reappear and make the user chose the correct options?

[CODE]

include <iomanip>

include <iostream>

include <fstream>

include <string>

using namespace std;

int main ()
{
int i,count(0),n;

char strg1[50],strg2[2];
char ptr1(strg1), ptr2(strg2);

cout << "Please input the string" << endl;
cin >> strg1;

cout << "Please input the character" << endl;
cin>> strg2;

while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
count++;
ptr1++;
}
cout << "Count: " << count << endl;
system ("pause");
return 0;
[/CODE]

I did dot about assignment which counts the repeated letters of our choosing but how do i make it count the all the repeated letters?

My assignment is this:

Write a function that receives a pointer to a character string and return the number of repeated characters the occur in the string. For example, the string "mississippi" has three repeated characters. Do not count repeated blanks in the string. If a character occurs more than two times, it should still count as one repeated character. Assume that the function has the prototype statement
int repeat(char*ptr)